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

时间:2016-01-15 11:06:46

标签: java user-interface listview wicket radio-group

我有一个页面,根据我从上一页收到的列表显示一些无线电选项。 Concretely- 第一页 - >按下提交按钮 - >生成的列表 - >本页 - >填充广播组(使用列表视图)

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

另外,如果我在构造函数外部初始化我的无线电组或列表视图,我会收到“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) {

    }
}

如何在构造函数外部或条件(list not null)下填充无线电组而不会导致wicket:id错误?请帮忙。任何建议都是最受欢迎的。

2 个答案:

答案 0 :(得分:1)

已更新为代码段

页面装入App init()方法 - 所以'实例'已创建

<br />
<a href="#" class="btn">Do Stuff</a>

第一页 - Java

mountPage("radioOptionPage", TestTargetPage.class);

首页 - 加价

    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);
}

第二页 - Java

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

</form>
</body>

第二页 - 标记

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;
}}

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

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

答案 1 :(得分:0)

我终于得到了它(在朋友的帮助下)... 我在第一页内制作了public class ContainsTest { private enum Container { VAL1, VAL2, VAL3; } public static void main(String[] args) { String arg1 = "VAL1"; String arg2 = "VAL4"; if(ContainsTest.existsVal(arg1)) { System.out.println("Command " + arg1 + " is going to be executed"); } else { System.out.println("Command " + arg1 + " not aviable"); } if(ContainsTest.existsVal(arg2)) { System.out.println("Command " + arg2 + " is going to be executed"); } else { System.out.println("Command " + arg2 + " not aviable"); } } private static boolean existsVal(String arg) { try { // This method does check if an enum with the given arg exists Container.valueOf(arg); return true; } catch(IllegalArgumentException e) { // If it does not exist then an exception will occur that has to be caught. return false; } } } 一个SelectionPage。 即Panel

我在其中创建了此功能,以根据新列表重新填充广播组。

SelectionPage extends Panel

然后在第一页上,我添加了以下几行,我初始化了面板..

public void reloadList(DataPassRegister dataPassRegister) {

    this.form.remove("selectGroup");
    final RadioGroup selectGroup = new RadioGroup("selectGroup");

    selectGroup.setOutputMarkupId(true);
    selectGroup.setOutputMarkupPlaceholderTag(true);
    add(selectGroup);
    this.form.add(selectGroup);

    final ListView selectRepetor = new ListView("selectRepetor", dataPassRegister.getNameList())
        {
        @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()));
            radio.setOutputMarkupId(true);
            radio.setOutputMarkupPlaceholderTag(true);
            item.add(radio);
            item.add(label);
            item.setOutputMarkupId(true);
            radio.add(new AjaxEventBehavior("onclick")
            {
                @Override
                protected void onEvent(AjaxRequestTarget target)
                {
                }
            });
        }
    };
    selectRepetor.setOutputMarkupId(true);
    selectRepetor.setOutputMarkupPlaceholderTag(true);
    selectGroup.add(selectRepetor);
}

这确保了面板稍后会变得可变。 最后,当我准备用新列表填充面板时,我调用上面的函数。即。

selectionPage.setOutputMarkupId(true);
selectionPage.setOutputMarkupPlaceholderTag(true);

这允许面板重新填充无线电组。但是,到目前为止,我必须添加反馈面板,以确保在按下提交时对话框不会关闭。