Spring-mvc 3.0 带有复选框问题的 CRUD

发布于 2024-09-14 06:50:12 字数 2454 浏览 1 评论 0原文

我正在做一个简单的用户增删改查。

我的 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 中,我的视图具有以下形式:

<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 属性 我的权力机构;嵌套异常是 java.lang.IllegalStateException: 无法转换类型值 [java.lang.String] 为所需类型 [com.tda.model.applicationuser.Authority] 对于属性 myAuthorities[0]:否 匹配编辑器或转换 找到策略

解决这个问题的正确方法是什么?

I am doing an simple user crud.

My ApplicationUser has the following properties:

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;

Authority class has:

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

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>

The jsp gets allAuthorities from this:

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

When I fill the form I get:

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?

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

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

发布评论

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

评论(1

仅冇旳回忆 2024-09-21 06:50:12

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

@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();
    }
}

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