Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I bind a inputbox values to a map value in a backing bean property when using a wizard

I am using the Primefaces wizard component. On one tab I am dynamically creating input boxes based on previous tabs input(user type). The inputbox text labels are derived from a list. In my backing bean, I have a map that contains input labels as keys and inputbox inputs as values.

Clicking on next, I would like the map(values) to be updated with the user input (corresponding to the key)

<c:forEach items="#{gdsiGeodataBean.actionCommand.fields}" var="reqs">
  <h:outputLabel for="#{reqs.name}" value="#{reqs.name}:* " />  
  <pou:inputText value="#{gdsiGeodataBean.actionCommand.values['reqs.name']}"  required="true" requiredMessage="Input is required."/> 
</c:forEach>

My backing bean :

private List<RequiredParam> fields; // +getter (no setter required)
private Map<String, String> values; // +getter (no setter required)

public CommandAction(String actionName, String actionParams, String context) {
    this.actionName = actionName;
    this.actionParams = actionParams;
    this.contextName = context;

    //Set up parameters
    getRequiredParams();
    getOptionalParams();
    fields = getFields();
    values = new HashMap<String, String>();
}

Essentially what I would like is for the map values to be updated with user inputs from the textinput boxes.

like image 744
algone Avatar asked Jan 04 '12 11:01

algone


1 Answers

Your approach to bind the input value to a map is not entirely correct.

<pou:inputText value="#{gdsiGeodataBean.actionCommand.values['reqs.name']}"  required="true" requiredMessage="Input is required."/> 

You're specifying a fixed map key instead of a dynamic map key based on the currently iterated #{reqs}. This way all submitted values will end up in one and same fixed map key "reqs.name", whereby each one overrides each other so that you only get the value of the last field in the map.

You need to remove those singlequotes to make it a really dynamic key.

<pou:inputText value="#{gdsiGeodataBean.actionCommand.values[reqs.name]}"  required="true" requiredMessage="Input is required."/> 

Unrelated to the concrete question, even though this approach will work when used as-is in your question, the <c:forEach> will fail in certain circumstances. E.g. when used inside a composite component or an iterating JSF component. Rather use <ui:repeat> instead. See also JSTL in JSF2 Facelets... makes sense?

like image 197
BalusC Avatar answered Nov 09 '22 06:11

BalusC