public class Employee implements IEmployee, Serializable {
private static final long serialVersionUID = 3539505455231361934L;
@Column(name="emp_Id", nullable=false)
private Integer emp_Id;
@Id
@GeneratedValue
@Column(name="login_Id", nullable=false)
private String login_Id;
@Column(name="password", nullable=false)
private String password;
@Column(name="first_name", nullable=false)
private String first_name;
@Column(name="last_name", nullable=false)
private String last_name;
@Column(name="email", nullable=false)
private String email;
@Column(name="address", nullable=false)
private String address;
@Column(name="mobile_Number", nullable=false)
private Integer mobile_Number;
@Column(name="create_Date", nullable=false)
private Date create_Date;
@Column(name="modified_Date", nullable=false)
private Date modified_Date;
@Column(name="security_Question")
private String security_Question;
@Column(name="security_Question_Answer")
private String security_Question_Answer;
@Column(name="login_Attempts")
private String login_Attempts;
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinTable(name="employe_role",
joinColumns = {@JoinColumn(name="login_Id")},
inverseJoinColumns = {@JoinColumn(name="role_Id")})
private Collection<Role> role;//need to change name --> Dependent Object Role
---定居者和吸气鬼 }
@Id
@GeneratedValue
@Column(name="role_Id")
private Integer role_Id;
@Column(name="role_Code", nullable=false)
private String role_Code;
@Column(name="role_Name", nullable=false)
private String role_Name;
@Column(name="discription", nullable=false)
private String discription;
@Column(name="created_Date")
private Date created_Date;
@Column(name="modified_Date")
private Date modified_Date;
<form:form name="register-employee" action="/registerEmployee" method="post" commandName="employee">
<c:forEach var="role" varStatus="statusEmpRole" items="${employee.role}">
<form:hidden path="role[${statusEmpRole.index}].role_Name" value="${role.role_Name}" />
<form:checkbox path="role[${statusEmpRole.index}].role_Name" value="${role.role_Name}" itemValue="role.role_Id" />
<c:out value="${role.role_Name}" /><br>
用于显示表单
@Override
@RequestMapping(value="/employeeregistrationform", method = RequestMethod.GET)
public ModelAndView employeeRegistrationForm(@ModelAttribute("employee") Employee employee, Model map) throws HibernateException, RoleNotFoundException {
IEmployee iEmployee = new Employee();
Collection<Role> collectionRoles= IRoleService.getLookUpRoles();
for (Role role : collectionRoles) {
LOGGER.info("roel {}",role.getRole_Name());
}
iEmployee.setFirst_name("helloooooo");
iEmployee.setRole(collectionRoles);
return new ModelAndView("registerEmploye", "employee", iEmployee);
}
获取提交的表单数据
@Override
@RequestMapping(value="/registerEmployee",method=RequestMethod.POST)
public ModelAndView registerEmployee(@ModelAttribute("employee")Employee employee, BindingResult result) {
LOGGER.info("Registering Employe {}",employee.getFirst_name());
LOGGER.info("Selected Role Employe {}",employee.getRole());
ModelAndView model = new ModelAndView();
model.setViewName("registerEmploye");
return model;
}
employee.getRole()变为null 我的情况是员工有多个角色。让管理员和Projectmanager说。创建员工管理员可以在提交员工注册后选择角色(这些来自数据库),我得到的角色对象为空。
请帮帮我。我错过了像属性编辑器或init绑定器这样的东西。如果是这样,请举例说明如何使用它们。
由于
答案 0 :(得分:0)
我创建了Init Binder
@InitBinder
public void bindForm(WebDataBinder binder) {
binder.registerCustomEditor(Collection.class, new RoleEditor(Collection.class,true));
}
我提供CustomCollectionEditor,即RoleEditor
package com.evoke.tms.util;
import java.util.HashSet; import java.util.Set;
import org.springframework.beans.propertyeditors.CustomCollectionEditor;
import com.evoke.tms.model.Role;
public class RoleEditor扩展CustomCollectionEditor {
private Set<Role> roles;
public RoleEditor(Class collectionType, boolean nullAsEmptyCollection) {
super(collectionType, true);
}
public void setValue( Object object ){
if(object!=null&&object instanceof String)
System.out.println("Object is of type - " + object.getClass().getCanonicalName());
String[] roleIds = (String[])object;
roles=new HashSet<Role>();
if(roleIds!=null && roleIds.length>0)
for( int i=0; i<roleIds.length; i++ ){
try {
int id = Integer.parseInt(roleIds[i]);
Role role = new Role();
role.setRole_Id(id);
roles.add(role);
}catch( NumberFormatException ne ){}
}
}
public Object getValue(){
System.out.println("Roles are - " + roles);
return roles;
}
}
我仍然对它的运作方式感到困惑 任何人都可以帮忙...