Spring MVC前后台传值

Spring MVC前台向后台传值

1.参数在URL中

  如:http://localhost:8099/SpringMVCProject/employee-module/saveEmployee?firstName=Kate&lastName=Tom,在URL中有参数firstName和lastName两个参数

  Controller类可以使用@RequestParam方式来接收参数:@RequestParam(value="firstName", required=false) 或@RequestParam("firstName")(为简写),代码如:

@RequestMapping(value = "/saveEmployee", method = RequestMethod.GET)
public String saveEmployee(@RequestParam(value="firstName", required=false) String firstName, @RequestParam(value="lastName", required=false) String lastName, Model model)
{
  if(firstName != null && lastName != null)
  {
    model.addAttribute("firstName", firstName);
    model.addAttribute("lastName", lastName);
  }
  return "employeeSave";
}

2.参数在form表单中

  参数存在form表单中,Controller类可以在方法中直接用jsp中标记的name名称进行接收或用类封装接收,这两种方式jsp端没有区别,Controller类的函数参数有区别,这里Spring MVC对封装到类中的参数接收和Struts2不一样,Struts2需要jsp端写成employee.firstName,而Spring MVC中只需要保持firstName就可以。

jsp代码:

<form action="${pageContext.request.contextPath}/employee-module/submitEmployeeSaved" method="post">
  firstName:<input type="text" name="firstName"/>
  lastName:<input type="text" name="lastName"/>
  <input type="submit" value="submit"/>
</form>

Controller类中方法代码:

使用普通参数接收

@RequestMapping(value = "/submitEmployeeSaved", method = RequestMethod.POST)
public String submitEmployeeSaved(String firstName, String lastName, Model model)
{
  model.addAttribute("firstName", firstName);
  model.addAttribute("lastName", lastName);
  return "employeeSavedSuccess";
}

使用EmployeeVO类接收参数

@RequestMapping(value = "/submitEmployeeSaved", method = RequestMethod.POST)
public String submitEmployeeSaved(EmployeeVO employee, Model model)
{
  model.addAttribute("firstName", employee.getFirstName());
  model.addAttribute("lastName", employee.getLastName());
  return "employeeSavedSuccess";
}

EmployeeVO 类

public class EmployeeVO implements Serializable
{

  private static final long serialVersionUID = 1L;

  private Integer id;
  private String firstName;
  private String lastName;

  public Integer getId() {
    return id;
  }
  public void setId(Integer id) {
    this.id = id;
  }
  public String getFirstName() {
    return firstName;
  }
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
  public String getLastName() {
    return lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  @Override
  public String toString() {
    return "EmployeeVO [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
  }
}

Spring MVC后台前后台传值

  后台向前台传值,可以将参数存放在model中(MAP结构),前台jsp使用EL表达式来获取参数值,如:

<html>
<head>
<title>Spring MVC Hello World</title>
</head>

<body>
  <h2>All Employees in System</h2>

  <table border="1">
    <tr>
      <th>Employee Id</th>
      <th>First Name</th>
      <th>Last Name</th>
    </tr>
    <c:forEach items="${employees}" var="employee">
      <tr>
        <td>${employee.id}</td>
        <td>${employee.firstName}</td>
        <td>${employee.lastName}</td>
      </tr>
    </c:forEach>
  </table>

  <input type="text" value="${name} ">
</body>
</html>

posted on 2016-03-29 15:06  KAZMA  阅读(704)  评论(0)    收藏  举报

导航