Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom property editors do not work for request parameters in Spring MVC?
    primarykey
    data
    text
    <p>I'm trying to create a multiaction web controller using Spring annotations. This controller will be responsible for adding and removing user profiles and preparing reference data for the jsp page.</p> <pre><code>@Controller public class ManageProfilesController { @InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(UserAccount.class,"account", new UserAccountPropertyEditor(userManager)); binder.registerCustomEditor(Profile.class, "profile", new ProfilePropertyEditor(profileManager)); logger.info("Editors registered"); } @RequestMapping("remove") public void up( @RequestParam("account") UserAccount account, @RequestParam("profile") Profile profile) { ... } @RequestMapping("") public ModelAndView defaultView(@RequestParam("account") UserAccount account) { logger.info("Default view handling"); ModelAndView mav = new ModelAndView(); logger.info(account.getLogin()); mav.addObject("account", account); mav.addObject("profiles", profileManager.getProfiles()); mav.setViewName(view); return mav; } ... } </code></pre> <p>Here is the part of my webContext.xml file:</p> <pre><code>&lt;context:component-scan base-package="ru.mirea.rea.webapp.controllers" /&gt; &lt;context:annotation-config/&gt; &lt;bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt; &lt;property name="mappings"&gt; &lt;value&gt; ... /home/users/manageProfiles=users.manageProfilesController &lt;/value&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="users.manageProfilesController" class="ru.mirea.rea.webapp.controllers.users.ManageProfilesController"&gt; &lt;property name="view" value="home\users\manageProfiles"/&gt; &lt;/bean&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt; </code></pre> <p>However, when i open the mapped url, i get exception:</p> <pre><code>java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [ru.mirea.rea.model.UserAccount]: no matching editors or conversion strategy found </code></pre> <p>I use spring 2.5.6 and plan to move to the Spring 3.0 in some not very distant future. However, according to this JIRA <a href="https://jira.springsource.org/browse/SPR-4182" rel="nofollow noreferrer">https://jira.springsource.org/browse/SPR-4182</a> it should be possible already in spring 2.5.1.</p> <p>The debug shows that the InitBinder method is correctly called.</p> <p>What am i doing wrong?</p> <p>Update:</p> <pre><code>public class UserAccountPropertyEditor extends PropertyEditorSupport { static Logger logger = Logger.getLogger(UserAccountPropertyEditor.class); public UserAccountPropertyEditor(IUserDAO dbUserManager) { this.dbUserManager = dbUserManager; } private IUserDAO dbUserManager; public String getAsText() { UserAccount obj = (UserAccount) getValue(); if (null==obj) { return ""; } else { return obj.getId().toString(); } } public void setAsText(final String value) { try { Long id = Long.parseLong(value); UserAccount acct = dbUserManager.getUserAccountById(id); if (null!=acct) { super.setValue(acct); } else { logger.error("Binding error. Cannot find userAccount with id ["+value+"]"); throw new IllegalArgumentException("Binding error. Cannot find userAccount with id ["+value+"]"); } } catch (NumberFormatException e) { logger.error("Binding error. Invalid id: " + value); throw new IllegalArgumentException("Binding error. Invalid id: " + value); } } } </code></pre> <p>There are no errors logged from UserAccountPropertyEditor.</p>
    singulars
    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