WebDataBinder 和 Set.Class 的问题

发布于 2024-11-02 06:44:24 字数 3062 浏览 0 评论 0原文

我在绑定表单中的数据时遇到问题: 我有两个类

@Entity
@Table(name = "ROLES")
public class Role implements GenericDomain {

private Long    id;
private String  code;
private String  name;

private Set<Privilege> privileges = new HashSet<Privilege>(0);

public Role() {}

/* getter and setter*/ 

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name = "ROLES_PRIVILEGES"
    , joinColumns = { @JoinColumn(name = "ROLE_ID") }
    , inverseJoinColumns = { @JoinColumn(name = "PRIVILEGE_ID") }
)

public Set<Privilege> getPrivileges() {
    return this.privileges;
}
public void setPrivileges(Set<Privilege> privileges) {
    this.privileges = privileges;
}
    /*  overide of hascode, equals*/ 
}

并且

@Entity
@Table(name = "PRIVILEGES")
public class Privilege implements GenericDomain {

private Long    id;
private String  code;

private Set<Role> roles = new HashSet<Role>(0);

public Privilege() {}

 /* getter and setter*/

 @ManyToMany(cascade=CascadeType.REFRESH, mappedBy="privileges")
public Set<Role> getRoles() {
    return this.roles;
}
public void setRoles(Set<Role> roles) {
    this.roles = roles;
}
@Override
public String toString(){
    return this.getCode() + this.getComment();
    } 

/*overide equals and hascode*/

在我的控制器中我有:

    @InitBinder
public void initBinder(WebDataBinder binder) {

   binder.registerCustomEditor(Set.class, "privileges", new CustomCollectionEditor(Set.class) {
       @Override 
       protected Object convertElement(Object element) {
            return (element == null)?null:privilegeService.getOne(Integer.parseInt((String)element));
        }
    });
}
    @RequestMapping(value = "edit", method = RequestMethod.POST)  
public String saveOldRole(  @ModelAttribute("role") Role role
                            , BindingResult result
                            , ModelMap model
                        ) {

    validator.validate(role, result);
    if (result.hasErrors()){
        logger.error(result.getAllErrors());
        model.addAllAttributes(result.getModel());
        return "/admin/role/edit";
    }
    logger.info(role.getPrivileges());
    Iterator p = role.getPrivileges().iterator();
    while(p.hasNext()){
        logger.info(p.next().getClass());
    }
    roleService.saveOrUpdate(role);
    model.addAttribute("roles", roleService.getAll());
    sessionStatus.setComplete();
    return "redirect:/admin/role/list.do";
}

我的调试是

role.RoleController:93 - [[MANAGE_USERS], [MANAGE_ROLES]]
role.RoleController:96 - class java.util.LinkedHashSet
role.RoleController:96 - class java.util.LinkedHashSet
22:29:44,915 ERROR tomcat-http--7 property.BasicPropertyAccessor:194 - IllegalArgumentException in class: com.stunaz.domain.Privilege, getter method of property: id

我不明白为什么在96,类类型是java.util.LinkedHashSet,我认为它应该是特权。 我不明白为什么我的 role.getPrivileges() 是一组集合,它应该是一组权限。

当然,在 saveOrUpdate 时出现错误。

i am having trouble with binding my data from a form :
I have two class

@Entity
@Table(name = "ROLES")
public class Role implements GenericDomain {

private Long    id;
private String  code;
private String  name;

private Set<Privilege> privileges = new HashSet<Privilege>(0);

public Role() {}

/* getter and setter*/ 

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name = "ROLES_PRIVILEGES"
    , joinColumns = { @JoinColumn(name = "ROLE_ID") }
    , inverseJoinColumns = { @JoinColumn(name = "PRIVILEGE_ID") }
)

public Set<Privilege> getPrivileges() {
    return this.privileges;
}
public void setPrivileges(Set<Privilege> privileges) {
    this.privileges = privileges;
}
    /*  overide of hascode, equals*/ 
}

And

@Entity
@Table(name = "PRIVILEGES")
public class Privilege implements GenericDomain {

private Long    id;
private String  code;

private Set<Role> roles = new HashSet<Role>(0);

public Privilege() {}

 /* getter and setter*/

 @ManyToMany(cascade=CascadeType.REFRESH, mappedBy="privileges")
public Set<Role> getRoles() {
    return this.roles;
}
public void setRoles(Set<Role> roles) {
    this.roles = roles;
}
@Override
public String toString(){
    return this.getCode() + this.getComment();
    } 

/*overide equals and hascode*/

and in my controller i have :

    @InitBinder
public void initBinder(WebDataBinder binder) {

   binder.registerCustomEditor(Set.class, "privileges", new CustomCollectionEditor(Set.class) {
       @Override 
       protected Object convertElement(Object element) {
            return (element == null)?null:privilegeService.getOne(Integer.parseInt((String)element));
        }
    });
}
    @RequestMapping(value = "edit", method = RequestMethod.POST)  
public String saveOldRole(  @ModelAttribute("role") Role role
                            , BindingResult result
                            , ModelMap model
                        ) {

    validator.validate(role, result);
    if (result.hasErrors()){
        logger.error(result.getAllErrors());
        model.addAllAttributes(result.getModel());
        return "/admin/role/edit";
    }
    logger.info(role.getPrivileges());
    Iterator p = role.getPrivileges().iterator();
    while(p.hasNext()){
        logger.info(p.next().getClass());
    }
    roleService.saveOrUpdate(role);
    model.addAttribute("roles", roleService.getAll());
    sessionStatus.setComplete();
    return "redirect:/admin/role/list.do";
}

and my debug is

role.RoleController:93 - [[MANAGE_USERS], [MANAGE_ROLES]]
role.RoleController:96 - class java.util.LinkedHashSet
role.RoleController:96 - class java.util.LinkedHashSet
22:29:44,915 ERROR tomcat-http--7 property.BasicPropertyAccessor:194 - IllegalArgumentException in class: com.stunaz.domain.Privilege, getter method of property: id

I dont understand why at 96, the class type is java.util.LinkedHashSet, i thought it should be Privileges.
I dont understand why my role.getPrivileges() is a Set of Set, it should be a Set of Privilege.

Of course at saveOrUpdate am getting an error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为守护你 2024-11-09 06:44:24

终于!!!
根本没有错误!
我将 spring jar 从 3.0.5.RELEASE 更新到 3.1.0.M1,瞧:有些东西停止使用 webdatabinder 和 CustomCollectionEditor。

我只是回滚到 3.0.5.RELEASE,一切都很好。

finaly!!!
there were no bug at all!
i updated my spring jar from 3.0.5.RELEASE to 3.1.0.M1, and voila : somthing stopped working with webdatabinder and CustomCollectionEditor.

i just rollback to 3.0.5.RELEASE and everything is fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文