使用< ui:repeat>< h:inputText>在List< String>上不更新模型值 [英] Using <ui:repeat><h:inputText> on a List<String> doesn't update model values

查看:90
本文介绍了使用< ui:repeat>< h:inputText>在List< String>上不更新模型值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是场景(简化):

有一个具有成员和适当的getter/setter的bean(称为mrBean):

There is a bean (call it mrBean) with a member and the appropriate getters/setters:

private List<String> rootContext;

public void addContextItem() {
    rootContext.add("");
}

JSF代码:

<h:form id="a_form">
            <ui:repeat value="#{mrBean.stringList}" var="stringItem">
                    <h:inputText value="#{stringItem}" />
            </ui:repeat>
            <h:commandButton value="Add" action="#{mrBean.addContextItem}">
                <f:ajax render="@form" execute="@form"></f:ajax>
            </h:commandButton>
</h:form>

问题是,当单击添加"按钮时,不会执行在<h:inputText/>中输入的表示stringList中的字符串的值.

The problem is, when clicking the "Add" button, the values that were entered in the <h:inputText/> that represent the Strings in the stringList aren't executed.

实际上,从不调用mrBean.stringList设置器(setStringList(List<String> stringList)).

Actually, the mrBean.stringList setter (setStringList(List<String> stringList)) is never called.

知道为什么吗?

一些信息- 我在Tomcat 6上使用MyFaces JSF 2.0.

Some info - I'm using MyFaces JSF 2.0 on Tomcat 6.

推荐答案

String类是不可变的,没有值的设置方法.吸气剂基本上是Object#toString()方法.

The String class is immutable and doesn't have a setter for the value. The getter is basically the Object#toString() method.

您需要直接在List上获取/设置值.您可以通过<ui:repeat varStatus>可用的列表索引来做到这一点.

You need to get/set the value directly on the List instead. You can do that by the list index which is available by <ui:repeat varStatus>.

<ui:repeat value="#{mrBean.stringList}" varStatus="loop">
    <h:inputText value="#{mrBean.stringList[loop.index]}" />
</ui:repeat>

您也不需要stringList的二传手. EL将通过List#get(index)获取项目,并通过List#add(index,item)设置项目.

You don't need a setter for the stringList either. EL will get the item by List#get(index) and set the item by List#add(index,item).

这篇关于使用&lt; ui:repeat&gt;&lt; h:inputText&gt;在List&lt; String&gt;上不更新模型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆