【问题标题】:Multiple Forms with JSF 2.2使用 JSF 2.2 的多种表单
【发布时间】: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;
}

}

【问题讨论】:

    标签: jsf-2 myfaces


    【解决方案1】:

    commandButton 提交其表单的值。并且看不到其他形式的值。

    所以在你的情况下,你必须把你的 commandButton 放在与要添加的值相同的形式中。


    表单标签呈现一个 HTML 表单元素。 JSF 表单使用“回发”技术将表单数据提交回包含表单的页面

    http://www.jsftoolbox.com/documentation/help/12-TagReference/html/h_form.html

    【讨论】:

    • 是的,但输入值是从操作中检索的。为什么第一次工作?为什么输出文本正确显示?同一对象的渲染输出和输入不同。
    • 请尝试在没有立即 =“true” 的情况下进行测试,并检查当您通过 Button Back 提交时您的 managedBean 中的值是否正确
    猜你喜欢
    • 2014-12-25
    • 2014-07-27
    • 2013-09-12
    • 2013-05-26
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 2011-07-28
    相关资源
    最近更新 更多