一、RESTful CURD
1、显示所有员工信息
– URI:emps
– 请求方式:GET
– 显示效果
2、添加所有员工的信息
• URI:emp
• 请求方式:POST
• 显示效果:完成添加,重定向到 list 页面。
3. 删除操作
– URL:emp/{id}
– 请求方式:DELETE
– 删除后效果:对应记录从数据表中删除
4. 修改操作:lastName 不可修改!
– 显示修改页面:
• URI:emp/{id}
• 请求方式:GET
• 显示效果:回显表单。
– 修改员工信息:
• URI:emp
• 请求方式:PUT
• 显示效果:完成修改,重定向到 list 页面。
二、开始编码
1、导入依赖
<dependencies>
<!--spring核心-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!--aop-->
<!--<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>-->
<!--web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--日志-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
2、配置web.xml 和 springmvc.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--配置 HiddenHttpMethodFilter 作用是把 post请求转化为 put 和delete请求-->
<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>
<!--配置 DispatcherServlet -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<!--配置自动扫描包-->
<context:component-scan base-package="com.smart"/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
3、dao(静态数据,没有用数据库)和entity
@Repository
public class EmployeeDao {
private static Map<Integer ,Employee> employees = null;
@Autowired
private DepartmentDao departmentDao;
static {
employees = new HashMap<Integer, Employee>();
employees.put(1001, new Employee(1001, "E-AA", "[email protected]", 1, new Department(101, "D-AA")));
employees.put(1002, new Employee(1002, "E-BB", "[email protected]", 1, new Department(102, "D-BB")));
employees.put(1003, new Employee(1003, "E-CC", "[email protected]", 0, new Department(103, "D-CC")));
employees.put(1004, new Employee(1004, "E-DD", "[email protected]", 0, new Department(104, "D-DD")));
employees.put(1005, new Employee(1005, "E-EE", "[email protected]", 1, new Department(105, "D-EE")));
}
private static Integer initId = 1006;
public void save(Employee employee){
if (employee.getId()==null){
employee.setId(initId++);
}
employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
employees.put(employee.getId(),employee);
}
public void delete(Integer id){
employees.remove(id);
}
public Collection<Employee> getAll(){
return employees.values();
}
public Employee get(Integer id){
return employees.get(id);
}
}
@Repository
public class DepartmentDao {
private static Map<Integer,Department> departments = null;
static {
departments = new HashMap<Integer, Department>();
departments.put(101, new Department(101, "D-AA"));
departments.put(102, new Department(102, "D-BB"));
departments.put(103, new Department(103, "D-CC"));
departments.put(104, new Department(104, "D-DD"));
departments.put(105, new Department(105, "D-EE"));
}
public Collection<Department> getDepartments(){
return departments.values();
}
public Department getDepartment(Integer id){
return departments.get(id);
}
}
4、handler
@Controller
public class EmployeeHandler {
@Autowired
EmployeeDao employeeDao;
@RequestMapping(value = "/emps")
public String list(Map<String,Object> map){
map.put("employees",employeeDao.getAll());
return "list";
}
}
5、jsp页面——list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:if test="${empty employees}">
没有任何员工信息
</c:if>
<c:if test="${not empty employees}">
<table border="1" cellpadding="10" cellspacing="10">
<tr>
<td>ID</td>
<td>LastName</td>
<td>Email</td>
<td>Gender</td>
<td>Department</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<c:forEach items="${employees}" var="emp">
<tr>
<td>${emp.id}</td>
<td>${emp.lastName}</td>
<td>${emp.email}</td>
<td>${emp.gender==0?"Female":"Male"}</td>
<td>${emp.department.departmentName}</td>
<td><a href="">Edit</a></td>
<td><a href="">Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
启动项目之后发现页面无法显示 jstl 标签内容,原因是 maven 项目生成的 web.xml 页面的头有问题,修改头文件为:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
然后就OK了,以上仅仅是查询操作。
6、剩余操作
input.jsp
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %><%--
Created by IntelliJ IDEA.
User: ASUS
Date: 2018/7/27
Time: 13:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
1、为什么需要使用 form 标签?
可以更加快速的开发出表单页面,而且可以更加方便的进行表单值的回显
2、注意:
可以通过ModelAttribute属性指定绑定的模型,
若没有指定该属性,则默认从request 域对象中读取command 的表单的bean
如果该属性值不存在,则会发生错误
--%>
<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,Object> genders = new HashMap<>();
genders.put("1","Male");
genders.put("0","Female");
request.setAttribute("genders",genders);
%>
Gender:<form:radiobuttons path="gender" items="${genders}"/>
<br>
Department:<form:select path="department" items="${departments}"
itemLabel="departmentName" itemValue="id"></form:select>
<br>
<input type="submit" value="submit">
</form:form>
</body>
</html>
二、SpringMVC 的表单标签
通过SpringMVC 的表单标签可以实现将模型数据中的属性和HTML表单元素相绑定,以实现表单数据更便捷编辑和表单值的回显。
1、form 标签
1)一般情况下,通过GET请求获取表单页面,而通过POST请求提交表单页面,因此获取表单页面和提交表单页面的URL是相同的。只要满足该最佳条件契约,<form:form> 标签就无需通过action 属性指定表单提交的URL。
2)可以通过ModelAttribute 属性指定绑定的模型属性,若没有指定该属性,则默认从request 域对象中读取command 的表单bean,如果该属性值也不存在,则会发生错误。
2、表单标签
1)SpringMVC 提供了多个表单组件标签,如<form:input/>、<form:select/> 等,用以绑定表单字段的属性值,它们的共有属性如下:
– path:表单字段,对应 html 元素的 name 属性,支持级联属性
– htmlEscape:是否对表单值的 HTML 特殊字符进行转换,默认值为 true
– cssClass:表单组件对应的 CSS 样式类名
– cssErrorClass:表单组件的数据存在错误时,采取的 CSS 样式2)form:input、form:password、form:hidden、form:textarea:对应 HTML 表单的 text、password、hidden、textarea标签
3)form:radiobutton:单选框组件标签,当表单 bean 对应的属性值和 value 值相等时,单选框被选中
4)form:radiobuttons:单选框组标签,用于构造多个单选框
– items:可以是一个 List、String[] 或 Map
– itemValue:指定 radio 的 value 值。可以是集合中 bean 的一个属性值
– itemLabel:指定 radio 的 label 值
– delimiter:多个单选框可以通过 delimiter 指定分隔符5) form:checkbox:复选框组件。用于构造单个复选框
6) form:checkboxs:用于构造多个复选框。使用方式同form:radiobuttons 标签
7) form:select:用于构造下拉框组件。使用方式同form:radiobuttons 标签
8) form:option:下拉框选项组件标签。使用方式同form:radiobuttons 标签
9) form:errors:显示表单组件或数据校验所对应的错误
– <form:errors path= “ *” /> :显示表单所有的错误
– <form:errors path= “ user*” /> :显示所有以 user 为前缀的属性对应的错误
– <form:errors path= “ username” /> :显示特定表单对象属性的错误