我正在創建一個頁面用於將用戶添加電話。當提交按鈕被點擊時,新值沒有被保存在jsf backing bean用戶變量中
XHTML頁面:
<h:form id="form1">
<p:growl id="messages" showDetail="true"/>
<p:panel header="Phone" style="width: 400px;">
<p:panelGrid columns="2">
<h:outputLabel for="number" value="Number: " />
<p:inputText id="number" value="#{addUserBean.phone.number}"/>
<h:outputLabel for="name" value="Country: " />
<p:inputText id="name" value="#{addUserBean.phone.country.name}"/>
<f:facet name="footer">
<h:commandButton value="Add Phone" action="#{addUserBean.Add}"/>
</f:facet>
</p:panelGrid>
<p:spacer height="30px;"/>
<p:dataTable value="#{AddUserBean.user.phones}" var="phonex" editable="true">
<f:facet name="header">
Phone List
</f:facet>
<p:ajax event="rowEdit" listener="#{AddUserBean.onEdit}" update=":form1:messages" />
<p:ajax event="rowEditCancel" listener="#{AddUserBean.onCancel}" update=":form1:messages" />
<p:column>
<f:facet name="header">
<h:outputText value="Number" />
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{phonex.number}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{phonex.number}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Country:" />
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{phonex.country.name}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{phonex.country.name}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Options" style="width:50px">
<p:rowEditor />
</p:column>
</p:dataTable>
</p:panel>
</h:form>
支持bean:
@Named
@RequestScoped
public class AddUserBean {
private Phone phone;
private Country country;
private User user;
@Inject
UserBeanLocal userBean;
public AddUserBean(){
phone= new Phone();
phone.setCountry(new Country());
user= new User();
user.setPhones(new ArrayList<Phone>());
country= new Country();
}
public Phone getPhone() {
return phone;
}
public void setPhone(Phone phone) {
this.phone = phone;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public void Add(){
phone.getCountry().setName(country.getName());
user.getPhones().add(phone);
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
當我點擊提交按鈕它應該增加新的手機在其列表中的用戶並刷新數據表。我的問題是,當它提交用戶變量沒有更新(它是更新,但在刷新它清除變量),這可能是由於構造函數,但如果我刪除初始化空指針異常引發引起的。
看到了這個帖子,但沒有奏效。 試圖改變bean的屬性來SessionScoped(javax.enterprise.context.SessionScoped)
但這種例外提出org.jboss.weld.exceptions.DeploymentException: WELD-000072: Bean declaring a passivating scope must be passivation capable. Bean: Managed Bean [class com.totempool.admin.beans.AddUserBean] with qualifiers [@Default @Any @Named]
你需要一個更廣泛的作用域bean - 比請求作用域bean更寬/更寬。在這種情況下,視圖範圍的bean應該足夠了。最後一條消息指出你忘記在'AddUserBean'目標會話作用域CDI bean上實現'java.io.Serializable'接口(以及'serialVersionUID')。 – Tiny 2015-02-24 21:37:40