我遇到了JSF的问题,希望有人可以帮助我。 我有一个Facelets xhtml页面,带有SessionScoped托管bean。当在页面上按下命令按钮时,在bean中调用一个方法,该方法在页面上使用另一个命令按钮创建另一个表单。到目前为止,这非常有效,但是当在页面上按下新创建的按钮时,它会以某种方式调用动作方法两次。 以下是我的支持bean的代码示例:
动态创建新表单的方法,并且工作正常:(我使用setActionExpression将操作绑定到按钮)
public void doCreateScreen() {
FacesContext context = FacesContext.getCurrentInstance();
Application application = context.getApplication();
panelGrid.getChildren().clear();
HtmlPanelGrid checkBoxGrid = (HtmlPanelGrid) application
.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
checkBoxGrid.setColumns(columnNumber + 1);
checkBoxGrid.getChildren().clear();
for (int i = 1; i < rowNumber + 1; i++) {
for (int j = 1; j < columnNumber + 1; j++) {
HtmlSelectBooleanCheckbox tmpCheckBox = (HtmlSelectBooleanCheckbox) application
.createComponent(HtmlSelectBooleanCheckbox.COMPONENT_TYPE);
String id = String.valueOf(i * columnNumber + j);
tmpCheckBox.setId("box" + id);
tmpCheckBox.setValue(true);
checkBoxGrid.getChildren().add(tmpCheckBox);
}
}
HtmlCommandButton createSeatsButton = (HtmlCommandButton) application
.createComponent(HtmlCommandButton.COMPONENT_TYPE);
createSeatsButton.setValue("Create Screen");
MethodExpression createSeats = application.getExpressionFactory()
.createMethodExpression(context.getELContext(),
"#{screencontroller.doCreateSeats}", null,
new Class<?>[0]);
createSeatsButton.setActionExpression(createSeats);
panelGrid.getChildren().add(checkBoxGrid);
panelGrid.getChildren().add(createSeatsButton);
}
此方法被调用两次:
public void doCreateSeats() {
try {
screen = screenOperator.createScreen(screen);
for (int i = 1; i < rowNumber + 1; i++) {
for (int j = 1; j < columnNumber + 1; j++) {
Seat seat = new Seat();
seat.setRow(i);
seat.setSeatNumber(j);
seat = screenOperator.createSeat(seat,
screen.getIdScreen());
}
}
panelGrid.getChildren().clear();
} catch (MovieTicketsException e) {
// TODO
e.printStackTrace();
}
}
和xhtml来源:
<div class="addform">
<h1>Add new screen</h1>
<h:form prependId="false">
<h:panelGrid columns="2">
<h:outputText value="Description:"></h:outputText>
<h:inputText value="#{screencontroller.screen.description}"
title="Description" id="Description" required="true"
class="inputfield">
<f:validateLength minimum="1" maximum="255"></f:validateLength>
</h:inputText>
<h:outputText value="Rows:"></h:outputText>
<h:selectOneMenu value="#{screencontroller.rowNumber}" title="Rows"
id="Rows" required="true" class="inputfield">
<f:selectItems value="#{screencontroller.rowsSelectItems}">
</f:selectItems>
</h:selectOneMenu>
<h:outputText value="Seats in a row:"></h:outputText>
<h:selectOneMenu value="#{screencontroller.columnNumber}"
title="Columns" id="Columns" required="true" class="inputfield">
<f:selectItems value="#{screencontroller.rowsSelectItems}">
</f:selectItems>
</h:selectOneMenu>
<h:outputLabel></h:outputLabel>
<h:commandButton action="#{screencontroller.doCreateScreen}"
value="Submit"></h:commandButton>
</h:panelGrid>
</h:form> <h:message for="Description" class="errormsg"></h:message> <h:message
for="Cinema" class="errormsg"></h:message></div>
<div class="listdiv"><h:form prependId="false">
<h:panelGrid binding="#{screencontroller.panelGrid}" columns="1">
</h:panelGrid>
</h:form></div>
</div>
我刚刚开始学习jsf,所以也许我做的事情确实错了,但如果有人能解释一下,我会非常感激。
由于