ui:repeat中的JSF 2.0 h:inputText [英] JSF 2.0 h:inputText inside ui:repeat

查看:93
本文介绍了ui:repeat中的JSF 2.0 h:inputText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改以下示例,在提交commandButton后,inputText中的值更改后不消失?我了解为什么会发生这种情况,但我不知道如何解决它.

How I should change following example that after changes values in inputText not disappears after commandButton was submitted? I'm understanding why it happens, but I don't know how to fix it.

<h:form>
    <h:selectOneMenu valueChangeListener="#{foo.selectChanges}" onchange="submit()" >
        <f:selectItem itemValue="5" itemLabel="Five"/>
        <f:selectItem itemValue="10" itemLabel="Ten"/>
    </h:selectOneMenu>

    <ui:repeat value="#{foo.repeatItems}" var="item">
        <div>
            <h:inputText value="#{item.value}"/>
        </div>
    </ui:repeat>

    <h:commandButton value="test" action="#{foo.submit}">
    </h:commandButton>
</h:form>

还有豆子:

public class FooBean {

    private RepeatItem[] repeatItems;

    private String value = "5";

    public String getValue() {
        return value;
    }

    public void setValue(final String value) {
        this.value = value;
    }

    public void submit() {

    }

    public void selectChanges(ValueChangeEvent e) {
        value = (String) e.getNewValue();
    }

    public RepeatItem[] getRepeatItems() {
        repeatItems = new RepeatItem[Integer.parseInt(value)];

        for (int i = 0; i < Integer.parseInt(value); i++) {
            repeatItems[i] = new RepeatItem();
        }

        return repeatItems;
    }

    public void setRepeatItems(final RepeatItem[] repeatItems) {
        this.repeatItems = repeatItems;
    }

    public static class RepeatItem {

        private String value;

        public String getValue() {
            return value;
        }

        public void setValue(final String value) {
            this.value = value;
        }
    }
}

推荐答案

您希望将Bean放入view范围,以使其在对同一视图的后续请求中保持活动状态.您还想预填充valueChangeListener方法而不是getter方法,因为在JSF的生命周期中多次调用了getter方法,实际上该方法只能返回.您还希望在此处使用f:ajax来通过部分提交而不是通过Javascript的submit()提交整个页面来改善用户体验.这是一个启动示例:

You'd like to put the bean in the view scope to keep it alive in subsequent requests to the same view. You'd also like to prefill in the valueChangeListener method instead of the getter method since a getter method is called multiple times in JSF's lifecycle and is actually supposed to only return the data. You would also like to use f:ajax here to improve user experience by partial submits instead of submitting the whole page by Javascript's submit(). Here's a kickoff example:

XHTML:

<h:form>
    <h:selectOneMenu valueChangeListener="#{bean.prefill}" converter="javax.faces.Integer">
        <f:selectItem itemValue="0" itemLabel="Please select" />
        <f:selectItem itemValue="5" itemLabel="Five"/>
        <f:selectItem itemValue="10" itemLabel="Ten"/>
        <f:ajax event="change" render="items" />
    </h:selectOneMenu>
    <h:panelGroup id="items">
        <ui:repeat value="#{bean.items}" var="item">
            <div><h:inputText value="#{item.value}"/></div>
        </ui:repeat>
    </h:panelGroup>
    <h:commandButton value="submit" action="#{bean.submit}" />
    <h:messages />
</h:form>

Bean:

@ManagedBean
@ViewScoped
public class Bean {

    private List<Item> items;

    public void prefill(ValueChangeEvent event) {
        Integer count = (Integer) event.getNewValue();
        items = new ArrayList<Item>();
        while (count-- > 0) items.add(new Item());
    }

    public void submit() {
        System.out.println(items);
    }

    public List<Item> getItems() {
        return items;
    }    

}

项目:

public class Item {

    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

另请参见:

  • 为什么JSF多次调用getter?
  • @ViewScoped的好处和陷阱
  • See also:

    • Why JSF calls getters multiple times?
    • The benefits and pitfalls of @ViewScoped
    • 这篇关于ui:repeat中的JSF 2.0 h:inputText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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