ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

自定义属性编辑器不适用于Spring MVC中的请求参数吗?

2019-12-10 02:19:42  阅读:278  来源: 互联网

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


我正在尝试使用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,应该已经可以在春季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记录的错误.

解决方法:

我认为您不想为WebDataBinder.registerCustomEditor()指定字段参数.它旨在与支持表单的对象一起使用,并且您没有使用它.

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

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

标签:data-binding,spring-annotations,spring,spring-mvc
来源: https://codeday.me/bug/20191210/2098480.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有