自定义属性编辑器在Spring MVC中无法使用请求参数? [英] Custom property editors do not work for request parameters in Spring MVC?

查看:120
本文介绍了自定义属性编辑器在Spring MVC中无法使用请求参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

@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文件:

Here is the part of my webContext.xml file:

<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" />

然而,当我打开映射网址时,我收到异常:

However, when i open the mapped url, i get exception:

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

我使用弹簧2.5.6并计划移动到Spring 3.0一些不是很遥远的未来。但是,根据这个JIRA https://jira.springsource.org/browse/SPR-4182 应该可能已经在春季2.5.1。

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 https://jira.springsource.org/browse/SPR-4182 it should be possible already in spring 2.5.1.

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

The debug shows that the InitBinder method is correctly called.

我做错了什么?

更新:

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。

There are no errors logged from UserAccountPropertyEditor.

推荐答案

我不认为你想指定字段参数 WebDataBinder.registerCustomEditor()。这意味着与表单支持对象一起工作,并且您没有使用它。

I don't think you want to be specifying the field argument to WebDataBinder.registerCustomEditor(). This intended to work alongside form-backing objects, and you're not using that.

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

Try the simpler 2-arg method instead, and it should work:

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

这篇关于自定义属性编辑器在Spring MVC中无法使用请求参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆