spring mvc 第四天【注解实现springmvc 配合使用Exception Resolver 的配置】

Tips:这里使用具体springmvc的异常处理只是拿一个简单的案例来说明问题,并不做实用,如有需求可做更改;

 这里演示的仅是能够实现service验证的一种方案,如有更好的请留言我会努力学习!!

之前有项目需求不光前台js验证,还需后台支持验证才行,使用了该方式来解决问题,这里做一下总结;

今天的案例说明:用户在前台发出请求,后台判定该请求中的数据是否【合法】,如不合法,返回给用户 相应的提示;

本人思路:从后台开始吧,更加的能够说明问题;

文档结构图:

01.定义实体类UserInfo

 1 package cn.happy.entity;
 2 
 3 public class UserInfo {
 4     private String name;
 5     private int age;
 6 
 7     public String getName() {
 8         return name;
 9     }
10     public void setName(String name) {
11         this.name = name;
12     }
13     public int getAge() {
14         return age;
15     }
16     public void setAge(int age) {
17         this.age = age;
18     }
19 }
UserInfo

02.定义三个异常处理类AgeException NameException UserInfoException

 1 package cn.happy.exceptions;
 2 
 3 public class UserInfoException extends Exception {
 4 
 5     public UserInfoException() {
 6         super();
 7         // TODO Auto-generated constructor stub
 8     }
 9 
10     public UserInfoException(String message) {
11         super(message);
12         // TODO Auto-generated constructor stub
13     }
14    
15 }
UserInfoException
 1 package cn.happy.exceptions;
 2 
 3 public class AgeException extends UserInfoException {
 4 
 5     public AgeException() {
 6         super();
 7         // TODO Auto-generated constructor stub
 8     }
 9 
10     public AgeException(String message) {
11         super(message);
12         // TODO Auto-generated constructor stub
13     }
14  
15 }
AgeException
 1 package cn.happy.exceptions;
 2 
 3 public class NameException extends UserInfoException{
 4 
 5     public NameException() {
 6         super();
 7         // TODO Auto-generated constructor stub
 8     }
 9 
10     public NameException(String message) {
11         super(message);
12         // TODO Auto-generated constructor stub
13     }
14    
15 }
NameException

03.定义Controller

 1 package cn.happy.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 import cn.happy.exceptions.AgeException;
 6 import cn.happy.exceptions.NameException;
 7 import cn.happy.exceptions.UserInfoException;
 8 
 9 @Controller
10 public class MyController {
11    //处理器方法
12     @RequestMapping(value="/first.do")
13     public String doFirst(String name,int age) throws UserInfoException{
14     
15         if (!"admin".equals(name)) {
16             throw new NameException("用户名错误");
17         }
18         
19         if (age>40) {
20             throw new AgeException("年龄太大");
21         }
22         
23         return "/WEB-INF/jsp/index.jsp";
24     }
25 }
MyController

04.定义applicationcontext.xml文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:aop="http://www.springframework.org/schema/aop" 
 6     xmlns:tx="http://www.springframework.org/schema/tx" 
 7     xmlns:mvc="http://www.springframework.org/schema/mvc" 
 8      xsi:schemaLocation="
 9         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
10         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
11         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
12         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
13         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
14         ">
15         
16      <!-- 包扫描器 -->
17      <context:component-scan base-package="cn.happy.controller"></context:component-scan>
18     
19     <!-- 注册系统异常处理器 -->
20      <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
21         <property name="defaultErrorView" value="errors.jsp"></property>
22         <property name="exceptionAttribute" value="ex"></property>
23         
24         <property name="exceptionMappings">
25            <props>
26               <prop key="cn.happy.exceptions.NameException">error/nameerrors.jsp</prop>
27               <prop key="cn.happy.exceptions.AgeException">error/ageerrors.jsp</prop>
28            </props>
29         </property>
30      </bean>
31   
32 </beans>
applicationcontext.xml

05.配置web.xml

 1     <servlet-name>springmvc</servlet-name>
 2     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 3     <init-param>
 4       <param-name>contextConfigLocation</param-name>
 5       <param-value>classpath:applicationContext.xml</param-value>
 6     </init-param>
 7     <load-on-startup>1</load-on-startup>
 8   </servlet>
 9   <servlet-mapping>
10     <servlet-name>springmvc</servlet-name>
11     <url-pattern>*.do</url-pattern>
12   </servlet-mapping>
web.xml

前台请求触发对应异常跳转到对应页面;源码已上传至

https://git.oschina.net/zymqqc/springmvc-code.git

原文地址:https://www.cnblogs.com/john69-/p/6123097.html