我有类User和userRole,我需要在User中绑定UserRole对象
我尝试下面的代码
UserRole.java
public class UserRoleBean {
private Integer role_id;
private String roleName;
private String roleDesc;
//getter setter
}
User.java
public class UserBean {
private Integer userId;
private String firstName;
private String lastName;
private Date dob;
private String emailAddr;
private String mobileNo;
private Integer balance;
private String password;
private UserRoleBean roleBean;
//getter and setter;
}
AddUser.jsp
<form:form method="POST" action="saveUser.html" modelAttribute="userBean">
<table>
<tr>
<td><form:label path="userId">User ID:</form:label></td>
<td><form:input path="userId" value="${user.userId}" readonly="true"/></td>
</tr>
<tr>
<td><form:label path="firstName">First Name:</form:label></td>
<td><form:input path="firstName" value="${user.firstName}"/></td>
</tr>
<tr>
<td><form:label path="lastName">Last Name :</form:label></td>
<td><form:input path="lastName" value="${user.lastName}"/></td>
</tr>
<tr>
<td><form:label path="emailAddr">Email Address :</form:label></td>
<td><form:input path="emailAddr" value="${user.emailAddr}" readonly="false"/></td>
</tr>
<tr>
<td><form:label path="dob">Date Of Birth:</form:label></td>
<td><form:input path="dob" /></td>
</tr>
<tr>
<td><form:label path="mobileNo">Mobile No :</form:label></td>
<td><form:input path="mobileNo" value="${user.mobileNo}"/></td>
</tr>
<tr>
<td><form:label path="password">Password:</form:label></td>
<td><form:password path="password" value="${user.password}"/></td>
</tr>
<tr>
<td><form:label path="balance">Balance:</form:label></td>
<td><form:input path="balance" value="${user.balance}"/></td>
</tr>
<tr>
<td><form:label path="roleBean">Select Role:</form:label></td>
<td>
<form:select path="roleBean">
<form:option value="0" label="Select" />
<form:options items="${forRoles}" itemValue="role_id" itemLabel="roleName" />
</form:select>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="${cap}"/></td>
</tr>
</table>
</form:form>
其他属性已绑定,但roleBean无法绑定,如何执行此操作,
任何帮助,将不胜感激
最佳答案
您必须像对User对象所做的那样分别设置roleBean
的每个属性。
设置roleBean
的任何属性时,将roleBean.role_id
用于Id
,将'roleBean.roleName'用于'roleName'。
就像roleName
的路径是<form:select path="roleBean">
,但如果<form:select path="roleBean.roleName">
的路径应该是roleName
。其他属性也是如此。
关于java - 无法以 Spring 形式绑定(bind)属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42575896/