gpt4 book ai didi

java - SPRING MVC - 如何获取表单:input path from JSP to Controller的值

转载 作者:行者123 更新时间:2023-11-29 10:44:44 24 4
gpt4 key购买 nike

我是 Spring MVC 的新手,我遇到了这个问题,我想获取 form:input 路径的值并将其传递给我的 Controller 。

Employee.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

EmployeeUserAccount.java

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;

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>

EmployeeController.java

    @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

最佳答案

一些要点:
1. 您的 DTO (Employee.java) 应包含默认构造函数。
2. 更改以下代码:

<form:form action="saveEmployee" method="post" >

至:

<form:form action="saveEmployee" method="post" commandName="saveRecord">

3.将所有输入字段更改为-exp:

<form:input path="name" value="${employee.name}"/> 

<form:input path="name" value="${name}"/>

我的意思是将“employee.fieldName”更改为“fieldName
4.现在根据表单参数更改 Controller 的方法:

public ModelAndView saveEmployee(@ModelAttribute("saveRecord") Employee employee, @RequestParam("empUserId") Integer empUserId) {
//do anything you want
}

关于java - SPRING MVC - 如何获取表单:input path from JSP to Controller的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44820798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com