Question

I'm using Spring MVC and jQuery for processing in the JSP the objects sent by the controller.

My controller is sending to the JSP multiple objects using:

modelAndView.addObject("roleForm" + rol.getRolecode(), form);

Where form is an object of the following class:

public class RoleForm
{
    private String roleCode;
    private LinkedHashSet<String> listEnabled;
... getters and setters ...
}

So, I'm sending objects with the key roleFormADMIN, roleFormUSER, etc. The quantity and names of these objects are not known, as they are retrieved from database.

In my JSP I'm accessing, for each rol, its correspondent RoleForm:

<c:forEach items="${rolesList}" var="role">
    <div class="role" id="${role}">
        <label class="adminSectionTitle">
            ${role}
        </label>

        <form:form method="post" id="roleForm${role}" modelAttribute="roleForm${role}" action="roles.html">
            <fieldset>
                <form:hidden path="roleCode"/>

                <div class="pickList">
                    <fieldset class="pickItems">
                        <legend >Enabled</legend>

                        <form:select  path="listFunctEnabled" id="combo1" size="5" multiple="true">     
                            <form:options items="${listFunctEnabled}" />
                        </form:select>
                    </fieldset>

                </div>
            </fieldset>
            <div class="formButtons">                       
                <input type="submit" value="<fmt:message key="button.save"/>" />
                <input type="button" name="reset" onclick="location.href='<c:url value="roles.html"/>'" value="<fmt:message key="button.clear"/>" />                                                
            </div>
        </form:form>        
    </div>
</c:forEach>            

The property roleCode of each RoleForm is correctly retrieved, but it can't access the property listFunctEnabled of the form (it's empty).

If I directly write the name of the form (for instance, roleFormADMIN.listFunctEnabled), then it writes every item of the list in the form:option.

If I use roleForm${role}.listFunctEnabled, then it considers this as an String and says that String has not any property called listFunctEnabled.

I need one form for each role as I need one button for each role in order to update its list of functenabled.

How could I achieve this?

Was it helpful?

Solution 2

I've finally solved the issue employing a HashMap, rather than a set. So, in my controller, I'm filling the map with the List of listFunctEnabled for every role (key of the hashmap) For instance: map[ROLE_ADMIN] = {Funct1, Funct2, Funct3}

The RoleForm remains the same, so the selected items are stored in listFunctEnabled property.

Now, in the JSP, I access that with the following:

<form:form method="post" id="roleForm${role}" modelAttribute="roleForm${role}" action="roles.html">
    <fieldset>
        <form:hidden path="roleCode"/>

        <div class="pickList">
            <fieldset class="pickItems">
                <legend >Enabled</legend>

                <form:select path="listFunctEnabled" id="combo1" size="5" multiple="true">      
                    <form:options items="${map[role]}" />
                </form:select>

            </fieldset>
        </div>
    </fieldset>
</form:form>

And no problems with that solution. I hope could be useful for someone else.

OTHER TIPS

Did you try setting the modelAttribute value to another variable and then accessing the listFunctEnabled list? This is what I am thinking.

<c:set var="roleFormName" value="roleForm${role}"/>

then use this in the select tag

<form:select  path="listFunctEnabled" id="combo1" size="5" multiple="true">     
     <form:options items="${roleFormName.listFunctEnabled}" />
</form:select>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top