簡體   English   中英

SPRING MVC-如何獲取form:JSP到Controller的輸入路徑的值

[英]SPRING MVC - How to get the value of form:input path from JSP to Controller

我是Spring MVC的新手,遇到了這個問題,我想獲取form:input path的值並將其傳遞給我的控制器。

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.現在,根據您的表單參數更改控制器的方法:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM