gpt4 book ai didi

java - Wicket:在页面构造函数外部动态填充 RadioGroup

转载 作者:行者123 更新时间:2023-11-30 07:40:11 27 4
gpt4 key购买 nike

我有一个页面,根据我从上一页收到的列表显示一些单选选项。具体来说——第一页 --> 按下提交按钮 --> 生成列表 --> 本页 --> 填充单选组(使用 ListView )

问题是所有页面都在应用程序启动时初始化。因此,如果我在构造函数中填充我的单选按钮组,我会得到一个空白页面(因为列表为空)。

此外,如果我在构造函数之外初始化我的单选按钮组或 ListView ,我会收到“wicket id not found”错误(因为 wicket 在初始化时在类中找不到 wicket:id)。

这是我的 HTML 代码:

<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
</head>
<body>
<wicket:panel>
<form wicket:id="selectionPageForm">
<table width="100%" cellpadding="5" class="datapass">
<tr>
<td>
<div wicket:id="selectGroup">
<span wicket:id="selectRepetor">
<input type="radio" class="radioAlign" wicket:id="selection" checked="checked"/>
<label class="check" style="float:none;" wicket:id="name">[bankName]</label>
</span>
</div>
</td>
</tr>
</table>
<div wicket:id="feedback"></div>
</form>
<hr>
</wicket:panel>
</body>
</html>

这是我的 Java 页面:dataPassRegister只是一个包含String ArrayList的POJO

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.Radio;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import com.googlecode.wicket.jquery.ui.panel.JQueryFeedbackPanel;
import com.googlecode.wicket.jquery.ui.widget.dialog.AbstractFormDialog;
import com.googlecode.wicket.jquery.ui.widget.dialog.DialogButton;


public class SelectionPage extends AbstractFormDialog<DataPassRegister>{

private final Form<DataPassRegister> form;
private final FeedbackPanel feedback;
protected final DialogButton btnSubmit = new DialogButton("Submit");
protected final DialogButton btnCancel = new DialogButton(LBL_CANCEL);
private ArrayList<String> nameList = new ArrayList();

public SelectionPage(String id, String title, DataPassRegister dataPassRegister) {

super(id, title, true);
this.form = new Form<DataPassRegister>("selectionPageForm");

final RadioGroup selectGroup = new RadioGroup("selectGroup");

if(dataPassRegister.getNameList() != null){
nameList = dataPassRegister.getNameList();
System.out.println("In selection - In if condition - List : " + nameList.toString());
}

final ListView selectRepetor = new ListView("selectRepetor", nameList)
{
@Override
protected void populateItem(final ListItem item)
{
final Radio radio = new Radio("selection", new Model(item.getModelObject().toString()));
final Label label = new Label("name", new Model(item.getModelObject().toString()));
item.add(radio);
item.add(label);
}
};
selectGroup.add(selectRepetor);

this.feedback = new JQueryFeedbackPanel("feedback");

this.form.add(selectGroup);
this.form.add(this.feedback);
this.add(this.form);
}

@Override
public Form<?> getForm() {
return this.form;
}

@Override
public void setModelObject(DataPassRegister dataPassRegister)
{
this.setDefaultModel(new CompoundPropertyModel<DataPassRegister>(dataPassRegister));
}

@Override
protected List<DialogButton> getButtons()
{
return Arrays.asList(this.btnSubmit, this.btnCancel);
}

@Override
protected DialogButton getSubmitButton() {
return this.btnSubmit;
}

@Override
protected void onError(AjaxRequestTarget target) {
target.add(this.feedback);
}

@Override
protected void onSubmit(AjaxRequestTarget target) {

}
}

如何在构造函数外部或在某种条件(列表不为空)下填充单选组,而不会导致 wicket:id 错误?请帮忙。欢迎任何建议。

最佳答案

已更新代码片段

页面已安装在 App init() 方法中 - 因此创建了“实例”

mountPage("radioOptionPage", TestTargetPage.class);

首页 - Java

    private void addFormComponent(Form<Void> form) {

final FeedbackPanel panel = new FeedbackPanel("statusbarpanel");
panel.setOutputMarkupId(true);
form.add(panel);

AjaxFallbackButton button = new AjaxFallbackButton("submitbtn", form) {
@Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) {

PageParameters parameters = new PageParameters();
for(int i = 0; i < 3; i++){
String random = RandomStringUtils.random(4, true, true);
parameters.add(random, random);
}
setResponsePage(TestTargetPage.class, parameters);
}
};
form.add(button);
}

首页 - 标记

<body>
<form wicket:id="imageForm">
<div wicket:id="statusbarpanel"></div>
<input type="submit" wicket:id="submitbtn" value="Click Me !!!"/>

</form>
</body>

第二页 - Java

public class TestTargetPage extends WebPage {

private FormModel model = new FormModel();

public TestTargetPage(PageParameters parameters) {

List<String> options = new ArrayList<String>();

if (parameters != null){
List<INamedParameters.NamedPair> allNamed = parameters.getAllNamed();
for (INamedParameters.NamedPair pair : allNamed){
options.add(pair.getValue());
}

model.setObject(options);
}

Form<Void> form = new Form<Void>("targetForm");
RadioChoice<String> radiOptions = addRadioGroups("radio");
form.add(radiOptions);

add(form);
}

private RadioChoice<String> addRadioGroups(String wicketId) {
List<String> radioOptions = model.getObject();
RadioChoice<String> radioChoice = new RadioChoice<String>(wicketId, radioOptions);

radioChoice.setOutputMarkupId(true);

return radioChoice;
}}

第二页 - 标记

<body>
<h1>Wicket RadioChoice Example</h1>

<form wicket:id="targetForm">
<p>
<label>Radio options :</label>
<br />
<span wicket:id="radio"></span>
</p>
<input type="submit" value="Display" />
</form>

</body>

如果我理解正确,您想要的只是在页面加载时动态设置单选组。在这种情况下,您可以为页面设置模型对象,从上一页填充该模型对象,并将源页面设置为目标页面中的默认模型对象,然后重定向到该目标页面。

通过为页面设置默认模型对象,您应该能够根据需要设置单选按钮。单选按钮的值将来自页面的默认模型对象。

关于java - Wicket:在页面构造函数外部动态填充 RadioGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34809689/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com