Results 1 to 5 of 5

Thread: Spring MVC + form with object attribute in the command

  1. #1
    Join Date
    Jul 2011
    Posts
    2

    Question Spring MVC + form with object attribute in the command

    Hi all,

    I'm new in Spring so sorry if I don't use the right vocabulary.
    I've some trouble submitting a form with spring MVC when the object representing the form have a complex attribute.

    Easier to explain with some code, I've something like this :
    Code:
    public class Person {
    	
    private String name;
    private int age;
    private House house;
    //+getters and setters
    }
    House is a class that I defined with several attributes. I have a list of houses pre-defined, and I would like that the user choose among them in the form.

    So here is a part of my controller :

    Code:
    	@RenderMapping
    	public ModelAndView showForm(RenderRequest request, RenderResponse response){
    		
    		ModelAndView mav= new ModelAndView("form");
    		
    			ArrayList<House> houses = service.getHouses();
    			mav.addObject("houses", houses);
    		
    //Command object
    			Person person = new Person();
    			mav.addObject("person", person);
    
    			return mav;
    }
    Then I arrive to my .jsp :

    Code:
    <form:form name="form" method="post" action="${url}" commandName="person">
    
    <p>
    	<form:select id="choosehouse" path="house">
    		<form:option value="" selected="selected">Select a house</form:option>		
    		<form:options items="${houses}" itemValue="id" itemLabel="name"/>
    	</form:select>
    </p>
     
    <p>Name :<form:input path="name"/></p>
    <p>Age :<form:input path="age"/></p>
    
    <input type="submit"/>
     
     </form:form>
    Then I have my handler to process the form :
    Code:
    @ActionMapping
    public void onSubmit(@ModelAttribute Person person)
    the problem is that if I put this, it does not work because the form will only throw the id of the house and not the corresponding house entity so I raise an exception.
    So I need a way either to throw the entire house entity instead of just the id (not sure that it is possible) or to complete my person entity before it goes to my onSubmit.

    Thanks for your replies!

  2. #2
    Join Date
    May 2011
    Posts
    4

    Default

    I'm curious about this exact same scenario. What is the proper way to have Spring bind the selected List object reference so it is available during handling of the POST request?

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

    Default

    And that is why there are Converters (or in older versions of Spring PropertyEditors) which convert a String representation (in this case the id) to an actual object.
    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
    Jul 2011
    Posts
    2

    Default

    Quote Originally Posted by Marten Deinum View Post
    And that is why there are Converters (or in older versions of Spring PropertyEditors) which convert a String representation (in this case the id) to an actual object.
    Thanks for your reply.
    Have you some examples (of using Converters or PropertyEditors) because I've already found the PropertyEditor but I couldn't make it work.

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Create a property editor that converts the id into your desired object (probably retrieve it from the database). Configure the editor (or converter) as explained in the reference guide. You simply add/configure it in the DataBinder.

    I suggest a read of the web chapter and taking a look at the sample application(s)...
    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

Tags for this Thread

Posting Permissions

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