【问题标题】:Richfaces Datatable loses submitted value of inputs on validation failRichfaces Datatable 在验证失败时丢失提交的输入值
【发布时间】:2015-01-04 14:25:51
【问题描述】:

我有两个输入 - 一个在“正常”h:dataTable 中,一个在 rich:dataTable 中它。见以下代码 sn-ps(输入任意值并按下按钮):

托管豆

@ManagedBean
@ViewScoped
public class TestController implements Serializable {

    private static final long serialVersionUID = -484022507596298941L;

    private String[] stringArray1 = {"Element 1", "Element 2"}; // + Getter
    private String[] stringArray2 = {"Element A", "Element B"}; // + Getter
    private Map<String, String> inputValues = new HashMap<String, String>(4); // + Getter

    public TestController() {
        inputValues.put(stringArray1[0], "");
        inputValues.put(stringArray1[1], "");
        inputValues.put(stringArray2[0], "");
        inputValues.put(stringArray2[1], "");
    }

    public void doSomething() {
        System.out.println("Did something");
    }

    public void validate(FacesContext facesContext, UIComponent uiComponent, Object value) {
        throw new ValidatorException(new FacesMessage("This can never be valid."));
    }
}

查看

<h:form>
    <h1>h:dataTable</h1>
    <h:dataTable id="table1" value="#{testController.stringArray1}" var="string" columnClasses="inactive">
        <h:column>
            <h:outputText value="#{string}:"/>
            <h:inputText id="someInput" value="#{testController.inputValues[string]}" validator="#{testController.validate}"/>
            <h:message for="someInput" id="msg" style="color: red;"/>
        </h:column>
    </h:dataTable>

    <h1>rich:dataTable</h1>
    <rich:dataTable id="table2" value="#{testController.stringArray2}" var="string">
        <rich:column>
            <h:outputText value="#{string}:"/>
            <h:inputText id="someInput" value="#{testController.inputValues[string]}" validator="#{testController.validate}"/>
            <h:message for="someInput" id="msg" style="color: red;"/>
        </rich:column>
    </rich:dataTable>

    <h:commandButton id="button" action="#{testController.doSomething}" value="do something"/>
</h:form>

这是已知的 Richfaces 行为还是某种错误?有没有办法让它的行为与普通的 JSF-DataTable 一样?改用 h:dataTable 并不总是一种选择,而且丢失“我只是想更正它”的输入是相当烦人的。

补充: 我刚刚检查了 ui:repeat 和 a4j:repeat 的行为,它是一样的:ui:repeat 保留提交的值,而 a4j:repeat 没有。

更新:重新编写示例代码以排除 cmets 中提到的一些可能的问题(输入字段现在指向不同的值;只有一个表单元素)。

在 Mojarra 2.1.21 上使用 RichFaces 4.3.7 和 JBoss AS 7 以及在 Mojarra 2.2.7 上使用 RichFaces 4.5.0 Alpha3 和 JBoss Wildlfy 进行测试 - 结果相同。

【问题讨论】:

  • 你是这样用的吗?同一页面中有两个表单?
  • 是的,一个用于一般导航,一个用于用户提供的输入。

标签: validation jsf datatable richfaces


【解决方案1】:

我刚刚使用 Richefaces 4.3.7 和 Mojarra 2.2.6 分别尝试了您页面的每个 form,它运行良好!我没有注意到任何异常行为,当验证失败时我没有丢失任何值。这意味着 Richfaces 组件不存在验证问题。

但是,当在一个页面中使用两个表单时,我可以注意到当我提交第一个表单时,第二个表单的 inputText 失去了它的价值,而如果我们提交 form2inputText第一个表单并没有失去它的价值,在我看来,这是因为 JSF 将其 HTML 组件的状态存储在 javax.faces.ViewState 中,并且对于 Richfaces 组件不做同样的事情,使用 Firebug 您可以轻松验证唯一的这两个 POST 请求之间常见的request parameterjavax.faces.ViewState

【讨论】:

  • 在阅读此 SO stackoverflow.com/questions/7371903/… 时,我得到的印象是应该可以同时拥有两个表单。问题可能在于两种形式都在相同的值上运行。
  • @cheffe 是的,关于 BalusC 的回答,使用并行表单没有问题。但是,问题不在于使用相同的值,因为我使用两个不同的值进行测试,但我得到了相同的行为
  • 我一次用一种形式在本地测试了这个,同样的问题。我没有你的 Mojarra 版本,我接下来检查一下。
  • @Louise 你有哪个版本?
  • 据我所知 2.1.21。配备 JBoss AS 7
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 2012-08-03
  • 2011-12-17
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多