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

标签 java mysql spring hibernate spring-mvc

我是 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/

相关文章:

java - 使用具有自定义功能接口(interface)的流

php - 链接到具有动态名称的文件

java - 一个jsp中的多个表单

php - 在同一张表上同时更新和选择

php - 将 MySQL 从本地主机移动到远程服务器

java - 在 Spring 应用程序中使用或测试时如何将参数传递给 Spring 存储库

java - 用于访问 LinkedIn 数据的 Spring Boot 应用程序

java - Maven 嵌入器 : Compiler dependency could not be resolved - No connector factories available

java - 无法理解 Spring security 的行为

java - 对象构建