我有一个UI一个JSF页面:简单显示字符串和一些控制列表将字符串添加到列表中继标记。当添加一个字符串,我使用Ajax来更新中继标记,并有新的字符串被立即显示无刷新页面。下面是我的页面的样子:
I've got a JSF page with a ui:repeater tag that simply displays a list of strings and some controls to add a string to a list. When adding a string I use ajax to update the repeater tag and have the new string be shown immediately without the page refresh. Here's how my page looks like:
<h:body>
<h:form>
<p:inputText id="name" value="#{testController.newString}"/>
<p:commandButton value="Add" actionListener="#{testController.addString}" update="strings" />
</h:form>
<h:panelGroup id="strings">
<ui:repeat var="str" value="#{stringModel.strings}" varStatus="stringData">
<div>
<h:outputText value="#{str}" />
<h:inputText value="#{str}" />
</div>
</ui:repeat>
</h:panelGroup>
</h:body>
一切正常,除了inputText组件。后UI-中继器使用Ajax更新仍然显示从previous字符串文本。例如,假定最初我有2个字符串,val1中和val2中的列表。我进入一个名为VAL3新的字符串,并提交表单。名单正确更新在服务器端和中继器的更新,它现在有3个要素。然而,尽管H:的outputText在新添加的元素将正确显示VAL3,所述的inputText将显示用val2的作为值。所以,我最终的东西看起来像这样:
Everything works except the inputText component. After ui-repeater is updated with Ajax is still displays the text from the previous string. For example, assume that initially i have a list with 2 strings, "val1" and "val2". I enter a new string called "val3" and submit the form. List is updated correctly on the server side and the repeater is updated, it now has 3 elements. However, while the h:outputText in the newly added element will correctly show "val3", the inputText will be displayed with "val2" as a value. So i end up with something looking like this:
output tag input tag
val1 val1
val2 val2
val3 val2 (???)
背bean是很简单的: 视图范围的模型bean
The backing beans are very simple: A view scoped model bean
@Component
@Scope("view")
public class StringModel {
private List<String> strings = Lists.newArrayList("Value 1");
public List<String> getStrings() {
return strings;
}
public void setStrings(List<String> strings) {
this.strings = strings;
}
}
和一个请求范围的controller bean:
And a request scoped controller bean:
@Component
@Scope("request")
public class TestController {
private String newString;
@Autowired private StringModel model;
public void addString() {
model.getStrings().add(newString);
}
public String getNewString() {
return newString;
}
public void setNewString(String newString) {
this.newString = newString;
}
}
我做了一些测试,这实际上以同样的方式对任何输入组件,是因为与textInput,文本区域等任何帮助将是非常美联社preciated。
I did some testing and this actually works the same way for any input component, be that textInput, textArea, etc. Any help would be highly appreciated.
我不能详细告诉正是为什么它会显示更新后的错误值(这将是该的内部循环索引&LT; UI:重复&GT;
坏了&mdash;尝试新Mojarra版),而只是引用从 varStatus通过索引字符串项
的作品。它也将立即修复暂时无法当你把一个形式这份名单提交编辑字符串值,未来的问题,因为字符串
类是不可变的不有一个二传手。
I can't tell in detail exactly why it displays the wrong value after update (it'll be that the internal loop index of <ui:repeat>
is broken — try a newer Mojarra version), but just referencing the string item by index from varStatus
works. It'll also immediately fix the future problem of being unable to submit the edited string value when you put this list in a form, because the String
class is immutable and doesn't have a setter.
<ui:repeat value="#{stringModel.strings}" var="str" varStatus="loop">
<div>
<h:outputText value="#{str}" />
<h:inputText value="#{stringModel.strings[loop.index]}" />
</div>
</ui:repeat>