Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just as a supplement to Mark's answer, here is what I ended up doing in my controller.</p> <pre><code>@Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { binder.registerCustomEditor(Server.class, "serverId", new PropertyEditorSupport() { @Override public void setAsText(String text) { Server type = (Server) em.createNamedQuery("Server.findById") .setParameter("id", Short.parseShort(text)).getSingleResult(); setValue(type); } }); } </code></pre> <p>You can also do this using Spring injection, as opposed to anonymous classes. This outlined by the <a href="http://static.springframework.org/spring/docs/2.5.x/reference/validation.html#beans-beans-conversion-customeditor-registration" rel="noreferrer">link</a> in Mark's answer.</p> <p>You you may also be able to extend ClassEditor (see below) instead of PropertyEditorSupport. The Javadoc states;</p> <blockquote> <p>Property editor for java.lang.Class, to enable the direct population of a Class property without recourse to having to use a String class name property as bridge. </p> </blockquote> <p>Don't know if I fully understand the benefit of this, but something to keep in mind.</p> <h2>Useful Javadocs</h2> <ul> <li><a href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/PropertyEditorSupport.html" rel="noreferrer">Property Editor Support</a></li> <li><a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/validation/DataBinder.html" rel="noreferrer">Spring Data Binder</a> </li> <li><a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/beans/propertyeditors/package-summary.html" rel="noreferrer">Spring Property Editors</a></li> <li><a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/beans/propertyeditors/ClassEditor.html" rel="noreferrer">Spring Class Editor</a></li> </ul>
 

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