【发布时间】:2014-05-02 07:54:44
【问题描述】:
我有一个获得一串权限的活页夹,并且必须将其转换为集合。但是当 binder 方法被调用时,它不能将 String 数组转换为集合。
我的控制器:
@Controller
@RequestMapping("/profile")
public class ProfileController {
@Autowired
@Qualifier("mvcUserService")
UserService userService;
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public ModelAndView editForm(Principal principal) {
User user = userService.getUser(principal.getName());
ModelAndView mav = new ModelAndView("user/profile");
return mav.addObject("user", user);
}
@RequestMapping(value = "/edit", method = RequestMethod.PUT)
public ModelAndView edit(@ModelAttribute("user") User user, BindingResult result) {
// some action with User object
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Permission.class, new PropertyEditorSupport() {
@Override
public void setAsText(String name) throws IllegalArgumentException {
Permission permission = userService.getPermission(name);
setValue(permission);
}
});
JSP:
<form:form action="/profile/edit" method="PUT" modelAttribute="user">
<input type="hidden" name="_method" value="PUT"/>
// setting up fields
<form:hidden path="id"/>
<form:hidden path="permissions"/>
</form:form>
权限类:
public class Permission implements Serializable {
private Integer id;
private String name;
还有我的用户类:
public class User implements Serializable {
// some fields
private Collection<Permission> permissions;
我需要实现正确的绑定。有什么建议我该怎么做?
【问题讨论】:
-
它是否调用了绑定?还是抛出错误?
-
它正在调用绑定。
-
那么什么不起作用?从您的示例中,您似乎没有发送 String[] 或任何东西。
-
在我的活页夹中,我以单个字符串“edit_user,delete_user”的形式获得权限。但我的活页夹无法将其转换为 Collection
. -
我的理解是 Spring MVC 已经知道如何处理 collecitons 部分,而不是 bean 部分。你是说它不是那样工作的吗?
标签: java jsp spring-mvc