SSM-SpringMVC-26:SpringMVC异常骇级之自定义异常注解版
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
注解的方法实现异常解析,话不多说,直接搞起,和以前一样的习惯,和上篇博客一样的代码放后面,不一样的在前面
案例:
1.自定义处理器和处理方法
package cn.dawn.day18annotationexception; import cn.dawn.day18annotationexception.userexception.UserageException; import cn.dawn.day18annotationexception.userexception.UsernameException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by Dawn on 2018/3/28. */ @Controller public class ZDYExceptionController { /*作用于这俩个*/ @ExceptionHandler({UsernameException.class,UserageException.class}) public ModelAndView resolveException(Exception ex) { ModelAndView modelAndView=new ModelAndView(); /*返回的异常对象*/ modelAndView.addObject("ex",ex); /*判断去那个页面*/ if(ex instanceof UsernameException){ modelAndView.setViewName("name"); } if(ex instanceof UserageException){ modelAndView.setViewName("age"); } return modelAndView; } @RequestMapping("/annotationException") public String annotationException(String username,Integer userage) throws Exception { if(!username.equals("admin")){ throw new UsernameException("登陆名不正确"); } if(userage<18){ throw new UserageException("未成年,走开"); } return "success"; } }
2.自己的xml大配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--包扫描器--> <context:component-scan base-package="cn.dawn.day18annotationexception"></context:component-scan> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/day18/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
3.修改web.xml的中央调度器的上下文配置路径
4.login.jsp
<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h2>登录</h2> <form action="${pageContext.request.contextPath}/annotationException" method="post"> 用户名:<input name="username"> 年龄:<input name="userage"> <input type="submit" value="登录"/> </form> </body> </html>
下面的内容与上篇博客相同
5.UserageException自定义异常
package cn.dawn.day17selfexceptionresolver.userexception; /** * Created by Dawn on 2018/3/30. */ public class UserageException extends Exception { public UserageException() { super(); } public UserageException(String message) { super(message); } }
6.UsernameException自定义异常
package cn.dawn.day17selfexceptionresolver.userexception; /** * Created by Dawn on 2018/3/30. */ public class UsernameException extends Exception { public UsernameException() { super(); } public UsernameException(String message) { super(message); } }
7.jsp页面
7.1success.jsp
<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %> <html> <body> <%--<img src="image/1.jpg">--%> <h2>Success!</h2> </body> </html>
7.2error.jsp
<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h2>ERROR</h2> <p>${ex.message}</p> 服务器被猴子砍了,攻城狮在抢修中,还杀了个程序猿祭天 </body> </html>
7.3age.jsp
<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h2>年龄错误ERROR</h2> <p>${ex.message}</p> 服务器被猴子砍了,攻城狮在抢修中,还杀了个程序猿祭天 </body> </html>
7.4name.jsp
<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h2>名字错误ERROR</h2> <p>${ex.message}</p> 服务器被猴子砍了,攻城狮在抢修中,还杀了个程序猿祭天 </body> </html>
8.启动tomcat,访问login.jsp
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
SSM-SpringMVC-24:SpringMVC异常高级之自定义异常
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 自定义异常,大家都会,对吧,无非就是继承异常类等操作,很简单,我就不多扯皮了,但是在xml配置文件中有个不同的操作,我一会重点列出来 案例开始: 1.自定义异常类:UserageException package cn.dawn.day17selfexceptionresolver.userexception; /** * Created by Dawn on 2018/3/30. */ public class UserageException extends Exception { public UserageException() { super(); } public UserageException(String message) { super(message); } } 2.自定义异常类:UsernameException package cn.dawn.day17selfexceptionresolver.userexception; /** * Created by ...
- 下一篇
SSM-SpringMVC-27:SpringMVC类型转换之日期类型初步
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本案例是上面的异常和日期类型转换结合的一个小小的Demo 案例开始 1.自定义处理器和处理方法: package cn.dawn.day19typeconverter; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import java.util.Date; /** * Created by Dawn on 2018/3/28. */ @Controller public class T...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS7设置SWAP分区,小内存服务器的救世主
- Docker快速安装Oracle11G,搭建oracle11g学习环境