这是一个简单的测试用例,我把它放在一起.
支持Bean.一个家庭有一个孩子的名单.
@VIEwScoped@ManagedBeanpublic class TestbackingBean implements Serializable { private Family f = new Family(); private Child childToRemove; public TestbackingBean() { f.addChild(new Child()); } public Family getFamily() { return f; } public voID setChildToRemove(Child childToRemove) { this.childToRemove = childToRemove; } public TimeZone getTimezone() { return TimeZone.getDefault(); } public List<Child> getChildren() { return f.getChildrenAsList(); } public Child getChildToRemove() { return childToRemove; } public voID addChild() { f.addChild(new Child()); } public voID removeChild() { f.removeChild(childToRemove); }}
这是JsF页面:
<!DOCTYPE HTML PUBliC "-//W3C//DTD xhtml 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-Transitional.dtd"><HTML xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/Jsf/core" xmlns:ui="http://java.sun.com/Jsf/facelets" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich" xmlns:h="http://java.sun.com/Jsf/HTML"><h:head> <Meta http-equiv="Content-Type" content="text/HTML; charset=UTF-8" /> <Title><ui:insert name="Title" /> </Title> <link rel="stylesheet" type="text/CSS" href="/hcbb/CSS/main.CSS" /></h:head><h:body> <h:form> <h:panelGroup ID="parentSection"> <h:outputLabel value="Parent name" for="parent_firstname" /> <h:inputText ID="parent_firstname" requiredMessage="required" immediate="true" label="Parent First name" value="#{testbackingBean.family.firstname}"> <f:valIDaterequired /> </h:inputText> <rich:message ID="parent_firstnameMessage" for="parent_firstname" /> </h:panelGroup> <h:panelGroup ID="childrenSection"> <h:datatable value="#{testbackingBean.children}" var="child"> <h:column> <h:panelGrID ID="childPanel" columns="3" > <h:outputText ID="childTitle" value="Child" /> <h:outputText ID="spacer" /> <a4j:commandbutton ID="removeBtn" action="#{testbackingBean.removeChild}" immediate="true" value="Remove Child" render="childrenSection" title="Remove"> <f:setPropertyActionListener target="#{testbackingBean.childToRemove}" value="#{child}" /> </a4j:commandbutton> <h:outputLabel ID="child_firstnameLbl" value="First name" /> <h:inputText ID="child_firstname" requiredMessage="required" immediate="true" label="Child First name" value="#{child.firstname}"> <f:valIDaterequired /> </h:inputText> <rich:message ID="child_firstnameMessage" for="child_firstname" /> <h:outputLabel ID="child_lastnameLbl" value="Last name" /> <h:inputText ID="child_lastname" requiredMessage="required" immediate="true" label="Child Last name" value="#{child.lastname}"> <f:valIDaterequired /> </h:inputText> <rich:message ID="child_lastnameMessage" for="child_lastname" /> <h:outputLabel ID="child_dobLbl" value="Birth Date" /> <h:inputText ID="child_dob" label="Child Birth Date" immediate="true" requiredMessage="required" value="#{child.dateOfBirth}"> <f:convertDateTime ID="dobConverter" pattern="MM/dd/yyyy" timeZone="#{testbackingBean.timezone}" /> <f:valIDaterequired /> </h:inputText> <rich:message ID="child_dobnameMessage" for="child_dob" /> </h:panelGrID> </h:column> </h:datatable> <a4j:commandlink ID="addChildBtn" immediate="true" render="childrenSection" action="#{testbackingBean.addChild}" value="Add Another Child"> </a4j:commandlink> </h:panelGroup> </h:form></h:body></HTML>
问题是在添加/删除子节时保存值.如果您输入父姓名,然后输入子姓名和出生日期,然后单击添加您刚添加的孩子的字段将消失?我会认为按钮上的immediate = true,字段将通过它们.
问题是添加父名,输入子信息并单击添加另一个子按钮,您刚刚输入的子信息将被删除.
关于我如何能够使这一切工作的任何建议.看起来像一个相当简单和有点标准的用例.
谢谢!
解决方法 乍一看看起来很好看.问题症状归结为f.getChildrenAsList()在JsF即将应用请求值时不返回包含新子项的列表.也许该方法在提交后再次从DB重新获取列表?添加断点以调查其retun值.或者视图范围可能失败并导致bean被重建?在bean的构造函数中添加一个断点.当您针对同一视图提交表单时,不应重建它.至于使用immediate属性,你对它们的所有使用都是多余的.只需删除它们.要更好地理解它的用法,请参阅Debug JSF lifecycle文章(的确如此,它是JsF 1.2的目标,但JsF2的原理是相同的),然后特别阅读本摘要:
07001总结If it isn’t entirely clear yet,here’s a summary,complete with real world use examples when they may be beneficial:
If set in
UIinput
(s) only,the process valIDations phase will be taken place in apply request values phase instead. Use this to prioritize valIDation for theUIinput
component(s) in question. When valIDation/conversion fails for any of them,the non-immediate components won’t be valIDated/converted.If set in
UICommand
only,the apply request values phase until with update model values phases will be skipped for any of theUIinput
component(s). Use this to skip the entire processing of the form. E.g. “Cancel” or “Back” button.If set in both
UIinput
andUICommand
components,the apply request values phase until with update model values phases will be skipped for any of theUIinput
component(s) which does not have this attribute set. Use this to skip the processing of the entire form expect for certain fIElds (with immediate). E.g. “Password forgotten” button in a login form with a required but non-immediate password fIEld.
以上是内存溢出为你收集整理的动态地向JSF表单添加字段全部内容,希望文章能够帮你解决动态地向JSF表单添加字段所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)