Spring-mvc 3.0 带有复选框问题的 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 中,我的视图具有以下形式:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您的
Authority
是一个复杂的 bean 时,HTML 表单仅适用于字符串值。您需要配置一个PropertyEditor
来执行Authority
和String
之间的转换:HTML forms work with string values only, when your
Authority
is a complex bean. You need to configure aPropertyEditor
to perform conversion betweenAuthority
andString
: