如何使用h:inputText +托管bean在JSF中保存列表/地图/集合 [英] How to save a list/map/set in JSF with h:inputText + managed bean

查看:115
本文介绍了如何使用h:inputText +托管bean在JSF中保存列表/地图/集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现的目标与以下链接中发布的目标非常相似.

What I'm trying to achieve is very similar to the one posted in the following link.

如何保存数组在JSF中使用ui:repeat + h:inputText +托管bean?

我对上面链接中的Arjan Tijms提供的答案特别着迷,但是我想要实现的目标略有不同.请考虑以下代码片段.

I'm particularly fascinated with the answer provided by Arjan Tijms in the link above however what I want to achieve is slightly different. Consider the following code snippets.

豆子

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;

@RequestScoped
@Named
public class MyBean {

    List<String> choices;

    public List<String> getChoices() {
        return choices;
    }

    @PostConstruct
    public void initChoices() {
        choices= new ArrayList<String>();
    }

    public String save() {
        // should save all the choices into some repository
        return "";
    }
}

和方面页面

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"        
    xmlns:ui="http://java.sun.com/jsf/facelets">

    <h:body>

        <h:form>
            <ui:repeat value="#{myBean.choices}" varStatus="status">            
                <h:inputText value="#{myBean.choices[status.index]}" />
            </ui:repeat>
            <h:commandButton value="Save" action="#{myBean.save}" />
        </h:form>
    </h:body>
</html>

问题是,如果我们在列表的开头有一些初始数据,这将起作用.初始列表为空的情况如何?

The thing is, this will work if we have some initial data in the list at the beginning. What about situations where the initial list will be empty?

我正在寻找的理想解决方案是每个选项都有1个h:inputText,然后单击保存"按钮,然后将每个h:inputText中的所有选项添加到选项列表中.我已经搜寻了很多东西,但似乎找不到任何提示.

The ideal solution which I'm looking for is to have 1 h:inputText for each choice and when save button is clicked, all choices in each h:inputText is then added to the choices list. I've searched high and low but can't seem to find any hints on how this can be done.

如果JSF 2确实不支持此功能,我想我将不得不仅用一个h:inputText来使用丑陋的方式,并使用转换器与列表进行相互转换,但我仍然希望这是一个理想的选择解决方案可以找到.

If JSF 2 really doesn't support this, I guess I'll have to use the ugly way with just one h:inputText and use a converter to convert to and from a list but I'm still hoping that an ideal solution can be found.

希望有来自stackoverflow的人可以向我介绍正确的方向.

Hopefully someone from stackoverflow can shed a light in the right direction for me.

推荐答案

只需添加一个添加"按钮即可向列表中添加新的String.

Just add an "add" button which adds a new String to the list.

<ui:repeat value="#{myBean.choices}" varStatus="status">            
    <h:inputText value="#{myBean.choices[status.index]}" />
</ui:repeat>
<h:inputText value="#{myBean.newChoice}" />
<h:commandButton value="Add" action="#{myBean.add}" />
<h:commandButton value="Save" action="#{myBean.save}" />

使用

private String newChoice;

public void add() {
    choices.add(newChoice);
    newChoice = null;
}

// ...

请注意,这仅在将bean放入视图范围内时才有效.将在每个请求上构造一个范围为一个请求的请求,从而每次都重新创建列表.

Note that this only works if bean is put in view scope. A request scoped one would be constructed on every request and hereby recreate the list everytime.

这篇关于如何使用h:inputText +托管bean在JSF中保存列表/地图/集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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