疯狂java


您现在的位置: 疯狂软件 >> 新闻资讯 >> 正文

Spring MVC流程表单提交


 

Spring MVC学习教程之流程表单提交,3步实现这个功能
 
本文中将用Spring实现流程表单的提交功能,具体功能是使用web表单来添加一个新员工到员工名单列表中。
 
 
 
网络配图
 
第1步:准备所需的类
 
添加 addEmployeeEmployeeManager.java方法,这个方法将用于添加新员工到列表中。
 
EmployeeManager.java
 
package com.programcreek.helloworld.service;
 
import java.util.ArrayList;
 
import java.util.List;
 
import com.programcreek.helloworld.model.Employee;
 
public class EmployeeManager {
 
private static List<Employee> employeeList;
 
public EmployeeManager(){
 
employeeList = new ArrayList<Employee>();
 
employeeList.add(new Employee(1, "Mike", "Smith"));
 
employeeList.add(new Employee(2, "John", "Taylor"));
 
employeeList.add(new Employee(3, "Dave", "Wilson"));
 
}
 
public List<Employee> getEmployeeList(){
 
return employeeList;
 
}
 
public Employee addEmployee(Employee e){
 
Employee employee = new Employee();
 
employee.setId(employeeList.size()+1);
 
employee.setFirstName(e.getFirstName());
 
employee.setLastName(e.getLastName());
 
employeeList.add(employee);
 
return employee;
 
}
}
 
Employee.java
 
package com.programcreek.helloworld.model;
 
public class Employee {
 
private int id;
 
private String lastName;
 
private String firstName;
 
public Employee(){
 
}
 
public Employee(int id, String lastName, String firstName) {
 
this.id = id;
 
this.lastName = lastName;
 
this.firstName = firstName;
 
}
 
public int getId() {
 
return id;
 
}
 
public void setId(int id) {
 
this.id = id;
 
}
 
public String getLastName() {
 
return lastName;
 
}
 
public void setLastName(String lastName) {
 
this.lastName = lastName;
 
}
 
public String getFirstName() {
 
return firstName;
 
}
 
public void setFirstName(String firstName) {
 
this.firstName = firstName;
 
}
 
}
 
第2步:创建控制器
 
创建一个控制器来处理表单的提交。
 
EmployeeAddController.java
 
package com.programcreek.helloworld.controller;
 
import org.springframework.stereotype.Controller;
 
import org.springframework.web.bind.annotation.ModelAttribute;
 
import org.springframework.web.bind.annotation.RequestMapping;
 
import org.springframework.web.servlet.ModelAndView;
 
import com.programcreek.helloworld.model.Employee;
 
import com.programcreek.helloworld.service.EmployeeManager;
 
@Controller
 
public class EmployeeAddController {
 
@RequestMapping("/showEmployeeForm")
 
public ModelAndView getEmployeeForm(){
 
ModelAndView mv = new ModelAndView("employeeAdd");
 
mv.addObject("employeeEntity", new Employee());
 
return mv;
 
}
 
@RequestMapping("/addEmployee")
 
public ModelAndView addEmployee(@ModelAttribute Employee e){
 
ModelAndView mv = new ModelAndView("EmployeeList");
 
EmployeeManager employeeManager = new EmployeeManager();
 
employeeManager.addEmployee(e);
 
mv.addObject("EmployeeList", employeeManager.getEmployeeList());
 
return mv;
 
}
 
}
 
 
 
网络配图
 
控制器有两个请求:一个是实现网页表单和其他流程表单的提交,这里用 @ModelAttribute的注解形式将属性映射到一个
 
员工表。
 
第3步:创建视图即jsp页面
 
employeeAdd.jsp
 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
<html>
 
<body>
 
<h1>Add Employee</h1>
 
<form:form method="post" modelAttribute="employeeEntity" action="addEmployee">
 
First Name:<form:input path="firstName"></form:input><br/>
 
Last Name: <form:input path="lastName"></form:input><br/>
 
<input type="submit" value="Submit">
 
</form:form>
 
</body>
 
</html>
 
添加一个新的链接,用来完成添加员工。
 
index.jsp
 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 
pageEncoding="ISO-8859-1"%>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
<html>
 
<head>
 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 
<title>Spring 4 MVC - HelloWorld Index Page</title>
 
</head>
 
<body>
 
<center>
 
<h3>
 
<a href="hello">Hello World</a>
 
</h3>
 
<h3>
 
<a href="employee">员工列表</a>
 
</h3>
 
<h3>
 
<a href="showEmployeeForm">添加</a>
 
</h3>
 
</center>
 
</body>
 
</html>