MVC设计模式
controller:负责接收并处理请求,响应客户端
Model:处理具体的业务逻辑,产生不同的模型数据
View:呈现模型,与用户进行交互
SpringMVC
SpringMVC 是 Spring 框架的一个后续产品,是目前最好的实现MVC设计模式的框架。
SpringMVC核心组件
- 1.DispatcherServlet:前置控制器,控制其他组件的执行,进行统一调度,降低组件之间的耦合度。
- 2.HandlerMapping:将请求映射到Handler
- 3.Handler:后端控制器,完成具体业务逻辑
- 4.HandlerInterceptor:处理器拦截器,如果需要的话可以实现这个接口完成一些拦截处理
- 5.HandlerExecutionChain:处理器执行链
- 6.HandlerAdapter:处理器适配器(完成Handler在执行业务之前需要将表单数据进行处理,包括表单数据的验证,表单数据类型的转换,将提交的表单数据封装的JavaBean中等一系列操作)
- 7.ModelAndView:装载模型数据和视图信息
- 8.ViewResolver:视图解析器
SpringMVC实现原理
- 1.客户端请求被DispatcherServlet接收
- 2.DispatcherServlet将请求映射到Handler
- 3.生成Handler以及HandlerInterceptor
- 4.返回HandlerExecutionChain(包含:Handler+HandlerInterceptor)
- 5.DispatcherServlet拿到HandlerInterceptor后通过HandlerAdapter执行Handler具体的业务方法
- 6.返回一个ModelAndView
- 7.DispatcherServlet拿到逻辑视图和模型数据后通过ViewResolver进行解析
-
8.返回填充了模型数据的View,响应给客户端
SpringMVC基于XML配置的使用
导入springmvc需要的jar
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<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">
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springmvc.xml的路径-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
springmvc.xml 配置Controller、HandlerMapping组件映射(基于注解使用则不需要这一步)
<!--配置HandlerMApping,将url请求映射到Handler-->
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!--配置mapping-->
<property name="mappings">
<props>
<!--配置test请求对应的Handler-->
<prop key="test" >testHandler</prop>
</props>
</property>
</bean>
<!--配置Handler-->
<bean id="testHandler" class="com.controller.XMLHandler"></bean>
springmvc.xml 配置ViewResolver组件映射
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置前缀-->
<property name="prefix" value="/"></property>
<!--配置后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
控制层:XMLHandler.java
public class XMLHandler implements Controller{
@Override
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
// 装载模型数据和逻辑视图
ModelAndView modelAndView = new ModelAndView();
// 添加模型数据
modelAndView.addObject("name","梅西");
// 添加逻辑视图
modelAndView.setViewName("show");
return modelAndView;
}
}
视图层:show.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${name}
</body>
</html>
SpringMVC基于注解的使用
基于注解的方式与XML配置的方式的区别在于Controller、HandlerMapping是通过注解进行映射的,不需要在XML文件中去配置这两个bean,这样就方便了很多,一般我们都采用注解的方式。
Springmvc.xml将AnnotationHandler自动扫描到IOC容器中
<?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.xsd">
<!-- 将AnnotationHandler自动扫描到IOC容器中 -->
<context:component-scan base-package="com.controller"></context:component-scan>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置前缀-->
<property name="prefix" value="/"></property>
<!--配置后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
控制层:使用@Controller和@RequestMapping注解
@Controller
public class AnnotationHandler {
/**方法一: ModelAndView 完成数据传递、视图解析**/
@RequestMapping("/modelAndViewTest")
public ModelAndView modelAndViewTest(){
// 装载模型数据和逻辑视图
ModelAndView modelAndView = new ModelAndView();
// 添加模型数据
modelAndView.addObject("name","C罗");
// 添加逻辑视图
modelAndView.setViewName("show");
return modelAndView;
}
/**方法二: Model传值,返回String类型的字符串进行视图解析**/
@RequestMapping("/modelTest")
public String modelTest(Model model){
// 添加模型数据
model.addAttribute("name","内马尔");
// 返回逻辑视图
return "show";
}
/**方法三: Map传值,返回String类型的字符串进行视图解析**/
@RequestMapping("/mapTest")
public String mapTest(Map<String,String> map){
// 添加模型数据
map.put("name","厄齐尔");
// 返回逻辑视图
return "show";
}
}
Restful风格概述
REST并不是一种新技术,它指的是一组架构约束条件和原则,符合REST的约束条件和原则的架构,就称之为RESTFul架构。RESTful只是一种软件设计风格。
RESTful架构的特点
- 1.统一了客户端访问资源的接口
- 2.url更加简洁,易于理解,便于扩展
- 3.有利于不同系统之间的资源共享
举个栗子
pom.xml
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
web.xml(Maven构建项目的时候采用的是JavaEE5.0版本,右键项目新建一个Module,选择JavaEE7,Web Application选择3.1,然后把web.xml复制过来)
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--处理中文乱码-->
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springmvc.xml的路径-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--过滤静态资源-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
<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>
</web-app>
public class Course {
private int id;
private String name;
private double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
@Repository
public class CourseDAO {
private Map<Integer,Course> courseMap = new HashMap<Integer, Course>();
public void add(Course course){
courseMap.put(course.getId(),course);
}
public Collection<Course> findAll(){
return courseMap.values();
}
public Course findById(int id){
return courseMap.get(id);
}
public void updae(Course course){
courseMap.put(course.getId(),course);
}
public void delete(int id){
courseMap.remove(id);
}
}
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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<mvc:annotation-driven >
<!-- 消息转换器 -->
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 将AnnotationHandler自动扫描到IOC容器中 -->
<context:component-scan base-package="com"></context:component-scan>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--配置前缀-->
<property name="prefix" value="/"></property>
<!--配置后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
@Controller
public class CourseController {
@Autowired
private CourseDAO courseDAO;
@PostMapping(value = "/add")
public String add(Course course){
courseDAO.add(course);
return "redirect:/getAll";
}
@GetMapping(value = "/getAll")
public ModelAndView findAll(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("courses",courseDAO.findAll());
modelAndView.setViewName("index");
return modelAndView;
}
@GetMapping(value = "/findById/{id}")
public ModelAndView findById(@PathVariable(value = "id") int id){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("course",courseDAO.findById(id));
modelAndView.setViewName("edit");
return modelAndView;
}
@PutMapping(value = "/update")
public String update(Course course){
courseDAO.updae(course);
return "redirect:/getAll";
}
@DeleteMapping(value = "/delete/{id}")
public String delete(@PathVariable(value = "id") int id){
courseDAO.delete(id);
return "redirect:/getAll";
}
}
add.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>add</title>
<link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body{
overflow-x:hidden;
}
#main{
width:1200px;
height:600px;
margin-left:500px;
}
</style>
</head>
<body>
<div id="main">
<div class="row">
<div class="col-md-12">
<h1>添加课程</h1>
</div>
</div>
<form class="form-horizontal" role="form" action="${pageContext.request.contextPath}/add" method="post">
<div class="form-group">
<label class="col-sm-1 control-label">课程编号</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="id" placeholder="请输入课程编号">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程名称</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="name" placeholder="请输入课程名称">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程价格</label>
<div class="col-sm-3">
<input type="text" class="form-control" name="price" placeholder="请输入课程价格">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-3">
<button type="submit" class="btn btn-default">提交</button>
</div>
</div>
</form>
</div>
</body>
</html>
edit.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<html>
<head>
<title>add</title>
<link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body{
overflow-x:hidden;
}
#main{
width:1200px;
height:600px;
margin-left:500px;
}
</style>
</head>
<body>
<div id="main">
<div class="row">
<div class="col-md-12">
<h1>修改课程</h1>
</div>
</div>
<form class="form-horizontal" role="form" action="${pageContext.request.contextPath}/update" method="post">
<div class="form-group">
<label class="col-sm-1 control-label">课程编号</label>
<div class="col-sm-3">
<input type="text" value="${course.id}" name="id" readonly="readonly" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程名称</label>
<div class="col-sm-3">
<input type="text" value="${course.name}" name="name" class="form-control">
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">课程价格</label>
<div class="col-sm-3">
<input type="text" value="${course.price}" name="price" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-3">
<input type="hidden" name="_method" value="PUT"/>
<button type="submit" class="btn btn-default">提交</button>
</div>
</div>
</form>
</div>
</body>
</html>
indext.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>课程列表</title>
<link href="${pageContext.request.contextPath}/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>课程管理</h1>
</div>
</div>
<!-- 显示表格数据 -->
<div class="row">
<div class="col-md-12">
<table class="table table-hover" id="emps_table">
<thead>
<tr>
<th>
<input type="checkbox" id="check_all"/>
</th>
<th>编号</th>
<th>课程名</th>
<th>价格</th>
<th>编辑</th>
<th>删除</th>
</tr>
</thead>
<tbody>
<c:forEach items="${courses}" var="course">
<tr>
<td><input type='checkbox' class='check_item'/></td>
<td>${course.id}</td>
<td>${course.name}</td>
<td>${course.price}</td>
<td>
<form action="${pageContext.request.contextPath}/findById/${course.id}" method="get">
<button class="btn btn-primary btn-sm edit_btn" type="submit">
<span class="glyphicon glyphicon-pencil">编辑</span>
</button>
</form>
</td>
<td>
<form action="${pageContext.request.contextPath}/delete/${course.id}" method="post">
<button class="btn btn-danger btn-sm delete_btn" type="submit">
<input type="hidden" name="_method" value="DELETE"/>
<span class="glyphicon glyphicon-trash">删除</span>
</button>
</form>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>