<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringMVC_CRUD</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</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>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 字符编码 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*
HiddenHttpMethodFilter
org.springframework.web.filter.HiddenHttpMethodFilter
HiddenHttpMethodFilter
/*
:springmvc.xml 增加context,mvc,beans名称空间。
```java
<?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.3.xsd">
<context:component-scan base-package="com.atguigu"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
/WEB-INF/views/list.jsp
index.jsp
⑥增加实体类
package com.atguigu.dao;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.atguigu.entities.Department;
@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);
}
}
package com.atguigu.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.atguigu.entities.Department;
import com.atguigu.entities.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", "[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 Collection<Employee> getAll(){
return employees.values();
}
public Employee get(Integer id){
return employees.get(id);
}
public void delete(Integer id){
employees.remove(id);
}
}
员工列表展示:查询所有员工:
访问index.jsp-------->发送一个/emps请求----------->控制器查询所有员工--------
放在请求域中---------------转发到list页面
<jsp:forward page="/emps"></jsp:forward>
@Controller
public class EmployeeController {
@Autowired
private EmployeeDao employeeDao;
@Autowired
private DepartmentDao departmentDao;
@RequestMapping("/emps")
public String getEmps(Model model) {
Collection<Employee> all = employeeDao.getAll();
model.addAttribute("emps",all );
return "/list";
}
jstl标签foreach循环遍历
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
jstl标签foreach循环遍历
<%@ 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>信息显示</title>
</head>
<body>
<h1>员工信息</h1>
<table border="1" cellpadding="5" cellspacing="0" >
<tr>
<th>id</th>
<th>lastName</th>
<th>email</th>
<th>gender</th>
<th>departmentName</th>
<th>EDIT</th>
<th>DELETE</th>
</tr>
<c:forEach items="${emps }" 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>
<a href="add01"> 添加</a>
</body>
</html>
id | lastName | gender | departmentName | EDIT | DELETE | |
---|---|---|---|---|---|---|
${emp.id } | ${emp.lastName } | ${emp.email } | ${emp.gender==0?"Female":"Male" } | ${emp.department.departmentName } | Edit | Delete |
在list页面点击“员工添加” --------------来到添加页面-------------输入数据---------------保存请求(保存)--------------保存完成后回到页表
1.先跳转到添加
@RequestMapping("/add01")
public String hello02(Model model) {
Collection<Department> departments = departmentDao.getDepartments();
model.addAttribute("depts",departments);
model.addAttribute("employee", new Employee());
return "/add";
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!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>员工添加</title>
</head>
<body>
<%
pageContext.setAttribute("ctp", request.getContextPath());
%>
用springmvc自带的标签
<form:form action="${ctp }/emp" modelAttribute="employee" method="POST">
lastName<form:input path="lastName"/><br/>
email<form:input path="email"/><br/>
gender:<br/>
男<form:radiobutton path="gender" value="1"/>
女<form:radiobutton path="gender" value="0"/>
dept
<!--
items="",指定要遍历的集合,自动遍历,遍历出的每一个元素都是一个department对象
itemLabel=”属性,指定出这个对象的哪个属性时作为option的
itemValue=”属性,指定刚才遍历出来的这个对象时哪个属性作为提交的值
-->
<form:select path="department.id" items="${depts }"
itemValue="id" itemLabel="departmentName"
></form:select>
<input type="submit" value="保存">
</form:form>
用jstl标签遍历
<%-- <form action="">
id: <input type="text" name="id"><br>
lastName <input type="text" name="lastName"><br>
email <input type="text" name="email"><br>
gender<br>
男<input type="radio" name="gender" value="1"><br>
女<input type="radio" name="gender" value="0"><br>
departmentName
<select name="department.id">
<c:forEach items="${depts}" var="departItem">
<option value="${departItem.id }">${departItem.departmentName }</option>
</c:forEach>
</select><br>
<input type="submit" value="提交"><br>
</form> --%>
</body>
</html>
提交表单后:
@RequestMapping(value="/emp",method=RequestMethod.POST)
public String addctp(Employee employee) {
employeeDao.save(employee);
System.out.println("新增用户"+employee);
return "redirect:/emps";
}
再次重定向到表单遍历
先点击修改跳转到修改页面,(要将修改的id用获取到,然后通过id查找到这条id的信息,显示到页面,之后将其修改,修改时将id也保存到后面修改的页面中,不能为null ,用modleAttribute获取id,得到id,并发送给修改后回显表单的方法中
// 回显修改的信息
@RequestMapping(value="/emp/{id}", method=RequestMethod.GET)
public String update(@PathVariable("id")Integer id,Model model) {
Employee employee = employeeDao.get(id);
model.addAttribute("employee", employee);
Collection<Department> departments = departmentDao.getDepartments();
model.addAttribute("depts", departments);
return "update";
}
// 修改后保存
@RequestMapping(value="/emp/{id}",method=RequestMethod.PUT)
public String updateemp(@ModelAttribute("employee")Employee employee) {
employeeDao.save(employee);
return "redirect:/emps";
}
@ModelAttribute
public void myModel(@RequestParam(value="id",required = false) Integer id,Model model) {
if(id!=null){
Employee employee = employeeDao.get(id);
model.addAttribute("employee",employee);
}
}
``
```jsp
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!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>修改信息</title>
<% pageContext.setAttribute("ctp", request.getContextPath()); %>
</head>
<body>
<h1>修改信息</h1>
<form:form action="${ctp }/emp/${employee.id }" modelAttribute="employee" method="POST">
<input type="hidden" name="_method" value="put">
<input type="hidden" name="_method" value="${employee.id }">
lastName<form:input path="lastName"/><br/>
email<form:input path="email"/><br/>
gender:<br/>
男<form:radiobutton path="gender" value="1"/>
女<form:radiobutton path="gender" value="0"/>
dept:
<form:select path="department.id" items="${depts }" itemLabel="departmentName"
itemValue="id"><br>
</form:select>
<input type="submit" value="修改"></form:form>
删除时获取id ,然后通过id删除
@RequestMapping(value="/emp1/{id}")
public String deleteemp(@PathVariable("id")Integer id) {
employeeDao.delete(id);
return "redirect:/emps";
}
<td><a href="${ctp }/emp1/${emp.id}">Delete</a></td>