Spring-MVC 3.0 Crud with Checkboxes问题 [英] Spring-mvc 3.0 crud with checkboxes issue

查看:75
本文介绍了Spring-MVC 3.0 Crud with Checkboxes问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个简单的用户任务.

I am doing an simple user crud.

我的ApplicationUser具有以下属性:

private Long id;
private String password;
private String username;
private Collection<Authority> myAuthorities;
private boolean isAccountNonExpired;
private boolean isAccountNonLocked;
private boolean isCredentialsNonExpired;
private boolean isEnabled;

权威类具有:

private Long id;
private String authority;
private String name;

在我的jsp中,我的视图具有以下形式:

In my jsp, my view has the following form:

<form:form modelAttribute="applicationUser"
    action="add" method="post">
    <fieldset>

    <form:hidden path="id" />

    <legend><fmt:message key="user.form.legend" /></legend>
    <p><form:label for="username" path="username" cssErrorClass="error"><fmt:message key="user.form.username" /></form:label><br />
    <form:input path="username" /> <form:errors path="username" /></p>

    <p><form:label for="password" path="password"
        cssErrorClass="error"><fmt:message key="user.form.password" /></form:label><br />
    <form:password path="password" /> <form:errors path="password" /></p>

    <p><form:label for="password" path="password"
        cssErrorClass="error"><fmt:message key="user.form.password2" /></form:label><br />
    <form:password path="password" /> <form:errors path="password" /></p>

    <p><form:label for="myAuthorities" path="myAuthorities"
        cssErrorClass="error"><fmt:message key="user.form.autorities" /></form:label><br />
    <form:checkboxes items="${allAuthorities}" path="myAuthorities" itemLabel="name"/><form:errors path="myAuthorities" /></p>

    <p><input type="submit"/></p>
    </fieldset>
</form:form>

jsp从此获取allAuthorities:

@ModelAttribute("allAuthorities")
public List<Authority> populateAuthorities() {
  return authorityService.findAll();
}

填写表格后,我得到:

无法转换的属性值 将java.lang.String键入所需的类型 属性的java.util.Collection myAuthorities;嵌套的例外是 java.lang.IllegalStateException: 无法转换类型的值 [java.lang.String]为必需的类型 [com.tda.model.applicationuser.Authority] 对于属性myAuthorities [0]:否 匹配的编辑器或转换 找到策略

Failed to convert property value of type java.lang.String to required type java.util.Collection for property myAuthorities; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [com.tda.model.applicationuser.Authority] for property myAuthorities[0]: no matching editors or conversion strategy found

哪个是解决此问题的正确方法?

Which is the correct way to solve this?

推荐答案

Authority是复杂的bean时,HTML表单仅适用于字符串值.您需要配置PropertyEditor以在AuthorityString之间执行转换:

HTML forms work with string values only, when your Authority is a complex bean. You need to configure a PropertyEditor to perform conversion between Authority and String:

@InitBinder 
public void initBinder(WebDataBinder b) {
    b.registerCustomEditor(Authority.class, new AuthorityEditor());
}

private class AuthorityEditor extends PropertyEditorSupport {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        setValue(authorityService.findById(Long.valueOf(text)));
    }

    @Override
    public String getAsText() {
        return ((Authority) getValue()).getId();
    }
}

这篇关于Spring-MVC 3.0 Crud with Checkboxes问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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