一.单拦截器
1.编写自定义拦截器方法:
a.实现HandlerInterceptor接口,实现preHandle postHandle afterCompletion三个抽象方法.
b.在配置文件中配置拦截器.代码如下:
2.拦截器中方法的执行顺序
preHandle—>目标方法—>postHandle—>afterCompletion
3.多个拦截器的执行顺序
多个拦截器的执行顺序跟拦截器在配置文件中的配置顺序对应。先配置的先执行.
4.多个拦截器的方法的执行顺序
preHandle: 根据拦截器的配置顺序,正序执行
postHandle:根据拦截器的配置顺序,倒序执行
afterCompletion:根据拦截器的配置顺序,倒序执行
5.单个拦截器方法执行顺序:
render:渲染
业务方法执行之后,视图渲染之前执行postHandle
二.双拦截器
业务方法执行前自定义拦截器根据springmvc.xml配置正序进行preHandle,业务方法执行后倒序执行postHandle,并倒序关闭,即afterCompletion
三.异常
1.ExceptionHandlerExceptionResolver
a.支持使用@ExceptionHandler注解来进行异常的处理
b.@ExceptionHandler({各种异常.class}) 是有优先级问题的. 优先选择匹配度最高的异常处理方法.
c.可以通过@ControllerAdvice来实现全局的异常处理.
当Handler中抛出异常后,如果在当前的Handler中找不到异常处理方法,就会到标注了@ControllerAdvice的类中找异常处理方法.
2.SimpleMappingExceptionResolver
a.将指定的异常映射到指定的页面.
b.具体的配置:
<bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionAttribute" value="yichang"></property>
<property name="exceptionMappings">
<props>
<prop key="java.lang.ArrayIndexOutOfBoundsException">error</prop>
</props>
</property>
</bean>
解决运算异常:
@ExceptionHandler(value={ArithmeticException.class})
public ModelAndView dealHandlerExceptionResolver(Exception e){
System.out.println("-----------11111111--------dealHandlerExceptionResolver");
ModelAndView mv = new ModelAndView();
// mv.addObject("myException");
mv.setViewName("error");
return mv;
3.@ResponseStatus
代码如下:
自定义异常:
@ResponseStatus(value = HttpStatus.LOCKED,reason = "用户被锁住,请拨打1022")
public class MyUserLockException extends RuntimeException {
private static final long serialVersionUID = -147674262913531007L;
}
控制层:
@RequestMapping(value="/testResponseStatus",method=RequestMethod.GET)
public String testResponseStatus(@RequestParam("userName") String userName){
if(userName.equalsIgnoreCase("li4")){
throw new MyUserLockException();
}
return "ok";
}//访问时输入li4则抛出自定义异常,显示用户被锁住
主入口:
testResponseStatus:<a href="${pageContext.request.contextPath}/testResponseStatus?userName=li3">testResponseStatus</a>
四.SpringMVC运行流程
五.SpringMVC和Spring整合
1.web.xml中配置:
<!--spring config-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springMVC config-->
<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>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
2.springmvc.xml配置:负责控制层(@Controller)
<!--use-default-filters="false"不是默认设置,由自己规定,保证只负责带有注解@Controller的控制层-->
<context:component-scan base-package="com.atguigu.springmvc" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
3.spring配置文件applicationContext.xml配置:负责业务层(@Service)
<!--扫包-->
<context:component-scan base-package="com.atguigu.springmvc">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>