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的更多相关文章
- Windows7WithSP1/TeamFoundationServer2012update4/SQLServer2012
[Info @09:03:33.737] ====================================================================[Info @ ...
- 《HelloGitHub月刊》第09期
<HelloGitHub>第09期 兴趣是最好的老师,<HelloGitHub>就是帮你找到兴趣! 前言 转眼就到年底了,月刊做到了第09期,感谢大家一路的支持和帮助
- 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/ ...
- iOS系列 基础篇 09 开关、滑块和分段控件
iOS系列 基础篇 09 开关.滑块和分段控件 目录: 案例说明 开关控件Switch 滑块控件Slider 分段控件Segmented Control 1. 案例说明 开关控件(Switch).滑块 ...
- http://www.cnblogs.com/Lawson/archive/2012/09/03/2669122.html
http://www.cnblogs.com/Lawson/archive/2012/09/03/2669122.html
- u-boot-2010.09移植(A)
第一阶段 1.开发环境 系统:centOS6.5 linux版本:2.6.32 交叉编译器:buildroot-2012.08 以上工具已经准备好,具体安装步骤不再 ...
- u-boot-2010.09移植(B)
前面我们的u-boot只是在内存中运行,要想在nandflash中运行,以达到开机自启的目的,还需作如下修改 一.添加DM9000网卡支持 1.修改board/fl2440/fl2440.c中的boa ...
- Linux JDK 安装及卸载 http://www.cnblogs.com/benio/archive/2010/09/14/1825909.html
参考:http://www.cnblogs.com/benio/archive/2010/09/14/1825909.html
- 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][ ...
- JavaScript学习09 函数本质及Function对象深入探索
JavaScript学习09 函数本质及Function对象深入探索 在JavaScript中,函数function就是对象. JS中没有方法重载 在JavaScript中,没有方法(函数)重载的概念 ...
随机推荐
- SpringBoot 2.x 开发案例之 Shiro 整合 Redis
前言 前段时间做了一个图床的小项目,安全框架使用的是Shiro.为了使用户7x24小时访问,决定把项目由单机升级为集群部署架构.但是安全框架shiro只有单机存储的SessionDao,尽管Shrio ...
- Spring--2.Spring之IOC--IOC容器的23个实验(2)
Spring--2.Spring之IOC--IOC容器的23个实验(1) 中的所有实验我都是在同一个工程中进行的,从第十个实验开始,我将新建一个新的工程开始实验. 目前导包还是跟第一个项目一致,bea ...
- Java配置文件读取中文乱码问题
背景 这是我之前在做的用友服务对接开发,昨天领导拿给财务测试时告诉我有乱码,当时我第一想法是用友那边的编码格式有问题,因为还在做其他任务,我说等问一下用友他们用的什么编码格式我们这边改一下,然后今天早 ...
- 使用git将本地文件提交到github存储库
1.首先你要安装git https://git-for-windows.github.io/ 去官网自行下载对应版本 2.安装好git服务器后,找到你项目的文件夹,右键git bash here打开命 ...
- 百度DMA+小度App的蓝牙语音解决方案案例展示
前记 跟着百度也有一段时间了,经过一年多的努力,我们也做出了一些产品.下面就给大家秀一下我们做的产品.有类似需求的朋友可以多多交流. 智能语音耳机 这个是就是可以通过按键来调用小度app的运动 ...
- eclipse开发工具内打开某js文件总是用记事本方式打开的问题
问题现象: 开发时不知道按到了什么快捷键,导致在某js文件内点击某调用方法时莫名其妙的用记事本方式打开了该js文件,试了几次都是这样.索性将该js文件关掉重新打开,结果双击该文件还是弹出了一个记事本, ...
- P1559 运动员最佳匹配问题 by hyl 天梦
#include<iostream> using namespace std; int n; int maxx[21][21]; int lie[21]; int aa[21]; int ...
- python sys.modules 和 sys.path 及 __name__
1.sys.modules 存放已经缓存的模块 值是dict 2.sys.path 搜索路径 值是list 3.if __name__= __main__ 可以看成python的程序入口,如果直接执行 ...
- Vue+elementUI 自定义动态数据菜单导航组件实现展开收缩+路由跳转router-view渲染数据 路由跳转到同一个页面带参数ID 自动刷新数据
准备:导入ElementUI 看官网教程 数据准备:JSON数据转换成树状 参考文章: JS实现 JSON扁平数据转换树状数据 后台我拿的数据是这样的格式: [ {id:1 , parentId: 0 ...
- [校内训练19_09_03]c Huge Counting
题意 有一个定义在 k 维非负整点上的函数:$f(x_1,x_2,...,x_k):N_{0}^{k}->\{0,1\}$ ,定义方法如下: 若存在$j∈[1,k],x_j=0$,则$f(x_1 ...