如何添加< h:inputtext>动态地在JSF中? [英] How to add <h:inputtext> dynamically in JSF?

查看:42
本文介绍了如何添加< h:inputtext>动态地在JSF中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的应用程序添加更多动态信息,例如:我的应用程序要求输入城市并提供一个城市,但是用户想要添加更多城市,我需要更动态地提供他,那么如何在JSF中做到这一点?

I my application I want to add more dynamically for ex: my application asks to input city and provides one but is the user wants to add more cities, I need to provide him more dynamically, so how to do that in JSF?

推荐答案

使用表(h:dataTable).

总体思路:您有一个字符串列表(每个字符串代表城市名称).开始时列表中只有1个项目-1个空字符串.

The general idea: you have a list of Strings (each represents city name). There is only 1 item in your list at start - 1 empty string.

如果您要添加一个城市,请执行一些操作(例如,按添加更多城市"按钮),然后此操作将一个空字符串添加到列表中,并且表应重新呈现(通过Ajax或整个)页面重新加载).

If you want to add one more city, you do some action (press "Add more cities" button for example), and this action put one more empty string to the list and your table should be rerendered (via ajax or whole page reloading).

此后,您将在页面上获得2个输入字段,每个输入字段都绑定到其自己的字符串值.然后用户输入城市名称,按处理"按钮,它会调用一些操作,您可以在其中处理已包含2个非空字符串的列表.

After that you'll get 2 input fields on your page each binded to its own string value. Then user inputs city names, presses 'Process' button and it calls some action in which you can process the list which will already contain 2 non-empty strings.

<h:dataTable value="#{dataTableBean.cities}" var="city">
    <h:column>
    <f:facet name="header" >
        <h:outputText value="City name"/>
    </f:facet>    
    <h:inputText value="#{city}"/>
    </h:column>
</h:dataTable>

<h:commandButton value="Add one more city" action="#{dataTableBean.enlargeList}"/>
<h:commandButton value="Submit" action="#{dataTableBean.processList}"/>

在bean中:

private List<String> cities = new LinkedList<String>();

//getter and setter
...

public String enlargeList () {
    cities.add ("");
    return "refreshTable"; //the easiest way - just reload the page
}

这篇关于如何添加&lt; h:inputtext&gt;动态地在JSF中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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