6

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.

maple_shaft
  • 10,435
  • 6
  • 46
  • 74
algone
  • 93
  • 1
  • 2
  • 9
  • As you can see I was following this example http://stackoverflow.com/questions/7010256/how-to-dynamically-build-a-back-bean-edit-form – algone Jan 04 '12 at 11:44
  • Try adding a FlowListener event on your managed bean and verify there if the values in your map are being updated on the server side: `flowListener="#{managedBean.onFlowListener}`. If this is not occurring then verify that you are not receiving any exceptions or validation errors during the Validation phase of the postback. – maple_shaft Jan 04 '12 at 12:35
  • @maple_shaft I already have that in my view. – algone Jan 04 '12 at 13:49
  • the problem is how do I update the values map with what the user puts into the inputtext fields. this (#{gdsiGeodataBean.actionCommand.values['reqs.name']) only allows me to retrieve the values in the map given the specified key( reqs.name). How do bind the inputs to the values in the backing bean map. – algone Jan 04 '12 at 13:57

1 Answers1

13

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?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Wouldn't `actionCommand.values` require a getter and setter to truly be a bound property? The OP didn't show this in his code, but I am asking because I genuinely don't know, I have never used the `c:forEach` component before. – maple_shaft Jan 06 '12 at 12:56
  • 2
    @maple_shaft: only a getter `getValues()` is required. A setter is unnecessary on maps/lists. JSF EL will set the value on the obtained map/list by the in EL given key/index. Like so: `gdsiGeodataBean.getActionCommand().getValues().put(reqs.getName(), newValue)`. This is regardless of whether it's inside ``, ``, etc. – BalusC Jan 06 '12 at 12:59
  • @BalusC, Thanks a heap! its works. Sorry I still dont have enough rep to vote your answer up, but thanks alot. – algone Jan 06 '12 at 13:50