springmvc(七)之RESTFUL_CRUD&springmvc表单标签&处理静态资源

1.需求

1.1显示所有员工信息

  • URI:emps
  • 请求方式:GET

1.2添加员工

显示添加页面

  • URI
  • 请求方式:GET

添加员工信息

  • URI:emp
  • 请求方式:POST
  • 显示效果:完成添加,重定向到list页面

1.3删除操作

  • URI:emp/{id}
  • 请求方式:DELETE

1.4修改操作:lastName不可修改

显示修改页面

  • URI:emp/{id}
  • 请求方式:GET
  • 显示效果:回显表单

修改员工信息

  • URI:emp
  • 请求方式:PUT
  • 显示效果:完成修改,重定向到list页面

2.开发

项目结构图:
在这里插入图片描述

步骤:
【第一步】、创建一个web工程。如:springmvc-02-restful
【第二步】、配置web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
					http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
	id="WebApp_ID" version="4.0">
	
	<!-- 配置前端控制器 -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 配置springmvc.xml配置文件的位置和名称。如果不指定,默认为:/WEB-INF/${servlet-name}-servlet.xml -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<!-- springDispatcherServlet在当前应用被加载时创建,而不是在第一次请求时创建 -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<!-- 拦截所有请求 -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

【第三步】、创建springmvc.xml文件
在src目录下创建springmvc.xml

【第四步】、配置浏览器支持restful风格
在web.xml中添加如下内容:

	...
	...
	<!-- 配置HiddenHttpMethodFilter 可以把POST请求转换为DELETE请求/POST请求 -->
	<filter>
		<filter-name>hiddenHttpMethodFilter</filter-name>
		<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>hiddenHttpMethodFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

【第五步】、配置springmvc.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

	<!-- 配置自动扫描包 -->
	<context:component-scan base-package="com.zzc"></context:component-scan>
	
	<!-- 配置视图解析器:如何把 handler 方法返回值解析为实际的物理视图 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" ></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
</beans>

【第六步】、创建包和类
pojo层
在src目录下创建com.zzc.pojo包,其下创建Employee类和Department类
Employee.java

public class Employee {

	private Integer id;
	private String lastName;
	private String email;
	private Integer sex;
	private Department department;
	
	public Employee() {
		
	}
	...
}	

Department.java

public class Department {

	private Integer id;
	private String name;
	...
}

dao层
没有连接数据库

在src目录下创建com.zzc.dao包,其下创建EmployeeDao类和DepartmentDao类
DepartmentDao.java

@Repository
public class DepartmentDao {

	private static Map<Integer, Department> departments = null;
	
	static {
		departments = new HashMap<Integer, Department>();
		
		departments.put(1001, new Department(10001, "测试部"));
		departments.put(1002, new Department(10002, "开发部"));
		departments.put(1003, new Department(10003, "销售部"));
		departments.put(1004, new Department(10004, "人事部"));
		departments.put(1005, new Department(10005, "财务部"));
	}
	
	/**
	 * 获取全部departments
	 * 
	 * @return
	 */
	public Collection<Department> getDepartments(){
		return departments.values();
	}
	
	/**
	 * 获取指定的department
	 * 
	 * @param id
	 * @return
	 */
	public Department getDepartment(Integer id){
		return departments.get(id);
	}
}

EmployeeDao.java

@Repository
public class EmployeeDao {

	private static Integer initId = 106;
	private static Map<Integer, Employee> employees = null;
	@Autowired
	private DepartmentDao departmentDao;
	
	static {
		employees = new HashMap<Integer, Employee>();
		
		employees.put(101, new Employee(101, "zzc", "[email protected]", 1, new Department(10002, "开发部")));
		employees.put(102, new Employee(102, "zhangsan", "[email protected]", 1, new Department(10004, "人事部")));
		employees.put(103, new Employee(103, "lisi", "[email protected]", 0, new Department(10001, "测试部")));
		employees.put(104, new Employee(104, "wangwu", "[email protected]", 0, new Department(10005, "财务部")));
		employees.put(105, new Employee(105, "zhaoliu", "[email protected]", 1, new Department(10003, "销售部")));
	}
	
	/**
	 * 添加员工
	 * 
	 * @param employee
	 */
	public void save(Employee employee) {
		if(null == employee.getId()) {
			employee.setId(initId++);
		}
		employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
		employees.put(employee.getId(), employee);
	}
	
	/**
	 * 获取所有员工
	 * 
	 * @return
	 */
	public Collection<Employee> getAll(){
		return employees.values();
	}
	
	/**
	 * 获取指定员工
	 * 
	 * @param id
	 * @return
	 */
	public Employee get(Integer id) {
		return employees.get(id);
	}
	
	/**
	 * 删除员工
	 * 
	 * @param id
	 */
	public void delete(Integer id) {
		employees.remove(id);
	}
}

【第七步】、创建页面
在WebContent目录下创建index.jsp,其内容为;

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isErrorPage="true"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>springmvc入门</title>

</head>
<body>
	<a href="emps">List All Employees</a>
</body>
</html>

访问所有员工信息

【第八步】、创建控制器、页面
在src目录下创建com.zzc.controller包,其下创建EmployeeController类
EmployeeController.java

@Controller
public class EmployeeController {
	@Autowired
	private EmployeeDao employeeDao;
	@Autowired
	private DepartmentDao departmentDao;
	
	@ModelAttribute
	public void getEmployee(Map<String, Object> map,
			@RequestParam(value="id", required=false)Integer id) {
		if(null != id) {
			map.put("employee", employeeDao.get(id));
		}
	}
	
	@RequestMapping(value="/emp", method=RequestMethod.PUT)
	public String updateOpt(Employee employee) {
		employeeDao.save(employee);
		return "redirect:/emps";
	}
	
	/**
	 * 跳转到更新员工信息页面
	 * 
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/emp/{id}", method=RequestMethod.GET)
	public String update(@PathVariable("id") Integer id, Map<String, Object> map) {
		map.put("employee", employeeDao.get(id));
		map.put("departments", departmentDao.getDepartments());
		return "update";
	}
	
	/**
	 * 删除员工
	 * 
	 * @return
	 */
	@RequestMapping(value="/emp/{id}", method=RequestMethod.DELETE)
	public String delete(@PathVariable("id") Integer id) {
		employeeDao.delete(id);
		return "redirect:/emps";
	}
	
	/**
	 * 执行添加员工操作
	 * 
	 * @param employee
	 * @return
	 */
	@RequestMapping(value="/emp", method=RequestMethod.POST)
	public String addOpt(Employee employee) {
		System.out.println(employee);
		employeeDao.save(employee);
		return "redirect:/emps";
	}
	
	/**
	 * 跳转到添加页面
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/emp", method=RequestMethod.GET)
	public String add(Map<String, Object> map) {
		map.put("departments", departmentDao.getDepartments());
		map.put("employee", new Employee());
		return "add";
	}
	
	/**
	 * 查询所有员工信息
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("/emps")
	public String list(Map<String, Object> map) {
		map.put("employees", employeeDao.getAll());
		return "list";
	}
}

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>员工列表</title>
<script type="text/javascript" src="/springmvc-02-restful/js/jquery-3.2.1.js"></script>
<script type="text/javascript">
	$(function(){
		$(".delete").click(function(){
			var href = $(this).attr("href")
			$("form").attr("action", href).submit()
			return false
		})
	})
</script>
</head>
<body>

	<form action="" method="post">
		<input type="hidden" name="_method" value="DELETE" />
	</form>

	<c:if test="${empty requestScope.employees }">
		没有任何员工信息
	</c:if>
	
	<c:if test="${!empty requestScope.employees }">
		<table border="1" cellpadding="10" cellspacing="0">
			<tr>
				<th>ID</th>
				<th>LastName</th>
				<th>Email</th>
				<th>Sex</th>
				<th>Department</th>
				<th>Edit</th>
				<th>Delete</th>
			</tr>
			
			<c:forEach items="${requestScope.employees }" var="emp">
				<tr>
					<td>${emp.id }</td>
					<td>${emp.lastName }</td>
					<td>${emp.email }</td>
					<td>${emp.sex == 0 ? "女" : "男" }</td>
					<td>${emp.department.name }</td>
					<td>
						<a href="emp/${emp.id}">Edit</a>
					</td>
					<td>
						<a class="delete" href="emp/${emp.id}">Delete</a>
					</td>
				</tr>
			</c:forEach>
		</table>
	</c:if>
	<br><br>
	<a href="emp">Add Employee</a>
</body>
</html>

update.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加员工</title>
</head>
<body>
	<!-- 使用spring的form标签 -->
	<form:form action="${pageContext.request.contextPath}/emp" method="POST" modelAttribute="employee">
		<c:if test="${employee.id == null }">
			<!-- path对应html中的name属性 -->
			LastName:<form:input path="lastName" /><br>
		</c:if>
		<c:if test="${employee.id != null }">
			<form:hidden path="id"/>
			<input type="hidden" name="_method" value="PUT" />
		</c:if>
		
		Email:<form:input path="Email" /><br>
		<%
			Map<String, String> sexs = new HashMap<String, String>();
			sexs.put("0", "女");
			sexs.put("1", "男");
			request.setAttribute("sexs", sexs);
		%>
		Sex:<form:radiobuttons path="sex" items="${sexs }"/><br>
		
		Department:<form:select path="department.id" items="${departments }" 
			itemLabel="name" itemValue="id"></form:select>
		<input type="submit" value="添加" />
	</form:form>
</body>
</html>

【第九步】、完成需求
查询所有员工

修改EmployeeController.java

@Controller
public class EmployeeController {
	@Autowired
	private EmployeeDao employeeDao;
	
	/**
	 * 查询所有员工信息
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("/emps")
	public String list(Map<String, Object> map) {
		map.put("employees", employeeDao.getAll());
		return "list";
	}
}

Map中添加了一个key为employees,value为所有员工信息。

在WEB-INF目录下面创建views目录,并创建list.jsp,其内容为:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>员工列表</title>
</head>
<body>
	<c:if test="${empty requestScope.employees }">
		没有任何员工信息
	</c:if>
	
	<c:if test="${!empty requestScope.employees }">
		<table border="1" cellpadding="10" cellspacing="0">
			<tr>
				<th>ID</th>
				<th>LastName</th>
				<th>Email</th>
				<th>Sex</th>
				<th>Department</th>
				<th>Edit</th>
				<th>Delete</th>
			</tr>
			
			<c:forEach items="${requestScope.employees }" var="emp">
				<tr>
					<td>${emp.id }</td>
					<td>${emp.lastName }</td>
					<td>${emp.email }</td>
					<td>${emp.sex == 0 ? "女" : "男" }</td>
					<td>${emp.department.name }</td>
					<td>
						<a href="">Edit</a>
					</td>
					<td>
						<a href="">Delete</a>
					</td>
				</tr>
			</c:forEach>
		</table>
	</c:if>
</body>
</html>

添加员工
list.jsp,添加如下链接来添加员工

	...
	...
			</c:forEach>
		</table>
	</c:if>
	<br><br>
	
	<a href="emp">Add Employee</a>
</body>
</html>

点击上面那个链接就会跳转到添加员工页面

EmployeeController.java,添加跳转添加页面方法:

	/**
	 * 跳转到添加页面
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/emp", method=RequestMethod.GET)
	public String add(Map<String, Object> map) {
		map.put("departments", departmentDao.getDepartments());
		map.put("employee", new Employee());
		return "add";
	}

由于要在添加员工页面中显示部门信息,所以要先查询,再放入请求域中。

在views目录下创建add.jsp,其内容为:

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加员工</title>
</head>
<body>
	<!-- 使用spring的form标签 -->
	<form:form action="emp" method="POST" modelAttribute="employee">
		<!-- path对应html中的name属性 -->
		LastName:<form:input path="lastName" /><br>
		Email:<form:input path="Email" /><br>
		<%
			Map<String, String> sexs = new HashMap<String, String>();
			sexs.put("0", "女");
			sexs.put("1", "男");
			request.setAttribute("sexs", sexs);
		%>
		Sex:<form:radiobuttons path="sex" items="${sexs }"/><br>
		
		Department:<form:select path="department.id" items="${departments }" 
			itemLabel="name" itemValue="id"></form:select>
		<input type="submit" value="添加" />
	</form:form>
</body>
</html>

使用的是spring中的form表单提交

后台执行添加员工操作(EmployeeController.java添加 如下方法):

	/**
	 * 执行添加员工操作
	 * 
	 * @param employee
	 * @return
	 */
	@RequestMapping(value="/emp", method=RequestMethod.POST)
	public String addOpt(Employee employee) {
		System.out.println(employee);
		employeeDao.save(employee);
		return "redirect:/emps";
	}

添加完毕后,重定向到员工列表页面

删除员工
修改list.jsp

<c:forEach items="${requestScope.employees }" var="emp">
	<tr>
		<td>${emp.id }</td>
		<td>${emp.lastName }</td>
		<td>${emp.email }</td>
		<td>${emp.sex == 0 ? "女" : "男" }</td>
		<td>${emp.department.name }</td>
		<td>
			<a href="">Edit</a>
		</td>
		<td>
			<a href="emp/${emp.id}">Delete</a>
		</td>
	</tr>
</c:forEach>

后台:

	/**
	 * 删除员工
	 * 
	 * @return
	 */
	@RequestMapping(value="/emp/{id}", method=RequestMethod.DELETE)
	public String delete(@PathVariable("id") Integer id) {
		employeeDao.delete(id);
		return "redirect:/emps";
	}

由于是DELETE请求,即使配置了HiddenHttpMethodFilter,但它的作用:可以把POST请求转换为DELETE请求/POST请求,但删除员工的链接是GET请求,所以,不能直接完成,需要求助js才能完成

在list.jsp中引入jquery
【注意】:要在springmvc.xml中配置不拦截静态资源

<!-- 不拦截静态资源 -->
<mvc:default-servlet-handler/>

作用:
        静态资源若没有经过映射请求,则交给WEB应用服务器默认的servlet处理;否则,交给springmvc前端控制器处理

有了上述配置之后,可以访问js文件,但是原有的url(/emps)都会失效。此时还需要添加一个配置

<mvc:annotation-driven />

需要两个以上的配置即可,此配置需要放在 视图解析器后面

更新员工
修改list.jsp

<c:forEach items="${requestScope.employees }" var="emp">
	<tr>
		<td>${emp.id }</td>
		<td>${emp.lastName }</td>
		<td>${emp.email }</td>
		<td>${emp.sex == 0 ? "女" : "男" }</td>
		<td>${emp.department.name }</td>
		<td>
			<a href="emp/${emp.id}">Edit</a>
		</td>
		<td>
			<a class="delete" href="emp/${emp.id}">Delete</a>
		</td>
	</tr>
</c:forEach>

后台:
跳转到更新页面

	/**
	 * 跳转到更新员工信息页面
	 * 
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/emp/{id}", method=RequestMethod.GET)
	public String update(@PathVariable("id") Integer id, Map<String, Object> map) {
		map.put("employee", employeeDao.get(id));
		map.put("departments", departmentDao.getDepartments());
		return "update";
	}

由于更新页面与添加页面基本没什么差异,只不过更新页面需要把数据进行回显而已。所以,这里就把更新页面与添加页面合在一起了(可以把之前Controller类中的方法的“add”修改为“update”,也可以不修改)
update.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加员工</title>
</head>
<body>
	<!-- 使用spring的form标签 -->
	<form:form action="${pageContext.request.contextPath}/emp" method="POST" modelAttribute="employee">
		<c:if test="${employee.id == null }">
			<!-- path对应html中的name属性 -->
			LastName:<form:input path="lastName" /><br>
		</c:if>
		<c:if test="${employee.id != null }">
			<form:hidden path="id"/>
			<input type="hidden" name="_method" value="PUT" />
		</c:if>
		
		Email:<form:input path="Email" /><br>
		<%
			Map<String, String> sexs = new HashMap<String, String>();
			sexs.put("0", "女");
			sexs.put("1", "男");
			request.setAttribute("sexs", sexs);
		%>
		Sex:<form:radiobuttons path="sex" items="${sexs }"/><br>
		
		Department:<form:select path="department.id" items="${departments }" 
			itemLabel="name" itemValue="id"></form:select>
		<input type="submit" value="添加" />
	</form:form>
</body>
</html>

后台:
更新员工操作

	@ModelAttribute
	public void getEmployee(Map<String, Object> map,
			@RequestParam(value="id", required=false)Integer id) {
		if(null != id) {
			map.put("employee", employeeDao.get(id));
		}
	}
	
	/**
	 * 更新员工
	 * 
	 * @param employee
	 * @return
	 */
	@RequestMapping(value="/emp", method=RequestMethod.PUT)
	public String updateOpt(Employee employee) {
		employeeDao.save(employee);
		return "redirect:/emps";
	}

由于lastName不需要修改,所以需要添加@ModelAttribute注解

发布了78 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Lucky_Boy_Luck/article/details/100107511
今日推荐