How to set submitted values of <h:inputText> inside <ui:repeat> into map, set or list

谁说胖子不能爱 提交于 2019-11-28 01:40:01
BalusC

With a Set, it is not possible as it doesn't allow referencing items by index or key. It's however possible with a List and a Map by just specifying the list index and map key in the input value.


With a List:

private List<String> list; // +getter (no setter necessary)

@PostConstruct
public void init() { 
    list = createAndFillItSomehow();
}
<ui:repeat value="#{bean.list}" varStatus="loop">
    <h:inputText value="#{bean.list[loop.index]}" />
</ui:repeat>

With a Map (only if your environment supports EL 2.2 or JBoss EL):

private Map<String, String> map; // +getter (no setter necessary)

@PostConstruct
public void init() { 
    map = createAndFillItSomehow();
}
<ui:repeat value="#{bean.map.entrySet().toArray()}" var="entry">
    <h:inputText value="#{bean.map[entry.key]}" />
</ui:repeat>

Noted should be that the canonical approach is to use a List of fullworthy javabeans. Let's assume a Javabean class named Par with properties id and value which maps exactly to a par table in DB with columns id and value:

private List<Par> pars; // +getter (no setter necessary)

@PostConstruct
public void init() { 
    pars = createAndFillItSomehow();
}
<ui:repeat value="#{bean.pars}" var="par">
    <h:inputText value="#{par.value}" />
</ui:repeat>

Either way, it works as good when using <p:inputText>, it's in no way related to PrimeFaces, it's in the context of this question merely a jQuery based JSF UI component library. Just replace h: by p: to turn it on.

I'm not sure, if I understood your requirements correctly. I suppose the following: You need a List of Strings in some backend and an ui:repeat tag to iterate over those strings with input-fields to edit them. Maybe there are some syntax-issues, but my idea should be clear:

public class Backend {
    private List<String> myStrings;

    public MyStringWrapper getMyStringWrapper(int index) {
        return new MyStringWrapper(index);
    }

    public class MyStringWrapper {
        private final int index;
        public MyStringWrapper(int index) { this.index = index; }
        public String getContent() { return myStrings.get(index); }
        public void setContent(String newContent) { myStrings.add(index, newContent); }
    }
}

In the frontend you use as follows:

<ui:repeat var="_index" value="#{backend.getIndexSequence()}">
  <p:inputText value="#{backend.getMyStringWrapper(_index).content}"/>
</ui:repeat>

Of course, you have to provide a getIndexSequence-method which produces a list of ints ranging from 0 to the size of the strings.

Do you mean like this?

<p:inputText id="me" value="#{_par.input}"/>

in BackBean:

public class Par implements Serializable {

private String inputText;
private String bezeichnung;

public Par()
{
}

public void setInput(String input)
{
this.inputText = input;
}

public String getInput()
{
return this.inputText
}

public void setBezeichnung(String bezeichnung)
{
this.bezeichnung = bezeichnung;
}

public String getBezeichnung()
{
return this.bezeichnung
}

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!