我正在尝试使用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中使用它了.
我究竟做错了什么?
更新:
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));
原文地址:https://www.jb51.cc/spring/531661.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。