开发者

restore form data after validation error in jsf

开发者 https://www.devze.com 2023-02-01 20:10 出处:网络
I have a combo box(drop down list) and a text field on the page. Both of them has required set to true. When I select a value in the drop down and submit the form, the validation kicks in and I\'m red

I have a combo box(drop down list) and a text field on the page. Both of them has required set to true. When I select a value in the drop down and submit the form, the validation kicks in and I'm redirected to the same page since the text field value is null. Which is good but I loose the data selected in the drop down list. How can I retain the form dat开发者_高级运维a when there is a validation error? I'm using facelets with jsf1.2.


HI,

It is working fine in JSF 2.0. I have just tried it. I have written the following code:

 <f:view>
        <h:form>
            <h:inputText id="name" value="#{jsfBean.name }" required="true"/>
            <h:selectOneMenu id="items" value="#{jsfBean.selectedItem }" required="true">
                <f:selectItems value="#{jsfBean.items }"/>
            </h:selectOneMenu>
            <h:commandButton id="submit" value="Submit" action="#{jsfBean.submit }" />
        </h:form>
    </f:view>

private String selectedItem;
private ArrayList<SelectItem> items = null;
public String getSelectedItem() {
    this.selectedItem = "--Select--";
    return selectedItem;
}
public void setSelectedItem(String selectedItem) {      
    this.selectedItem = selectedItem;
}
public ArrayList<SelectItem> getItems() {
    this.items = new ArrayList<SelectItem>();
    SelectItem selectItem = new SelectItem("1","1");
    SelectItem selectItem1 = new SelectItem("--Select--","--Select--");
    this.items.add(selectItem);
    this.items.add(selectItem1);
    return items;
}
public void setItems(ArrayList<SelectItem> items) {
    this.items = items;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消