【SpringMVC--全面版】

SpringMVC

SpringMVC的架构

  • 表现层框架
  • 是Spring框架的一部分

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0uT1wsMQ-1589804439758)(C:\Users\dby\AppData\Roaming\Typora\typora-user-images\1578286176899.png)]

SpringMVC处理流程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3JKSnI45-1589804439761)(C:\Users\dby\AppData\Roaming\Typora\typora-user-images\1578286821915.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QjAWZsBa-1589804439764)(C:\Users\dby\AppData\Roaming\Typora\typora-user-images\1578288939054.png)]

三大组件和两个开发的部分

  • DispatcherServlet

    • 用户请求到达前端控制器,它就相当于mvc模式中的c,dispatcherServlet是整个流程控制的中心,由它调用其它组件处理用户的请求,dispatcherServlet的存在降低了组件之间的耦合性
  • HandlerMapping

    • HandlerMapping负责根据用户请求url找到Handler即处理器,springmvc提供了不同的映射器实现不同的映射方式,例如:配置文件方式,实现接口方式,注解方式等。
  • HandlerAdapter

    • 通过HandlerAdapter对处理器进行执行,这是适配器模式的应用,通过扩展适配器可以对更多类型的处理器进行执行。

      下图是许多不同的适配器,最终都可以使用usb接口连接

  • Handler

    • Handler 是继DispatcherServlet前端控制器的后端控制器,在DispatcherServlet的控制下Handler对具体的用户请求进行处理

      由于Handler涉及到具体的用户业务请求,所以一般情况需要程序员根据业务需求开发Handler。

  • ViewResolver

    • View Resolver负责将处理结果生成View视图,View Resolver首先根据逻辑视图名解析成物理视图名即具体的页面地址,再生成View视图对象,最后对View进行渲染将处理结果通过页面展示给用户
  • View

    • springmvc框架提供了很多的View视图类型的支持,包括:jstlView、freemarkerView、pdfView等。我们最常用的视图就是jsp

      一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户,需要由程序员根据业务需求开发具体的页面。

默认加载的组件

org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,\org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,\org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,\org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver\

处理器映射器和适配器


<bean
	class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />

<bean
	class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />

以上两行可以变成


<mvc:annotation-driven />

视图解析器

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		
		<property name="prefix" value="/WEB-INF/jsp/" />
		
		<property name="suffix" value=".jsp" />
	bean>

入门程序

  • springmvc.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	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-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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	
	<context:component-scan base-package="cn.itheima" />

beans>
  • web.xml

<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-firstdisplay-name>
	<welcome-file-list>
		<welcome-file>index.htmlwelcome-file>
		<welcome-file>index.htmwelcome-file>
		<welcome-file>index.jspwelcome-file>
		<welcome-file>default.htmlwelcome-file>
		<welcome-file>default.htmwelcome-file>
		<welcome-file>default.jspwelcome-file>
	welcome-file-list>

	
	<servlet>
		<servlet-name>springmvc-firstservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		
		
		<init-param>
			<param-name>contextConfigLocationparam-name>
			<param-value>classpath:springmvc.xmlparam-value>
		init-param>
	servlet>

	<servlet-mapping>
		<servlet-name>springmvc-firstservlet-name>
		
		<url-pattern>*.actionurl-pattern>
	servlet-mapping>
web-app>
  • itemController
@Controller
public class ItemController {

	// @RequestMapping:里面放的是请求的url,和用户请求的url进行匹配
	// action可以写也可以不写
	@RequestMapping("/itemList.action")
	public ModelAndView queryItemList() {
		// 创建页面需要显示的商品数据
		List<Item> list = new ArrayList<>();
		list.add(new Item(1, "1华为 荣耀8", 2399, new Date(), "质量好!1"));
		list.add(new Item(2, "2华为 荣耀8", 2399, new Date(), "质量好!2"));
		list.add(new Item(3, "3华为 荣耀8", 2399, new Date(), "质量好!3"));
		list.add(new Item(4, "4华为 荣耀8", 2399, new Date(), "质量好!4"));
		list.add(new Item(5, "5华为 荣耀8", 2399, new Date(), "质量好!5"));
		list.add(new Item(6, "6华为 荣耀8", 2399, new Date(), "质量好!6"));

		// 创建ModelAndView,用来存放数据和视图
		ModelAndView modelAndView = new ModelAndView();
		// 设置数据到模型中
		modelAndView.addObject("list", list);
		// 设置视图jsp,需要设置视图的物理地址
		modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");

		return modelAndView;
	}
}

SSM整合

sqlMapConfig.xml



<configuration>
	
	<typeAliases>
		
		<package name="com.itheima.springmvc.pojo" />
	typeAliases>


configuration>

applicationContext-dao.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	
	<context:property-placeholder location="classpath:db.properties" />

	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10" />
		<property name="maxIdle" value="5" />
	bean>

	
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		
		<property name="dataSource" ref="dataSource" />
		
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
	bean>

	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		
		<property name="basePackage" value="cn.itcast.ssm.mapper" />
	bean>

beans>

applicationContext-trans.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		
		<property name="dataSource" ref="dataSource" />
	bean>

	
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
		tx:attributes>
	tx:advice>

	
	<aop:config>
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* cn.itcast.ssm.service.*.*(..))" />
	aop:config>
    
    
    
    
	<tx:annotation-driven transaction-manager="transactionManager">tx:annotation-driven>

beans>

springmvc.xml


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	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-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/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	
	<context:component-scan base-package="cn.itcast.ssm.controller" />

	
	<mvc:annotation-driven />

	
	
	<bean
	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		
		<property name="prefix" value="/WEB-INF/jsp/" />
		
		<property name="suffix" value=".jsp" />
	bean>

beans>

web.xml


<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-webdisplay-name>
	<welcome-file-list>
		<welcome-file>index.htmlwelcome-file>
		<welcome-file>index.htmwelcome-file>
		<welcome-file>index.jspwelcome-file>
		<welcome-file>default.htmlwelcome-file>
		<welcome-file>default.htmwelcome-file>
		<welcome-file>default.jspwelcome-file>
	welcome-file-list>

	
	<context-param>
		<param-name>contextConfigLocationparam-name>
		<param-value>classpath:spring/applicationContext*.xmlparam-value>
	context-param>

	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
	listener>

	
	<servlet>
		<servlet-name>springmvc-webservlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		<init-param>
			<param-name>contextConfigLocationparam-name>
			<param-value>classpath:spring/springmvc.xmlparam-value>
		init-param>
	servlet>

	<servlet-mapping>
		<servlet-name>springmvc-webservlet-name>
		
		<url-pattern>*.actionurl-pattern>
	servlet-mapping>

web-app>

ModelAndView参数绑定

默认支持的参数

  • HttpServletRequest

    • 通过request对象获取请求信息
  • HttpServletResponse

    • 通过response处理响应信息
  • HttpSession

    • 通过session对象得到session中存放的对象
  • Model和ModelMap

    • Model是一个接口,在参数里直接声明model即可

      如果使用Model则可以不使用ModelAndView对象,Model对象可以向页面传递数据,View对象则可以使用String返回值替代

servlet时代开发方式

public ModelAndView toEdit(Integer id,
			HttpServletRequest request,HttpServletResponse response
			,HttpSession session,Model model){
		
		//Servlet时代开发
	String id = request.getParameter("id");
	//查询一个商品
//		Items items = itemService.selectItemsById(Integer.parseInt(id));
		Items items = itemService.selectItemsById(id);
		ModelAndView mav = new ModelAndView();
		//数据
		mav.addObject("item", items);
		mav.setViewName("editItem");
		return mav;
	}

绑定简单类型(现在开发方式)

  • Integer id

参数类型推荐使用包装数据类型,因为基础数据类型不可以为null

整形:Integer、int

字符串:String

单精度:Float、float

双精度:Double、double

布尔型:Boolean、boolean

绑定pojo类型(现在开发方式)

  • Items items

pojo对象中的属性名和表单中input的name属性一致。

解决POST乱码

	
	<filter>
		<filter-name>encodingfilter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
		
		<init-param>
			<param-name>encodingparam-name>
			<param-value>UTF-8param-value>
		init-param>
	filter>
	<filter-mapping>
		<filter-name>encodingfilter-name>
		<url-pattern>/*url-pattern>
	filter-mapping>

自定义参数绑定

springmvc没法把字符串转成日期格式

DataConveter

/**
 * 转换日期类型的数据
 * S : 页面传递过来的类型
 * T : 转换后的类型
 * @author lx
 *
 */
public class DateConveter implements Converter<String, Date>{

	public Date convert(String source) {
		// TODO Auto-generated method stub
		try {
			if(null != source){//2016:11-05 11_43-50
				DateFormat df = new SimpleDateFormat("yyyy:MM-dd HH_mm-ss");
				return df.parse(source);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

}

springmvc.xml

 <mvc:annotation-driven conversion-service="conversionServiceFactoryBean"/>
        
        
        <bean id="conversionServiceFactoryBean" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        	
        	<property name="converters">
        		<list>
        			<bean class="com.itheima.springmvc.conversion.DateConveter"/>
        		list>
        	property>
        bean>

绑定数组类型

Integer[] ids

itemList.jsp

<form action="${pageContext.request.contextPath }/queryItem.action" method="post">
查询条件:
<table width="100%" border=1>
<tr>
<td>商品id<input type="text" name="item.id" />td>
<td>商品名称<input type="text" name="item.name" />td>
<td><input type="submit" value="查询"/>td>
tr>
table>
商品列表:
<table width="100%" border=1>
<tr>
	<td>选择td>
	<td>商品名称td>
	<td>商品价格td>
	<td>生产日期td>
	<td>商品描述td>
	<td>操作td>
tr>
<c:forEach items="${itemList }" var="item">
<tr>
	<td><input type="checkbox" name="ids" value="${item.id}"/>td>
	<td>${item.name }td>
	<td>${item.price }td>
	<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>td>
	<td>${item.detail }td>
	
	<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改a>td>

tr>
c:forEach>

table>
form>

ItemController

/**
 * 包装类型 绑定数组类型,可以使用两种方式,pojo的属性接收,和直接接收
 * 
 * @param queryVo
 * @return
 */
@RequestMapping("queryItem")
public String queryItem(QueryVo queryVo, Integer[] ids) {

	System.out.println(queryVo.getItem().getId());
	System.out.println(queryVo.getItem().getName());

	System.out.println(queryVo.getIds().length);
	System.out.println(ids.length);

	return "success";
}

绑定集合类型

ItemController

@RequestMapping(value = "/updates.action",method = {RequestMethod.POST,RequestMethod.GET})
	public ModelAndView updates(QueryVo vo){
		
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("success");
		return mav;
	}

jsp



	
	
		
		
	
	
	"/>
	
	
	修改



Springmvc和Struts2的区别

1、 springmvc的入口是一个servlet即前端控制器,而struts2入口是一个filter过滤器

2、 springmvc是基于方法开发(一个url对应一个方法),请求参数传递到方法的形参,可以设计为单例或多例(建议单例),struts2是基于类开发,传递参数是通过类的属性,只能设计为多例

3、 Struts采用值栈存储请求和响应的数据,通过OGNL存取数据, springmvc通过参数解析器是将request请求内容解析,并给方法形参赋值,将数据和视图封装成ModelAndView对象,最后又将ModelAndView中的模型数据通过request域传输到页面。Jsp视图解析器默认使用jstl。

@RequestMapping

URL路径映射

@RequestMapping(value=“item”)或@RequestMapping("/item")

窄化路径(添加在类上面)

在class上添加@RequestMapping(url)指定通用请求前缀, 限制此类下的所有方法请求url必须以请求前缀开头

请求方法限定

  • 限定GET方法

@RequestMapping(method = RequestMethod.GET)

如果通过POST访问则报错:

HTTP Status 405 - Request method ‘POST’ not supported

例如:

@RequestMapping(value = “itemList”,method = RequestMethod.POST)

  • 限定POST方法

@RequestMapping(method = RequestMethod.POST)

如果通过GET访问则报错:

HTTP Status 405 - Request method ‘GET’ not supported

  • GET和POST都可以

@RequestMapping(method = {RequestMethod.GET,RequestMethod.POST})

Controller中方法换回值

###返回ModelAndView

controller方法中定义ModelAndView对象并返回,对象中可添加model数据、指定view。

返回void

在Controller方法形参上可以定义request和response,使用request或response指定响应结果.Ajax请求合适,json格式数据

使用request转发页面,如下:

request.getRequestDispatcher("页面路径").forward(request, response);

request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);

2、可以通过response页面重定向:

response.sendRedirect("url")

response.sendRedirect("/springmvc-web2/itemEdit.action");

3、可以通过response指定响应结果,例如响应json数据如下:

response.getWriter().print("{\"abc\":123}");

返回String

返回视图路径,model带数据

//指定逻辑视图名,经过视图解析器解析为jsp物理路径:/WEB-INF/jsp/itemList.jsp
return "itemList";

Redirect重定向

return "redirect:/item/itemlist.action";

路径变了

forward内部转发

return "forward:/itemEdit.action";

路径不变

异常处理器

springmvc在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常处理器可以实现一个系统的异常处理逻辑。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dZtfTtST-1589804439767)(file:///C:\Users\dby\AppData\Local\Temp\ksohtml12396\wps1.png)]

package com.itheima.springmvc.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

/**
 * 异常处理器的自定义的实现类
 * @author lx
 *
 */
public class CustomExceptionResolver implements HandlerExceptionResolver{

	public ModelAndView resolveException(HttpServletRequest request, 
			HttpServletResponse response, Object obj,
			Exception e) {
		// TODO Auto-generated method stub  发生异常的地方 Serivce层  方法  包名+类名+方法名(形参) 字符串
		//日志    1.发布 tomcat war  Eclipse  2.发布Tomcat  服务器上  Linux  Log4j
		
		ModelAndView mav = new ModelAndView();
		//判断异常为类型
		if(e instanceof MessageException){
			//预期异常
			MessageException me = (MessageException)e;
			mav.addObject("error", me.getMsg());
		}else{
			mav.addObject("error", "未知异常");
		}
		mav.setViewName("error");
		return mav;
	}

}

<bean 
id="customHandleException" 	class="cn.itcast.ssm.exception.CustomHandleException"/>

错误页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>

	<h1>系统发生异常了!h1>
	<br />
	<h1>异常信息h1>
	<br />
	<h2>${msg }h2>

body>
html>

上传图片

@RequestMapping("updateItem")
public String updateItemById(Item item, MultipartFile pictureFile) throws Exception {
	// 图片上传
	// 设置图片名称,不能重复,可以使用uuid
	String picName = UUID.randomUUID().toString();

	// 获取文件名
	String oriName = pictureFile.getOriginalFilename();
	// 获取图片后缀
	String extName = FilenameUtils.getExtension(pictureFile.getOriginalFilename);

	// 开始上传
	pictureFile.transferTo(new File("C:/upload/image/" + picName +"."+ extName));

	// 设置图片名到商品中
	item.setPic(picName +"."+ extName);
	// ---------------------------------------------
	// 更新商品
	this.itemService.updateItemById(item);

	return "forward:/itemEdit.action";
}
<tr>
				<td>商品图片td>
				<td>
					<c:if test="${item.pic !=null}">
						<img src="/pic/${item.pic}" width=100 height=100/>
						<br/>
					c:if>
					<input type="file"  name="pictureFile"/> 
				td>
			tr>

springmvc.xml


        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        	
        	<property name="maxUploadSize" value="5000000"/>
        bean>

Json数据交互@RequestBody

{
"id": 1,
"name""测试商品",
"price": 99.9,
"detail": "测试商品描述",
"pic": "123456.jpg"
}

@RequestBody注解实现接收http请求的json数据,将json数据转换为java对象进行

/**
 * 测试json的交互
 * @param item
 * @return
 */
@RequestMapping("testJson")
// @ResponseBody
public @ResponseBody Item testJson(@RequestBody Item item) {
	return item;
}

手写json.action

<script type="text/javascript">
$(function(){
	//alert(1);
	var params = '{"id": 1,"name": "测试商品","price": 99.9,"detail": "测试商品描述","pic": "123456.jpg"}';

// 	$.post(url,params,function(data){
		//回调
// 	},"json");//
	$.ajax({
		url : "${pageContext.request.contextPath }/json.action",
		data : params,
		contentType : "application/json;charset=UTF-8",//发送数据的格式
		type : "post",
		dataType : "json",//回调
		success : function(data){
			alert(data.name);
		}
		
	});
});
script>

Restful风格开发

拦截器


<mvc:interceptors>
	<mvc:interceptor>
		
		<mvc:mapping path="/**" />
		
		<bean class="cn.itcast.ssm.interceptor.HandlerInterceptor1" />
	mvc:interceptor>
	<mvc:interceptor>
		
		<mvc:mapping path="/**" />
		
		<bean class="cn.itcast.ssm.interceptor.HandlerInterceptor2" />
	mvc:interceptor>
mvc:interceptors>
public class HandlerInterceptor1 implements HandlerInterceptor {
	// controller执行后且视图返回后调用此方法
	// 这里可得到执行controller时的异常信息
	// 这里可记录操作日志
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("HandlerInterceptor1....afterCompletion");
	}

	// controller执行后但未返回视图前调用此方法
	// 这里可在返回用户前对模型数据进行加工处理,比如这里加入公用信息以便页面显示
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		System.out.println("HandlerInterceptor1....postHandle");
	}

	// Controller执行前调用此方法
	// 返回true表示继续执行,返回false中止执行
	// 这里可以加入登录校验、权限拦截等
	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		System.out.println("HandlerInterceptor1....preHandle");
		// 设置为true,测试使用
		return true;
	}
}

拦截器执行顺序

HandlerInterceptor1…preHandle…

HandlerInterceptor2…preHandle…

HandlerInterceptor2…postHandle…

HandlerInterceptor1…postHandle…

HandlerInterceptor2…afterCompletion…

HandlerInterceptor1…afterCompletion…

preHandle按拦截器定义顺序调用

postHandler按拦截器定义逆序调用

afterCompletion按拦截器定义逆序调用

postHandler在拦截器链内所有拦截器返成功调用

afterCompletion只有preHandle返回true才调用

HandlerInterceptor1的preHandler方法返回true,HandlerInterceptor2返回false,运行流程如下:

HandlerInterceptor1…preHandle…

HandlerInterceptor2…preHandle…

HandlerInterceptor1…afterCompletion…

拦截器的应用

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>

<form action="${pageContext.request.contextPath }/user/login.action">
<label>用户名:label>
<br>
<input type="text" name="username">
<br>
<label>密码:label>
<br>
<input type="password" name="password">
<br>
<input type="submit">

form>

body>
html>
//去登陆的页面
	@RequestMapping(value = "/login.action",method = RequestMethod.GET)
	public String login(){
		return "login";
	}
	@RequestMapping(value = "/login.action",method = RequestMethod.POST)
	public String login(String username
			,HttpSession httpSession){
		httpSession.setAttribute("USER_SESSION", username);
		return "redirect:/item/itemlist.action";
	}
	 
package com.itheima.springmvc.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class Interceptor1 implements HandlerInterceptor{

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("方法前 1");
		//判断用户是否登陆  如果没有登陆  重定向到登陆页面   不放行   如果登陆了  就放行了
		// URL  http://localhost:8080/springmvc-mybatis/login.action
		//URI /login.action
		String requestURI = request.getRequestURI();
		if(!requestURI.contains("/login")){
			String username = (String) request.getSession().getAttribute("USER_SESSION");
			if(null == username){
				response.sendRedirect(request.getContextPath() + "/login.action");
				return false;
			}
		}
		return true;
	}
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("方法后 1");
		
	}
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		System.out.println("页面渲染后 1");
		
	}

}

你可能感兴趣的:(SSM,java,spring)