【问题标题】:SPRING MVC - How to get the value of form:input path from JSP to ControllerSPRING MVC - 如何获取表单的值:从 JSP 到控制器的输入路径
【发布时间】:2017-12-02 21:34:38
【问题描述】:

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

【问题讨论】:

  • 那 Employee 是你的 DTO(数据传输对象)吗?只有将 dto 声明为参数才能帮助您获取值
  • 首先使用 Model 属性传递一个 Employee 对象怎么样?以完成数据绑定。我在你的代码中看不到。

标签: java mysql spring hibernate spring-mvc


【解决方案1】:

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

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

到:

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

3.更改所有输入字段for-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
}

【讨论】:

  • 现在如何从控制器获取用户名和密码的值?
  • @Makudex,在您的情况下,控制器中的“employee.employeeUserAccount.password”将包含用户输入的密码。我的意思是employee.getEmployeeUserAccount().getPassword()
猜你喜欢
  • 2020-10-13
  • 2017-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-08
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
相关资源
最近更新 更多