Java SpringMVC-如何获取表单的值:从JSP到控制器的输入路径
我是SpringMVC的新手,我遇到了一个问题,我想获取表单的值:输入路径并将其传递给我的控制器 Employee.javaJava SpringMVC-如何获取表单的值:从JSP到控制器的输入路径,java,mysql,spring,hibernate,spring-mvc,Java,Mysql,Spring,Hibernate,Spring Mvc,我是SpringMVC的新手,我遇到了一个问题,我想获取表单的值:输入路径并将其传递给我的控制器 Employee.java public class Employee implements Serializable { private static final long serialVersionUID = -3465813074586302847L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private int empId
public class Employee implements Serializable {
private static final long serialVersionUID = -3465813074586302847L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int empId;
@Column
private String name;
@Column
private String email;
@Column
private String address;
@Column
private String telephone;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="empUserId")
private EmployeeUserAccount employeeUserAccount;
//setters and getters
public class EmployeeUserAccount implements Serializable {
private static final long serialVersionUID = -3465813074586302847L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int empUserId;
@Column
private String userName;
@Column
private String password;
@Column
private String userLevel;
@OneToOne(mappedBy="employeeUserAccount")
private Employee employee;
@RequestMapping(value = "/saveEmployee", method = RequestMethod.POST)
public ModelAndView saveEmployee(@ModelAttribute("command") Employee employee, @RequestParam("empUserId") Integer empUserId) {
// How to get the employeeUserAccount.userName from path
// How to get the employeeUserAccount.password from path
EmployeeUserAccount.java
public class Employee implements Serializable {
private static final long serialVersionUID = -3465813074586302847L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int empId;
@Column
private String name;
@Column
private String email;
@Column
private String address;
@Column
private String telephone;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="empUserId")
private EmployeeUserAccount employeeUserAccount;
//setters and getters
public class EmployeeUserAccount implements Serializable {
private static final long serialVersionUID = -3465813074586302847L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int empUserId;
@Column
private String userName;
@Column
private String password;
@Column
private String userLevel;
@OneToOne(mappedBy="employeeUserAccount")
private Employee employee;
@RequestMapping(value = "/saveEmployee", method = RequestMethod.POST)
public ModelAndView saveEmployee(@ModelAttribute("command") Employee employee, @RequestParam("empUserId") Integer empUserId) {
// How to get the employeeUserAccount.userName from path
// How to get the employeeUserAccount.password from path
EmployeeForm.jsp
<form:form action="saveEmployee" method="post" >
<table>
<form:hidden path="empId"/>
<input type="hidden" name="empUserId" value="${employee.employeeUserAccount.empUserId}"/>
<tr>
<td>Name:</td>
<td><form:input path="name" value="${employee.name}"/></td>
</tr>
<tr>
<td>Email:</td>
<td><form:input path="email" value="${employee.email}"/></td>
</tr>
<tr>
<td>Address:</td>
<td><form:input path="address" value="${employee.address}"/></td>
</tr>
<tr>
<td>Telephone:</td>
<td><form:input path="telephone" value="${employee.telephone}"/></td>
</tr>
<tr>
<td>Username:</td>
<td><form:input path="employeeUserAccount.userName" value="${employee.employeeUserAccount.userName}"/></td>
</tr>
<tr>
<td>Password:</td>
<td><form:input path="employeeUserAccount.password" value="${employee.employeeUserAccount.password}"/></td>
</tr>
<tr>
<td>Role:</td>
<td>
<%-- <form:select path="employeeUserAccount.userLevel">
<form:options />
</form:select> --%>
<form:select path="employeeUserAccount.userLevel">
<c:forEach items="${role}" var="r">
<c:choose>
<c:when test="${r==employee.employeeUserAccount.userLevel}">
<option value="${r}" selected="true">${r}</option>
</c:when>
<c:otherwise>
<option value="${r}">${r}</option>
</c:otherwise>
</c:choose>
</c:forEach>
</form:select>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Save"></td>
</tr>
</table>
</form:form>
一些要点:1。您的DTO(
Employee.java
)应包含默认构造函数。2。更改以下代码:
<form:form action="saveEmployee" method="post" >
该员工是您的DTO(数据传输对象)吗?仅将dto声明为参数将有助于获取值关于首先使用Model属性传递Employee对象是什么?为了完成数据绑定。我在你的代码中看不到这一点。现在我如何从控制器中获取用户名和密码的值呢?@Makudex,在你的例子中,控制器中的“employee.employeeUserAccount.password”将包含用户输入的密码。我的意思是employee.getEmployeeUserAccount().getPassword()
public ModelAndView saveEmployee(@ModelAttribute("saveRecord") Employee employee, @RequestParam("empUserId") Integer empUserId) {
//do anything you want
}