提前谢谢
我有添加员工表格的问题,用户设置了这么多信息姓名,电话。。。。直到下拉多选(我的问题)用户必须从下拉列表中选择一个或多个,但问题是我无法得到这个用户已经选择的列表我尝试了许多方法,但我找到了这两种方法,这两种方法更接近解决我的问题下来你会发现2个版本的控制器和视图+类信息,如果你需要的话
controller class+html视图“version one”:问题是如果我从html页面的下拉列表中选择了3个条目,那么我在controller中得到的正确的所选列表大小是3,但是如果我尝试得到这个条目的项目或雇员,那么它会显示为null
@Controller
@RequestMapping("/employee")
public class EmployeeController {
// more codes
@PostMapping("/save")
public String saveEmployee(
@ModelAttribute("employee") Employee employee,
Model model) {
System.out.println("size ="+ employee.getWorkOns.size()); // its work well
System.out.println("size ="+ employee.getWorkOns.get(0).getEmployee()); // nullpointerexception
System.out.println("size ="+ employee.getWorkOns.get(0).getProject()); // nullpointerexception
// workOns is list of workOn
model.addAttribute("workOnList", workOns);
// more code
}
html选择选项版本1:
<form th:action="@{/employee/save}" th:object="${employee}" method="POST">
<!-- more code -->
<select th:field="*{workOns}" multiple="multiple"> <!-- workOns is field from employee and its a list<WorkOn> -->
<option th:each="workon: ${workOnList}"
th:value="${{workon}}"
th:text="${workon.project.name}"/>
</select>
<!-- more code -->
</form>
controller class+html视图“version tow”:在这种情况下,我尝试获取工作项的项目名称selecte the problem如果我从html页面的下拉列表中选择3个项,那么我在controller中得到的selected list的大小是错误的,大小总是1
@Controller
@RequestMapping("/employee")
public class EmployeeController {
// more codes
@PostMapping("/save")
public String saveEmployee(
@ModelAttribute("employee") Employee employee,
@ModelAttribute("workOns") List<String> workOnList,
Model model) {
System.out.println("size ="+ employee.getWorkOns.size()); // always is print 1
// workOns is list of workOn
model.addAttribute("workOnList", workOns);
// more code
}
html选择选项版本2:
<form th:action="@{/employee/save}" th:object="${employee}" method="POST">
<!-- more code -->
<select name="workOns" multiple="multiple">
<option th:each="workon: ${workOnList}"
th:value="${workon.project.projId}"
th:text="${workon.project.name}"/>
</select>
<!-- here i try get list names of the project of workOn whish the user get selected -->
<!-- more code -->
</form>
员工类别
@Entity
@Table(name = "employee")
public class Employee {
// more vars ....
@OneToMany(mappedBy = "employee", cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH,CascadeType.REMOVE })
private List<WorkOn> workOns;
// constructors and setters and getters ....
}
项目类
@Entity
@Table(name="project")
public class Project {
// more vars ....
@OneToMany(mappedBy = "project", cascade = {CascadeType.DETACH,CascadeType.MERGE,CascadeType.REFRESH,CascadeType.REMOVE })
private List<WorkOn> WorkOns;
// constructors and setters and getters ....
}
修井班
@Entity
@Table(name = "workon")
public class WorkOn {
// more vars ....
@ManyToOne(cascade = {CascadeType.DETACH , CascadeType.MERGE , CascadeType.REFRESH})
@JoinColumn(name = "emp_id")
private Employee employee;
@ManyToOne(cascade = {CascadeType.DETACH , CascadeType.MERGE , CascadeType.REFRESH})
@JoinColumn(name = "proj_id")
private Project project;
// constructors and setters and getters ....
}
部门级
@Entity
@Table(name="department" )//,schema = "company_management")
public class Department {
// more vars ....
@OneToMany(mappedBy = "department" , cascade = {CascadeType.DETACH,CascadeType.MERGE,CascadeType.REFRESH,CascadeType.REMOVE})
private List<Employee> employees;
@OneToMany(mappedBy = "department" , cascade = {CascadeType.DETACH,CascadeType.MERGE,CascadeType.REFRESH,CascadeType.REMOVE})
private List<Project> projects;
// constructors and setters and getters ....
}
暂无答案!
目前还没有任何答案,快来回答吧!