【发布时间】:2023-04-01 22:42:01
【问题描述】:
我有一个包含两个表单的 JSF 页面
我使用 JSF 2.2 和 MyFaces 实现
显示下一个示例(xhtml 和 ManagedBean)
当我按下 add Item(doNew) 按钮时,输入显示正确的值;在我按下返回(doCancel)按钮后,再次按下添加项目(doNew)。
当我按下第一个时,输入 exampleBean.newBean.descripcion 的值显示旧的描述。但是输出 #{exampleBean.newBean.descripcion}" 总是显示正确的值。如果我只使用一种形式,问题就很好买,我需要了解为什么不能使用两种形式?
问候,
<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:body>
<h:form styleClass="horizontal-form form-search" id="form1">
<h:messages></h:messages>
<h:commandButton styleClass="btn btn-default btn-success"
value="Add Item" action="#{exampleBean.doNew}">
</h:commandButton>
</h:form>
<h:form id="form2">
<h:messages></h:messages>
<h:outputText value="#{exampleBean.newBean.descripcion}"></h:outputText>
<h:inputText value="#{exampleBean.newBean.descripcion}" id="input1234"></h:inputText>
<h:commandButton styleClass="btn btn-default btn-default"
value="Back" action="#{exampleBean.doCancel}"
immediate="true">
</h:commandButton>
</h:form>
我的 ManagedBean 是
@ManagedBean(name = "exampleBean")
@ViewScoped
public class ExampleManagedBean {
private Object newBean;
protected String panelMode = null;
public ExampleManagedBean() {
// TODO Auto-generated constructor stub
panelMode = "default";
newBean = new Concepto();
}
public String doCancel() {
panelMode = "default";
return null;
}
public String doNew() {
panelMode = "edit";
Concepto c1 = new Concepto();
c1.setDescripcion("descripcion " + System.currentTimeMillis());
newBean = c1;
return null;
}
public Object getNewBean() {
return newBean;
}
public void setNewBean(Object newBean) {
this.newBean = newBean;
}
public String getPanelMode() {
return panelMode;
}
public void setPanelMode(String panelMode) {
this.panelMode = panelMode;
}
}
【问题讨论】: