我创建了托管 bean(CourtUitility.java),它有两个属性 court(CourtBean.java 的对象)和 courtAll(List 对象包含 courtbean 的数组列表)。我的代码如下 CourtUitility.java
@ManagedBean
@RequestScoped
public class CourtUitility {
private CourtBean court =new CourtBean();
private List<CourtBean> courtAll = new ArrayList<CourtBean>();
/** Creates a new instance of CourtUitility */
public CourtUitility() {
courtAll = new ArrayList<CourtBean>();
int userID = Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("USER_ID").toString());
courtAll = CourtService.GenerateCourtList(userID);
}
public CourtBean getCourt() {
return court;
}
public void setCourt(CourtBean court) {
this.court = court;
}
public List<CourtBean> getCourtAll() {
return courtAll;
}
public void setCourtAll(List<CourtBean> courtAll) {
this.courtAll = courtAll;
}
public void save(ActionEvent actionEvent) {
court.setUserID(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("USER_ID").toString()));
System.out.println("Court ID : " + court.getCourtID());
System.out.println("User ID : " + court.getUserID());
CourtService.AddCourt(court);
court = new CourtBean();
courtAll = CourtService.GenerateCourtList(Integer.parseInt(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("USER_ID").toString()));
FacesMessage msg = new FacesMessage("Court Record Created", "");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
clientMaster.xhtml
<ui:composition template="/template/mainLayout.xhtml" >
<ui:define name="pageContent">
<h:form id="co">
<p:growl life="5000" showDetail="true" showSummary="true" id="courtmessage" />
<p:panel id="courtDetail" header="Court Details">
<p:messages id="panelMessage" showDetail="true" showSummary="false" autoUpdate="true" globalOnly="true" />
<h:panelGrid columns="4" styleClass="grid">
......
<p:commandButton id="addCourt" immediate="true" value="Add Court" actionListener="#{courtUitility.save}" update=":co,:cot" async="true" >
</p:commandButton>
</h:panelGrid>
</p:panel>
</h:form>
<ui:include id="abc" src="CourtEditDatatable.xhtml"/>
</ui:define>
</ui:composition>
当我通过命令按钮输入提交表单时,数据库中的所有数据都为空。调试后发现,提交表单时,首先调用托管bean构造函数并重新初始化所有属性。在调用该动作侦听器之后,它在数据库中为空。我也尝试过查看范围,但问题仍然存在。有没有办法在调用动作监听器时停止调用构造函数???
要分享的另一件事是,我在同一个项目中使用向导创建了具有相同模式的客户端主控。它工作正常。保存记录时不会引起问题。是因为巫师吗??