Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring form submit can not bind to subclass
    text
    copied!<p>I'm developing web application with spring. I have two bean classes. This is the super class.</p> <pre><code>public class SuperClass{ protected String name; public SuperClass(){ } public SuperClass(String name){ this.name = name; } // here the getter and the setter } </code></pre> <p>This is the subclass</p> <pre><code>public class SubClass extends SuperClass{ protected String address; public SubClass(){ } public SubClass(String name, String address){ super(name); this.address = address; } // here the getters and the setters } </code></pre> <p>Here is my controller class.</p> <pre><code>@Controller @RequestMapping("/editSub") public class EditController { @RequestMapping(method = RequestMethod.GET) public String showEdit(ModelMap model){ model.addAttribute("sub", new SubClass("some name", "some address")); return "edit"; } @RequestMapping(method = RequestMethod.POST) public String edited(@ModelAttribute("sub") SubClass sub, ModelMap model) { //do somthing here model.addAttribute("sub", sub); return "somepage"; } } </code></pre> <p>and in my edit.jsp,</p> <pre><code>&lt;form:form method="POST" modelAttribute="sub" action="editSub"&gt; &lt;form:input path="name"/&gt; &lt;form:input path="address"/&gt; &lt;input type="submit" value="Edit"/&gt; &lt;/form:form&gt; </code></pre> <p>Here is my problem. When I try this, it gives me 400 error. But when I change the "edited" method in controller class as below, it works fine.</p> <pre><code>@RequestMapping(method = RequestMethod.POST) public String edited(@ModelAttribute("sub") SuperClass sub, ModelMap model) { //do somthing here model.addAttribute("sub", sub); return "somepage"; } </code></pre> <p>But as obvious, then there is no any address field in the object. So it only works with the super class and gives me 400 error when I bind the form to a subclass. I can't understand why? Also the object "sub" can not be casted into SubClass inside "edited" method.</p> <p>Please help me through. I searched about this and nothing useful found. I have read this thread also. <a href="http://forum.springsource.org/showthread.php?94650-Problem-binding-subclasses" rel="nofollow">http://forum.springsource.org/showthread.php?94650-Problem-binding-subclasses</a>. But it doesn't solve my problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload