How to assign value to an array with bean JSF 2.0 support

I have this new problem. I have the following code in JSF 2.0 with Primefaces 2.2.1:

<h:selectOneMenu id="cmbRole8" value="#{myWorkflow.posToInsert[7]}" > <f:selectItem itemLabel="Select a position..." itemValue="-1" /> <f:selectItems value="#{appPositions.allPositions}" var="ap" itemLabel="#{ap.roDescription}" itemValue="#{ap.roPositionid}" /> </h:selectOneMenu> 

This is repeated 7 more times for a total of 8 selectOneMenu controls, where I need to get user input. Obviously, this does not work, because the recipients and setters do not know which index to use when assigning values. How can I achieve this correctly?

+4
source share
1 answer

Obviously, this does not work, because getters and setters do not know which index to use when assigning values.

This is not true. Your problem is probably caused by the fact that you did not initialize the array yourself. JSF / EL will not do this for you (as, for example, with any other "nested object"). It will set the array value only at the given index.

eg.

 private int[] positions; @PostConstruct public void init() { positions = new int[3]; // You need to preinitialize it yourself! } public int[] getPositions() { return positions; } // No setter needed! 

with

 <h:selectOneMenu value="#{bean.positions[0]}" ... /> <h:selectOneMenu value="#{bean.positions[1]}" ... /> <h:selectOneMenu value="#{bean.positions[2]}" ... /> 
+4
source

All Articles