Note that there are some explanatory texts on larger screens.

plurals
  1. POData binding of an abstract class in spring-mvc
    primarykey
    data
    text
    <p>I've went thru Spring documentation and source code and still haven't found answer to my question.</p> <p>I have these classes in my domain model and want to use them as backing form objects in spring-mvc.</p> <pre><code> public abstract class Credentials { private Long id; .... } public class UserPasswordCredentials extends Credentials { private String username; private String password; .... } public class UserAccount { private Long id; private String name; private Credentials credentials; .... } </code></pre> <p>My controller:</p> <pre><code> @Controller public class UserAccountController { @RequestMapping(value = "/saveAccount", method = RequestMethod.POST) public @ResponseBody Long saveAccount(@Valid UserAccount account) { //persist in DB return account.id; } @RequestMapping(value = "/listAccounts", method = RequestMethod.GET) public String listAccounts() { //get all accounts from DB return "views/list_accounts"; } .... } </code></pre> <p>On UI I have dynamic form for the different credential types. My POST request usually looks like:</p> <pre><code> name name credentials_type user_name credentials.password password credentials.username username </code></pre> <p>Following exception is thrown if I try to submit request to the server :</p> <pre><code> org.springframework.beans.NullValueInNestedPathException: Invalid property 'credentials' of bean class [*.*.domain.UserAccount]: Could not instantiate property type [*.*.domain.Credentials] to auto-grow nested property path: java.lang.InstantiationException org.springframework.beans.BeanWrapperImpl.newValue(BeanWrapperImpl.java:628) </code></pre> <p>My initial thought was to use <code>@ModelAttribute</code></p> <pre><code> @ModelAttribute public PublisherAccount prepareUserAccountBean(@RequestParam("credentials_type") String credentialsType){ UserAccount userAccount = new PublisherAccount(); Class credClass = //figure out correct credentials class; userAccount.setCredentials(BeanUtils.instantiate(credClass)); return userAccount; } </code></pre> <p>Problem with this approach is that <code>prepareUserAccountBean</code> method get called before any other methods (like <code>listAccounts</code>) as well which is not appropriate.</p> <p>One robust solution is to move out both <code>prepareUserAccountBean</code> and <code>saveUserAccount</code> to the separate Controller. It doesn't sound right : I want all user-related operations to reside in the same controller class.</p> <p>Any simple solution? Can I utilize somehow DataBinder, PropertyEditor or WebArgumentResolver?</p> <p>Thank you!!!!!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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