Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC + form with object attribute in the command Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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?

    Comment


    • #3
      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.

      Comment


      • #4
        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.

        Comment


        • #5
          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)...

          Comment

          Working...
          X