Results 1 to 5 of 5

Thread: Need help to get values from other beans in jsp

  1. #1
    Join Date
    Oct 2006
    Posts
    23

    Question Need help to get values from other beans in jsp

    Iīm using spring2 and Spring-form tags.
    In a form tag I want to fill a options list with values from another object(AddUserForm object) than the command object for the form.(User object)
    I have created a set of organization objects in my form bean so the user can choose an organization when creating a user.

    Code:
    <bean id="addUserForm" class="AddUserForm">
            <property name="commandName" value="user"/>
            <property name="commandClass" value="User"/>
            <property name="formView" value="userForm"/>
    	<property name="successView" value="userRedirect"/>
    	<property name="validator" ref="userValidator"/>
    	<property name="person" ref="person"/>
            <property name="organizations">
                <set>
                   <bean class="Organization">
                       <property name="id" value="40"/>
                       <property name="name" value="test org"/>
                   </bean>
                </set>
            </property>
        </bean>
    My problem is that I donīt know how to access the addUserForm.organizations set from the jsp-page.

    I have tried to do like this and both with and without the spring:bind tag
    Code:
    <tr>
        <td><fmt:message key="person.label.organizations"/> br/>
        <form:select path="organizationId">
            <form:option value="-" label="Select organization"/>
            <spring:bind path="addUserForm.*">
                <form:options items="${addUserForm.organizations}" itemValue="id" itemLabel="name" />
            </spring:bind>
        </form:select></td>
    </tr>
    what must I do to get access to the addUserForm bean from the JSP page?
    The main object for the page is User and thatīs the object which get the selected value.

    /henrik
    Last edited by 2nice; Nov 6th, 2006 at 09:39 AM. Reason: corrected spring:bind value="" to path=""

  2. #2
    Join Date
    Oct 2004
    Location
    Stockholm
    Posts
    79

    Default

    Ovveride the method referenceData() to return a map containing the "organizations" array.
    You will then be able to acces the organizations like this
    <form:options items="${organizations}" .....


    See

    http://www.springframework.org/docs/...ontroller.html
    Roberto Cosenza
    --
    Skicka stora filer enkelt med http://www.filecentral.se

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You named your form user and in your jsp page you are trying to locate a command with the nameaddUserForm which is obviously not there.

    Next you are setting the organizations on your Controller and you cannot access your controller from your jsp page. Everything you want to access has to be either in the formBackingObject or the referenceData. As this is only referenced data (needed to build a list) I would just put in the reference data.

    override the referenceData method and place the values in a map.

    Code:
    protected Map referenceData(HttpServletRequest request) throws Exception {
        Map referenceData = new HashMap();
        referenceData.put("organizations", this.organizations);
        return referenceData;		
    }
    In your code change your tag part to

    Code:
    <form:select path="organizationId">
        <form:option value="-" label="Select organization"/>
        <form:options items="${organizations}" itemValue="id" itemLabel="name" />
    </form:select>
    and you should be good to go.

    Read the chapter about the web-mvc and, forms library and take a look at the samples included with Spring, they provide tons of information.
    Last edited by Marten Deinum; Nov 6th, 2006 at 10:17 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Oct 2006
    Posts
    23

    Default

    robcos, thanks for your reply!
    I did as you say and I got it to work.
    I override the method referenceData in the AddUserForm class:
    Code:
        
    protected java.util.Map referenceData(javax.servlet.http.HttpServletRequest httpServletRequest) throws java.lang.Exception
        {
            Map myMap = new HashMap();
            myMap.put("organizations", this.getOrganizations() ); 
            return myMap; 
        }
    /henrik

  5. #5
    Join Date
    Oct 2006
    Posts
    23

    Default

    Thanks mdeinum for your reply.
    As you can se from my former post I could solve my problem by overriding the method referenceData. Now it works very well.
    The class User shall not have the variable organization because itīs just a list of values and only one of them will be put in the User object.

    /henrik

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •