ID | LastName | Gender | Department | Edit | Delete | |
---|---|---|---|---|---|---|
1001 | AA | [email protected] | Male | AA | Edit |
Delete |
1002 | CC | [email protected] | Female | CC | Edit |
Delete |
1003 | BB | [email protected] | Male | BB | Edit |
Delete |
1004 | EE | [email protected] | Male | EE | Edit |
Delete |
1005 | DD | [email protected] | Female | DD | Edit |
Delete |
ID | LastName | Gender | Department | Edit | Delete | |
---|---|---|---|---|---|---|
1001 | AA | [email protected] | Male | AA | Edit |
Delete |
1002 | CC | [email protected] | Female | CC | Edit |
Delete |
1003 | BB | [email protected] | Male | BB | Edit |
Delete |
1004 | EE | [email protected] | Male | EE | Edit |
Delete |
1005 | DD | [email protected] | Female | DD | Edit |
Delete |
1006 | FF | [email protected] | Male | FF | Edit |
Delete |
Employee | Department |
---|---|
id | id |
lastName | departmentName |
gender |
.html
或.do
等后缀/
,则SpringMVC将捕获WEB容器的所有请求,包括静态资源的请求,SpringMVC会将他们当成一个普通请求处理,因找不到对应处理器将导致错误
的方式解决静态资源的问题:
将在SpringMVC上下文中定义一个DefaultServletHttpRequestHandler,它会对进入DispatcherServlet的请求进行筛查,如果发现是没有经过映射的请求,就将该请求交友WEB应用服务器默认的Servlet处理,如果不是静态资源的请求,才由DispatcherServlet继续处理
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.luckyazrael.springmvc.crud">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>
<mvc:default-servlet-handler/>
<mvc:annotation-driven>mvc:annotation-driven>
beans>
<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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>springDispatchServletservlet-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>springDispatchServletservlet-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>
见上方表格
省略了与数据库交互的内容,重点不在数据库操作,so…从简
@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")));
}
/**
* 若新增员工,该员工的id为initId
*/
private static Integer initId = 1006;
/**
* 新增员工信息
* @param employee
*/
public void save(Employee employee){
if (employee.getId() == null){
employee.setId(initId++);
}
employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
employees.put(employee.getId(),employee);
}
/**
* 获取(查询)所有员工信息
* @return
*/
public Collection<Employee> getAll(){
return employees.values();
}
/**
* 根据员工id获取(查询)员工的信息
* @param id
* @return
*/
public Employee get(Integer id){
return employees.get(id);
}
/**
* 根据员工id删除员工信息
* @param id
*/
public void delete(Integer id){
employees.remove(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"));
}
/**
* 获取全部的Departments
* @return
*/
public Collection<Department> getDepartments(){
return departments.values();
}
/**
* 根据id获取对应的Department
* @param id
* @return
*/
public Department getDepartment(Integer id){
return departments.get(id);
}
}
<body>
<a href="emps">List All Employeesa>
body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>listtitle>
<%--
SpringMVC 处理静态资源:
1. 为什么会有这样的问题:
优雅的REST风格的资源URL不希望带 .html 或 .do 等后缀
若将DispatcherServlet请求映射配置为 / ,
则SpringMVC将捕获WEB容器的所有请求,包括静态资源的请求,SpringMVC会将他们当成一个普通请求处理,
因找不到对应处理器将导致错误
2. 解决:在SpringMVC的配置文件中配置<mvc:default-servlet-handler/>
--%>
<script type="text/javascript" src="script/jquery-1.7.2.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>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>
<a href="emp">Add New Employeea>
body>
html>
使用Spring的表单标签
form标签
表单标签
、
等,用以绑定表单字段的属性值,他们的共有属性如下:
form:input
、form:password
、form:hidden
、form:textarea
:对应HTML表单的text、password、hidden、textarea标签form:radiobutton
:单选框组件标签,当表单bean对应的属性值和value值相等时,单选框被选中form:radiobuttons
:单选框组标签,用于构造多个单选框
form:checkbox
:复选框组件。用于构造单个复选框form:checkboxs
:用于构造多个复选框。使用方法同form:radiobuttons
标签form:select
:用于构造下拉框组件。使用方法同form:radiobuttons
标签form:option
:下拉框选项组件标签。使用方法同form:radiobuttons
标签form:errors
:显示表单组件或数据校验所对应的错误
:显示表单所有的错误
:显示所有以user为前缀的属性对应的错误
:显示特定表单对象属性的错误<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>inputtitle>
head>
<body>
<%--
1. 为什么要使用SpringMVC的form标签?
可以更快速的开发出表单页面,而且可以更方便的进行表单值的回显
2. 注意:可以通过 modelAttribute 属性指定绑定的模型属性,
若没有指定该属性,则默认从request域对象中读取command的表单bean,
如果该属性值也不存在,则会发生错误。
--%>
<form:form action="${pageContext.request.contextPath}/emp" method="POST" modelAttribute="employee">
<%-- path属性对应html 表单标签中的name属性值 --%>
<c:if test="${employee.id == null}">
LastName:<form:input path="lastName" />
c:if>
<c:if test="${employee.id != null}">
<form:hidden path="id"/>
<%--对于_method 不能使用form:hidden标签,因为modelAttribute 对应的bean中没有_method 这个属性--%>
<%-- <form:hidden path="_method" value="PUT" --%>
<input type="hidden" name="_method" value="PUT"/>
c:if>
<br>
Email:<form:input path="email" /> <br>
<%
Map<String,String> genders = new HashMap<>();
genders.put("0","Female");
genders.put("1","Male");
request.setAttribute("genders",genders);
%>
Gender:<form:radiobuttons path="gender" items="${genders}"/> <br>
Department:<form:select path="department.id" items="${departments}" itemLabel="departmentName" itemValue="id"/><br>
<input type="submit" value="Submit"/>
form:form>
body>
html>
@Controller
public class EmployeeHandler {
@Autowired
private EmployeeDao employeeDao;
@Autowired
private DepartmentDao departmentDao;
@ModelAttribute
public void getEmployee(@RequestParam(value = "id",required = false) Integer id,Map<String,Object> map){
if (id != null){
map.put("employee",employeeDao.get(id));
}
}
@RequestMapping(value = "/emp",method = RequestMethod.PUT)
public String update(Employee employee){
employeeDao.save(employee);
return "redirect:/emps";
}
@RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
public String input(@PathVariable("id") Integer id,Map<String,Object> map){
map.put("employee",employeeDao.get(id));
map.put("departments",departmentDao.getDepartments());
return "input";
}
/**
* 执行删除的操作
* @param id
* @return
*/
@RequestMapping(value = "/emp/{id}",method = RequestMethod.DELETE)
public String delete(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/emps";
}
/**
* 执行添加操作并重定向到list页面
* @param employee
* @return
*/
@RequestMapping(value = "/emp",method = RequestMethod.POST)
public String save(Employee employee){
employeeDao.save(employee);
return "redirect:/emps";
}
/**
* 跳转到添加员工信息的操作页面
* @param map
* @return
*/
@RequestMapping(value = "/emp",method = RequestMethod.GET)
public String input(Map<String,Object> map){
map.put("departments",departmentDao.getDepartments());
map.put("employee",new Employee());
return "input";
}
/**
* 跳转到list页面,并显示所有的员工信息
* @param map
* @return
*/
@RequestMapping("/emps")
public String list(Map<String,Object> map){
map.put("employees",employeeDao.getAll());
return "list";
}
}