【问题标题】:AjaxFormComponentUpdatingBehaviour issue after Form submitting in Wicket在 Wicket 中提交表单后的 AjaxFormComponentUpdatingBehaviour 问题
【发布时间】:2012-04-27 11:17:29
【问题描述】:

我对@9​​87654321@ 有这样的问题。当您将其设置为表单上的某些组件并为其添加验证时,在您按下“提交表单”按钮后,假设您收到一个错误,即您的组件未通过验证,之后 ajax 是表现不同,不更新模型。

这里是代码示例:

TextField someText = new TextField("someTextId");
someText.setRequired(true); //added validation on requireness
CheckBox checkBx = new CheckBox("checkBxId");
TextField changeableTxt = new TextField("changeableTxtId");
changeableTxt.setEnabled(false);

checkBx.add(new AjaxFormComponentUpdatingBehaviour("onclick"){
protected void onUpdate(AjaxRequestTarget target) {
    if(compoundModel.isCheckBx()){
         changeableTxt.setEnabled(true);
         target.addComponent(changeableTxt);
    }else{
         compoundModel.setChangeableTxt(null);
         changeableTxt.setEnabled(false);
         target.addComponent(changeableTxt);
    }
  }
});
Form form = new Form("form", compoundModel);
form.add(someText, checkBx, changeableTxt);
add(form);

所以如果检查checkBx,请输入一些值到changeableTxt,将someText留空并按提交按钮,会出现错误,该字段 someText 是必需的。之后,如果我们点击checkBx,它将使changeableTxt字段被禁用,但它会在输入值之前离开,而不是null。

【问题讨论】:

    标签: java ajax submit wicket behavior


    【解决方案1】:

    让我们开始解释为什么您可能认为您的代码可以正常工作: AjaxFormComponentUpdatingBehavior 将更新 CheckBox 的模型,但仅更新此模型。这意味着,如果您删除代码行 compoundModel.setChangeableTxt(null);changeableTxt 甚至会保持为空

    因此,如果 Checkbox 应该更改 changeableTxt TextField 的值,它应该在单击它时提交它的值。您可以通过在 checkBxchangeableTxt 周围包裹一个表单来实现此目的,并在单击 CheckBox 时使用 AjaxFormSubmitBehavior 提交此表单。

    public class TestingPanel extends Panel {
        public TestingPanel(String id) {
        super(id);
    
        final CompoundModel compoundModel = new CompoundModel();
    
        final Form<CompoundModel> form = new Form<CompoundModel>("form",
                new CompoundPropertyModel<CompoundModel>(compoundModel)) {
            @Override
            protected void onValidate() {
                System.out.println("validate: "
                        + compoundModel.getChangeableTxt());
                System.out.println("validate: "
                        + getModel().getObject().getChangeableTxt());
    
                super.onValidate();
            }
        };
        form.setOutputMarkupId(true);
        add(form);
    
        TextField someText = new TextField("someText");
        someText.setRequired(true); // added validation on requireness
        final CheckBox checkBx = new CheckBox("checkBx");
        final TextField changeableTxt = new TextField("changeableTxt");
        changeableTxt.setOutputMarkupId(true);
        changeableTxt.setEnabled(false);
    
        Form checkBoxForm = new Form("checkBoxForm");
        form.add(checkBoxForm);
    
        AjaxFormSubmitBehavior submitBehavior = new AjaxFormSubmitBehavior(
                checkBoxForm, "onclick") {
    
            @Override
            protected void onSubmit(AjaxRequestTarget target) {
                if (checkBx.getModelObject() == true) {
                    changeableTxt.setEnabled(true);
                    target.add(changeableTxt);
                } else {
                    compoundModel.setChangeableTxt(null);
                    changeableTxt.setEnabled(false);
                    target.add(changeableTxt);
                }
    
            }
    
            @Override
            protected void onError(AjaxRequestTarget target) {
    
            }
        };
        checkBx.add(submitBehavior);
        checkBoxForm.add(checkBx, changeableTxt);
    
        AjaxFormComponentUpdatingBehavior updateBehavior = new AjaxFormComponentUpdatingBehavior(
                "onclick") {
            protected void onUpdate(AjaxRequestTarget target) {
                if (compoundModel.isCheckBx()) {
                    changeableTxt.setEnabled(true);
                    target.addComponent(changeableTxt);
                } else {
                    // compoundModel.setChangeableTxt("");
                    changeableTxt.setEnabled(false);
                    target.add(changeableTxt);
                }
            }
        };
    
        form.add(someText);
    
        FeedbackPanel feedbackPanel = new FeedbackPanel("feedbackPanel");
        form.add(feedbackPanel);
    
        AjaxSubmitLink submit = new AjaxSubmitLink("submit", form) {
    
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                target.add(form);
            }
    
            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                target.add(form);
    
            }
        };
        add(submit);
    
    }
    
    class CompoundModel implements Serializable {
    
        private boolean checkBx = false;
    
        private String someText = null;
    
        private String changeableTxt = null;
    
        public boolean isCheckBx() {
            return checkBx;
        }
    
        public void setCheckBx(boolean checkBx) {
            this.checkBx = checkBx;
        }
    
        public String getSomeText() {
            return someText;
        }
    
        public void setSomeText(String someText) {
            this.someText = someText;
        }
    
        public String getChangeableTxt() {
            return changeableTxt;
        }
    
        public void setChangeableTxt(String changeableTxt) {
            this.changeableTxt = changeableTxt;
        }
    
    }
    }
    

    使用以下html:

    <!DOCTYPE html>
    <html xmlns:wicket="http://wicket.apache.org">
    <wicket:panel>
        <form wicket:id="form">
            <div wicket:id="feedbackPanel" />
            <input type="text" wicket:id="someText"  /><br />
            <form wicket:id="checkBoxForm">
                <input type="checkbox" wicket:id="checkBx" /><br />
                <input type="text" wicket:id="changeableTxt" /><br />
            </form>
        </form>
        <a wicket:id="submit">submit</a>
    </wicket:panel>
    

    【讨论】:

    • Ive did everything, as you said, Thorsten, but it appears, that, when I click checkBox it makes textfield enabled, but when I click it once again, it doesnt 使文本字段禁用并为空
    • 我一到家就会检查。我将提供我完整的 java 和 html 文件,以便我们可以看到我们偏离轨道的地方
    • 好吧,这 aint working normal, my forms doesnt 在点击时会发生变化,但我找到了解决此问题的其他方法。我创建了新项目,并为它们设置了一些值,因此提交后不会更新的不是旧组件,而是新的新组件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 2015-12-09
    相关资源
    最近更新 更多