09-SpringMVC03

Posted rqy0526

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了09-SpringMVC03相关的知识,希望对你有一定的参考价值。

今日知识

1. SpringMVC自定义异常处理
2. SpringMVC的interceptor(过滤器)

SpringMVC自定义异常处理

1.web.xml正常写

<servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--<load-on-startup>1</load-on-startup>-->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

2. application.xml写法

<context:component-scan base-package="com.rqy"/>
<mvc:annotation-driven/>
<!--配置springmvc视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

3. Controller处理层

@Controller
public class HelloController  
@RequestMapping("hello")
public String hello(int type) throws Exception 
    if (type==1)
    throw  new MyException("你的用户名过长");//自定义异常
    else if (type==2)
        throw new FileSizeException();
    else if (type>2)
        throw new UnknownParamException();
    
    return "hello" ;

4. 自定义异常MyException-->其他异常也类似

public class MyException extends Exception 
public MyException(String  message)
    super(message);
    

5. 注册自定义异常处理器(会自动拦截异常)

* 避免直接在页面显示异常信息,我们自定义异常处理器,抛出对应的异常时将转发到不同的页面上。
* 案例:
* @Component
public class CustomExceptionResolver implements HandlerExceptionResolver 
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) 
    ModelAndView modelAndView = new ModelAndView();
    System.out.println(o);
    if (e instanceof MyException) 
        MyException myException = (MyException) e;
        modelAndView.addObject("content", myException.getMessage());
        modelAndView.setViewName("customException");
     else if (e instanceof FileSizeException) 

        modelAndView.setViewName("fileException");
     else if (e instanceof UnknownParamException) 
        modelAndView.setViewName("paramException");

    
    return modelAndView;


* 其中customException,fileException,paramException对应的jsp页面
* 例如:页面打印出: 自定义的异常 $content,即可。

SpringMVC的interceptor(过滤器)

1. 原理:就是过滤器

2. ctrl+o:直接可以调出实现接口的方法

3. implements HandlerInterceptor

* public class MySecondInterceptor implements HandlerInterceptor 
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception 
    System.out.println("preHandle2");
    return true;

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception 
    System.out.println("postHandle2");

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception 
    System.out.println("afterCompletion2");

4. 如果一个为false的话,下面的全部流程都不进行

5. xml配置

*     <mvc:interceptors>
    <!--全部请求都通过interceptor-->
    <!--/**-->
    <bean class="com.rqy.interceptor.MyFirstInterceptor"/>
    <bean class="com.rqy.interceptor.MySecondInterceptor"/>
    <!--<ref bean="interceptor3"/>-->
    <!--interc拦截部分请求-->
    <!--user/query
    user/add
    user/register-->
    <mvc:interceptor>
        <!--localhost/user/a yes-->
        <mvc:mapping path="/user/**"/>
        <bean class="com.rqy.interceptor.MyThirdInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>

6. 结论:preHandle全为true才会执行到requestMapping和postHandle

7. 案例(用户未登录拦截到login.jsp)

* public class MyFirstInterceptor implements HandlerInterceptor 
    //当返回值为false时不能够进入RequestMapping对应的方法
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception 
        System.out.println("preHandle1");
        String username = request.getParameter("username");
        if (username==null|| "".equals(username))
            return true;//如果没有参数提交,继续验证下面方法postHandle,看看用户是否登录了
        else 
            request.getSession().setAttribute("username",username);
            return true;  //username不为空,意味用户登录操作,不进行拦截
        
    
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception 
        String username = (String) request.getSession().getAttribute("username");
        if (username==null|| "".equals(username))
            modelAndView.setViewName("login");//用户未登录返回login界面
        else 
            modelAndView.addObject("username",username);
            modelAndView.setViewName("hello");
        
        System.out.println("postHandle1");
    
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception 
        System.out.println("afterCompletion1");
    

以上是关于09-SpringMVC03的主要内容,如果未能解决你的问题,请参考以下文章

SpringMVC:SpringMVC的常见注解

SpringMVC知识总结

SpringMVC知识总结

09. Spring MVC源码解析

springMVC学习笔记

SpringMVC简介及其快速入门搭建项目