Note that there are some explanatory texts on larger screens.

plurals
  1. POConverting from String to custom Object for Spring MVC form Data binding?
    text
    copied!<p>I am using Spring MVC's SimpleFormController in conjunction with Spring MVC's form JTL to create a form to edit a Generic object.</p> <p>On my form I have a drop down where the user can specify a server via a drop down.</p> <pre><code>&lt;form:form commandName="generic"&gt; &lt;form:select path="server"&gt; &lt;form:options items="${servers}" itemValue="id" itemLabel="name"/&gt; &lt;/form:select&gt; &lt;/form:form&gt; </code></pre> <p>Servers here is propagated by a database call for all available servers. server is a Server ORM pojo, that is a sub-object of another ORM pojo (Generic) that serves as my form backing object.</p> <p>My goal here is to change Generic's server reference, which is represented on the database level as a Foreign Key to the server table.</p> <p>I am using JPA as my persistence layer and JPA generated entity classes as my ORM pojos.</p> <p><strong>Unfortunately this doesn't seem to be binding properly when my form submits, as it can't translate from String to Server.</strong></p> <pre><code>Field error in object 'generic' on field 'server': rejected value [1]; codes [typeMismatch.generic.server,typeMismatch.server,typeMismatch.com.generic.orm.jpa.Server,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [generic.server,server]; arguments []; default message [server]]; default message [Failed to convert property value of type [java.lang.String] to required type [com.generic.orm.jpa.Server] for property 'server'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [com.generic.orm.jpa.Server] for property 'server': no matching editors or conversion strategy found], generic=com.generic.orm.jpa.generic[id=3]} and static attributes {} </code></pre> <p>I've been looking for an example of how to accomplish this with no luck. I believe I need to overwrite something within the SimpleFormController, like I did in <a href="https://stackoverflow.com/questions/706660/spring-mvc-formselect-tag-multiple-selections-not-binding-correctly">this question</a>, but Spring MVC's documentation is light on details. Can anyone help me out here?</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