SpringMVC_RESTRUL_CRUD_显示所有员工信息
截图:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <!-- 配置org.springframework.web.filter.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> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置 DispatcherServlet的一个初始化参数:配置SpringMvc 配置文件的位置和名称--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <!-- 设置启动 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <!-- 请求处理 --> <servlet-name>springDispatcherServlet</servlet-name> <!-- /:应答所有请求 --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-mvc-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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 配置自定义扫描包 --> <context:component-scan base-package="como.springmvc.handlers"></context:component-scan> <!-- 配置视图解析器:如何把handler方法返回值解析为实际的物理视图 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- spring中加入jstl标签库 --> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <!-- 前缀 --> <property name="prefix" value="/WEB-INF/views/"></property> <!-- 后缀 --> <property name="suffix" value=".jsp"></property> </bean> </bean> </beans>
实体类:
Department.java
package como.springmvc.handlers.entity; public class Department { private int id; private String departementName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDepartementName() { return departementName; } public void setDepartementName(String departementName) { this.departementName = departementName; } public Department(int id, String departementName) { super(); this.id = id; this.departementName = departementName; } public Department() { super(); } }
Employee.java
package como.springmvc.handlers.entity; public class Employee { private int id; private String lastName; private String email; private int gender; private Department department; 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 getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getGender() { return gender; } public void setGender(int gender) { this.gender = gender; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } public Employee(int id, String lastName, String email, int gender, Department department) { super(); this.id = id; this.lastName = lastName; this.email = email; this.gender = gender; this.department = department; } public Employee() { super(); } }
Dao层代码:
DepartmentDao.java
package como.springmvc.handlers.dao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Repository;
import como.springmvc.handlers.entity.Department;
@Repository//@Repository用于标注数据访问组件,即DAO组件
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"));
}
//获取全部的departments
public Collection<Department> getDepartments(){
return departments.values();
}
//获取指定id的department
public Department getDepartment(Integer id){
return departments.get(id);
}
}
Employee.java
package como.springmvc.handlers.dao; import java.util.Collection; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import como.springmvc.handlers.entity.Department; import como.springmvc.handlers.entity.Employee; @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","aa@123",1, new Department(101, "D-AA"))); employees.put(1002, new Employee(1002,"E-BB","bb@123",0, new Department(102, "D-BB"))); employees.put(1003, new Employee(1003,"E-CC","cc@123",1, new Department(103, "D-CC"))); employees.put(1004, new Employee(1004,"E-DD","dd@123",1, new Department(104, "D-DD"))); employees.put(1005, new Employee(1005,"E-EE","ee@123",0, new Department(105, "D-EE"))); } public Collection<Employee> getAll(){ return employees.values(); } }
视图层代码:
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>
</title> </head> <body> <a href="emps">list all Employees</a> </body> </html>
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘list.jsp‘ starting page</title> </head> <body> <c:if test="${empty requestScope.employees }"> no information </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>Gender</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.gender==0?‘female‘:‘male‘}</td> <td>${emp.department.departementName}</td> <td><a href="">Edit</a></td> <td><a href="">Delete</a></td> </tr> </c:forEach> </table> </c:if> </body> </html>