【问题标题】:Custom property editors do not work for request parameters in Spring MVC?自定义属性编辑器不适用于 Spring MVC 中的请求参数?
【发布时间】:2010-06-08 15:26:29
【问题描述】:

我正在尝试使用 Spring 注释创建一个多动作 Web 控制器。该控制器将负责添加和删除用户配置文件并为 jsp 页面准备参考数据。

@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;
    }
    ...
}

这是我的 webContext.xml 文件的一部分:

<context:component-scan base-package="ru.mirea.rea.webapp.controllers" />
<context:annotation-config/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
            <value>
               ...
              /home/users/manageProfiles=users.manageProfilesController
            </value>
    </property>
</bean>

<bean id="users.manageProfilesController" class="ru.mirea.rea.webapp.controllers.users.ManageProfilesController">
    <property name="view" value="home\users\manageProfiles"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

但是,当我打开映射的 url 时,我得到了异常:

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

我使用 Spring 2.5.6,并计划在不远的将来迁移到 Spring 3.0。但是,根据这个 JIRA https://jira.springsource.org/browse/SPR-4182,它应该已经可以在 spring 2.5.1 中实现。

调试显示InitBinder方法被正确调用。

我做错了什么?

更新:

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);
        }
    }
}

没有从 UserAccountPropertyEditor 记录的错误。

【问题讨论】:

    标签: spring data-binding spring-mvc spring-annotations


    【解决方案1】:

    我认为您不想将 field 参数指定给 WebDataBinder.registerCustomEditor()。这旨在与表单支持对象一起工作,而您没有使用它。

    改用更简单的 2-arg 方法,它应该可以工作:

    binder.registerCustomEditor(UserAccount.class, new UserAccountPropertyEditor(userManager)); 
    binder.registerCustomEditor(Profile.class, new ProfilePropertyEditor(profileManager));
    

    【讨论】:

    • 它似乎有效!明天我会再做一些测试,当我开始工作的时候。如果一切都好,我会接受答案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 2013-02-08
    相关资源
    最近更新 更多