今日知识

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的更多相关文章

  1. Windows7WithSP1/TeamFoundationServer2012update4/SQLServer2012

    [Info   @09:03:33.737] ====================================================================[Info   @ ...

  2. 《HelloGitHub月刊》第09期

    <HelloGitHub>第09期 兴趣是最好的老师,<HelloGitHub>就是帮你找到兴趣! 前言 转眼就到年底了,月刊做到了第09期,感谢大家一路的支持和帮助

  3. js正则表达式校验非零的正整数:^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. iOS系列 基础篇 09 开关、滑块和分段控件

    iOS系列 基础篇 09 开关.滑块和分段控件 目录: 案例说明 开关控件Switch 滑块控件Slider 分段控件Segmented Control 1. 案例说明 开关控件(Switch).滑块 ...

  5. http://www.cnblogs.com/Lawson/archive/2012/09/03/2669122.html

    http://www.cnblogs.com/Lawson/archive/2012/09/03/2669122.html

  6. u-boot-2010.09移植(A)

    第一阶段 1.开发环境 系统:centOS6.5           linux版本:2.6.32         交叉编译器:buildroot-2012.08 以上工具已经准备好,具体安装步骤不再 ...

  7. u-boot-2010.09移植(B)

    前面我们的u-boot只是在内存中运行,要想在nandflash中运行,以达到开机自启的目的,还需作如下修改 一.添加DM9000网卡支持 1.修改board/fl2440/fl2440.c中的boa ...

  8. Linux JDK 安装及卸载 http://www.cnblogs.com/benio/archive/2010/09/14/1825909.html

    参考:http://www.cnblogs.com/benio/archive/2010/09/14/1825909.html

  9. c#用正则表达式判断字符串是否全是数字、小数点、正负号组成 Regex reg = new Regex(@"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$");

    Regex reg = new Regex(@"^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][ ...

  10. JavaScript学习09 函数本质及Function对象深入探索

    JavaScript学习09 函数本质及Function对象深入探索 在JavaScript中,函数function就是对象. JS中没有方法重载 在JavaScript中,没有方法(函数)重载的概念 ...

随机推荐

  1. MySQL插入操作

    说明:value的值可以为数据,DEFAULT,NULL,expr 含有ATUO_INCREMENT的列可以插入DEFAULT.NULL,或者不插入记录来实现自动增长. 插入记录的三种方法:①可以同时 ...

  2. 自动将本地文件保存到GitHub

    前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 这篇文章主要讲讲如何自动将本地文件保存到GitH ...

  3. 【开源】后台权限管理系统升级到aspnetcore3.1

    *:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...

  4. bzoj 2683 CDQ分治

    题目描述 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 将格子x,y里的数 ...

  5. GitHub进阶之利用Git远程仓库篇

    #在上一篇文章,相信大家对GitHub已经有了一个基础的理解 接下来我们来学习一下如何利用git来远程仓库 一,git是什么 git:一个免费的开源版本控制软件 用途:利用Git管理GitHub上的代 ...

  6. 使用RobotFramework的DataBaseLibrary(Java实现)

    RobotFramework能用Python和Jython两条腿走路.但有的时候你得选一条.今天就碰上个问题,为了整合其它模块必须用Java实现的DataBaseLibrary 其实实它很简单,记录步 ...

  7. Please verify that your device’s clock is properly set, and that your signing certificate is not expired.

    解决方法: 1.关闭项目,找到项目文件XXXX.xcodeproj,在文件上点击右键,选择“显示包内容”(Show Package Contents).会新打开一个Finder. 2.在新打开的Fin ...

  8. vue-cookies

    vue-cookies用于登录,一般和vuex一起使用 vuex在各个组件共享值,cookie恒久保留值 一.安装 npm install vue-cookies --save 二.引用(在store ...

  9. ffifdyop——绕过中一个奇妙的字符串

    根据师傅们的博客总结如下: ffifdyop 经过md5加密后:276f722736c95d99e921722cf9ed621c 再转换为字符串:'or'6<乱码>  即  'or'66� ...

  10. 移除sitemap中的entity

    下面截图是sitemap所在的位置 如果遇到什么原因,当前使用的entity被弃用需要删除,必须要把当前site map 引用的entity也一并删除. 不然会导致site map不能正常加载