一、新建一个Dynamic Web Project(一直Next到最后,勾选Generate...,否则不会有web.xml文件)。
二、在lib文件夹中加入相应jar包
三、在web.xml中配置
xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>springDispatcherServletservlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class> <init-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:springmvc.xmlparam-value> init-param> <load-on-startup>1load-on-startup> servlet> <servlet-mapping> <servlet-name>springDispatcherServletservlet-name> <url-pattern>/url-pattern> servlet-mapping> <filter> <filter-name>HiddenHttpMethodFilterfilter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class> filter> <filter-mapping> <filter-name>HiddenHttpMethodFilterfilter-name> <url-pattern>/*url-pattern> filter-mapping> web-app>
四、在src目录下新建一个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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.gong.springmvc">context:component-scan> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/">property> <property name="suffix" value=".jsp">property> bean> beans>
五、在src目录下新建com.gong.springmvc.curd.entties、com.gong.springmvc.curd.dao、com.gong.springmvc.curd.handlers包
1.在com.gong.springmvc.curd.entties中新建Departmen.java和Employee.java
Department.java
package com.gong.springmvc.crud.entities; public class Department { private Integer id; private String departmentName; public Department() { // TODO Auto-generated constructor stub } public Department(int i, String string) { this.id = i; this.departmentName = string; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } @Override public String toString() { return "Department [id=" + id + ", departmentName=" + departmentName + "]"; } }
Employee.java
package com.gong.springmvc.crud.entities; import java.util.Date;public class Employee { private Integer id; private String lastName; private String email; //1 male, 0 female private Integer gender; private Department department; private Date birth; private Float salary; public Integer getId() { return id; } public void setId(Integer 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 Integer getGender() { return gender; } public void setGender(Integer gender) { this.gender = gender; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public Float getSalary() { return salary; } public void setSalary(Float salary) { this.salary = salary; } @Override public String toString() { return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + ", department=" + department + ", birth=" + birth + ", salary=" + salary + "]"; } public Employee(Integer id, String lastName, String email, Integer gender, Department department) { super(); this.id = id; this.lastName = lastName; this.email = email; this.gender = gender; this.department = department; } public Employee() { // TODO Auto-generated constructor stub } }
2.在com.gong.springmvc.curd.dao中新建DepartmentDao.java和EmployeeDao.java
DepartmentDao.java
package com.gong.springmvc.crud.dao; import java.util.Collection; import java.util.HashMap; import java.util.Map; import org.springframework.stereotype.Repository; import com.gong.springmvc.crud.entities.Department; @Repository public class DepartmentDao { private static Mapdepartments = null; static{ departments = new HashMap (); 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 getDepartments(){ return departments.values(); } public Department getDepartment(Integer id){ return departments.get(id); } }
EmployeeDao.java
package com.gong.springmvc.crud.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 com.gong.springmvc.crud.entities.Department; import com.gong.springmvc.crud.entities.Employee; @Repository public class EmployeeDao { private static Mapemployees = null; @Autowired private DepartmentDao departmentDao; static{ employees = new HashMap (); employees.put(1001, new Employee(1001, "E-AA", "aa@163.com", 1, new Department(101, "D-AA"))); employees.put(1002, new Employee(1002, "E-BB", "bb@163.com", 1, new Department(102, "D-BB"))); employees.put(1003, new Employee(1003, "E-CC", "cc@163.com", 0, new Department(103, "D-CC"))); employees.put(1004, new Employee(1004, "E-DD", "dd@163.com", 0, new Department(104, "D-DD"))); employees.put(1005, new Employee(1005, "E-EE", "ee@163.com", 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 Collection getAll(){ return employees.values(); } public Employee get(Integer id){ return employees.get(id); } public void delete(Integer id){ employees.remove(id); } }
只是为了理解springmvc,就直接模拟数据库中的数据。
3.在com,gong.springmvc.curd.handlers中新建一个EmployeeHandler.java
EmployeeHandler.java
package com.gong.springmvc.crud.handlers; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.gong.springmvc.crud.dao.DepartmentDao; import com.gong.springmvc.crud.dao.EmployeeDao; @Controller public class EmployeeHandler { @Autowired private EmployeeDao employeeDao; @Autowired private DepartmentDao departmentDao; @RequestMapping("/emps") public String list(Mapmap) { map.put("employees", employeeDao.getAll()); return "list"; } }
六、jsp页面设置
1.在WebCont下新建一个index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 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=UTF-8"> <title>Insert title heretitle> head> <body> <a href="emps">testLista> body> html>
该jsp中的href="emps"对应着EmployeeHandler.java中的list方法,该方法最后返回/WEB-INF/views/list.jsp。
在WEB-INF下新建一个views文件夹,在该文件夹下新建一个list.jsp
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 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=UTF-8"> <title>Insert title heretitle> 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>IDth> <th>LastNameth> <th>Emailth> <th>Genderth> <th>Departmentth> <th>Editth> <th>Deleteth> 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.departmentName }td> <td><a href="emp/${emp.id}">Edita>td> <td><a class="delete" href="emp/${emp.id}">Deletea>td> tr> c:forEach> table> c:if> <br><br> body> html>
七、启动服务器之后
点击testList:
成功发送请求,并显示相关雇员的信息列表。
springmvc实例之显示雇员相关信息(一)