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

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

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

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

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

文档结构图:

01.定义实体类UserInfo

 package cn.happy.entity;

 public class UserInfo {
private String name;
private int age; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

UserInfo

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

 package cn.happy.exceptions;

 public class UserInfoException extends Exception {

     public UserInfoException() {
super();
// TODO Auto-generated constructor stub
} public UserInfoException(String message) {
super(message);
// TODO Auto-generated constructor stub
} }

UserInfoException

 package cn.happy.exceptions;

 public class AgeException extends UserInfoException {

     public AgeException() {
super();
// TODO Auto-generated constructor stub
} public AgeException(String message) {
super(message);
// TODO Auto-generated constructor stub
} }

AgeException

 package cn.happy.exceptions;

 public class NameException extends UserInfoException{

     public NameException() {
super();
// TODO Auto-generated constructor stub
} public NameException(String message) {
super(message);
// TODO Auto-generated constructor stub
} }

NameException

03.定义Controller

 package cn.happy.controller;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.happy.exceptions.AgeException;
import cn.happy.exceptions.NameException;
import cn.happy.exceptions.UserInfoException; @Controller
public class MyController {
//处理器方法
@RequestMapping(value="/first.do")
public String doFirst(String name,int age) throws UserInfoException{ if (!"admin".equals(name)) {
throw new NameException("用户名错误");
} if (age>) {
throw new AgeException("年龄太大");
} return "/WEB-INF/jsp/index.jsp";
}
}

MyController

04.定义applicationcontext.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
"> <!-- 包扫描器 -->
<context:component-scan base-package="cn.happy.controller"></context:component-scan> <!-- 注册系统异常处理器 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="errors.jsp"></property>
<property name="exceptionAttribute" value="ex"></property> <property name="exceptionMappings">
<props>
<prop key="cn.happy.exceptions.NameException">error/nameerrors.jsp</prop>
<prop key="cn.happy.exceptions.AgeException">error/ageerrors.jsp</prop>
</props>
</property>
</bean> </beans>

applicationcontext.xml

05.配置web.xml

     <servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup></load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

web.xml

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

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

spring mvc 第四天【注解实现springmvc 配合使用Exception Resolver 的配置】的更多相关文章

  1. spring mvc 第一天【注解实现springmvc的基本配置】

    创建pojo,添加标识类的注解@Controller,亦可添加该Handler的命名空间:设置类的@RequestMapping(value="/hr") 该类中的方法(Handl ...

  2. spring mvc 第二天【注解实现springmvc Handler处理ajax简单请求 的配置】

    这里使用的是在前台发起ajax请求Handler,后台伪造数据响应给前台, 配置对应ajax请求的Handler信息如下 @Controller public class MyController { ...

  3. J2EE进阶(十三)Spring MVC常用的那些注解

    Spring MVC常用的那些注解 前言 Spring从2.5版本开始在编程中引入注解,用户可以使用@RequestMapping, @RequestParam,@ModelAttribute等等这样 ...

  4. Hibernate Validation,Spring mvc 数据验证框架注解

    1.@NotNull:不能为 Null,但是可以为Empty:用在基本数据类型上. @NotNull(message="{state.notnull.valid}", groups ...

  5. Spring MVC(十四)--SpringMVC验证表单

    在Spring MVC中提供了验证器可以进行服务端校验,所有的验证都必须先注册校验器,不过校验器也是Spring MVC自动加载的,在使用Spring MVC校验器之前首先要下载相关的jar包,下面是 ...

  6. Spring MVC 梳理 - 四种HandlerMapping

    总结 (1)SpringMVC导入jar包,配置web.xml 这些都是基础的操作. (2)四种HandlerMapping:DefaultAnnotationHandlerMapping;Simpl ...

  7. Spring MVC (二)注解式开发使用详解

    MVC注解式开发即处理器基于注解的类开发, 对于每一个定义的处理器, 无需在xml中注册. 只需在代码中通过对类与方法的注解, 即可完成注册. 定义处理器 @Controller: 当前类为处理器 @ ...

  8. SpringMVC学习总结(四)——基于注解的SpringMVC简单介绍

    SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是 DispatcherServlet,DispatcherServlet负责转发每一个Request ...

  9. Spring MVC工作原理 及注解说明

    SpringMVC框架介绍 1) spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面. Spring 框架提供了构建 Web 应用程序的全功 ...

随机推荐

  1. 【转】easyui $.message.alert 点击右上角的关闭按钮时,不执行定义的回调函数

    今天發現這個問題 easyui  $.message.alert  点击右上角的关闭按钮时,不执行定义的回调函数

  2. javascript函数中变量重名

    <script type="text/javascript"> function fun(a){ console.log(a); // function var a=1 ...

  3. python中的单引号,双引号,三引号

    转载自: http://blog.csdn.net/wanghai__/article/details/6285310 先说1双引号与3个双引号的区别,双引号所表示的字符串通常要写成一行 如: s1 ...

  4. 【转】bind - DNS 設定

    http://www.l-penguin.idv.tw/article/dns.htm 參考資訊 DNS 是所有伺服之母,電腦連節時均以 IP 為主,比方說輸入 202.43.195.52 就會到台灣 ...

  5. 现代工程仿真CAE技术介绍

    随着现代科学技术的发展,人们正在不断建造更为快速的交通工具.更大规模的建筑物.更大跨度的桥梁.更大功率的发电机组和更为精密的机械设备.这一切都要求工程师在设计阶段就能精确地预测出产品和工程的技术性能, ...

  6. 查询sql2005&2008全部表信息

    如果是查询sql server ,把sys.extended_properties修改为SysProperties SELECT 表名 THEN D.NAME ELSE '' END, 表说明 THE ...

  7. urlparse

    urlparse模块 urlparse主要是URL的分解和拼接,分析出URL中的各项参数,可以被其他的URL使用,而且只在python2.7中存在,python3中是在urllib包下的urllib. ...

  8. vi 使用入门

    几种模式:     Normal Mode 命令模式     Insert Mode 编辑模式     Command-line Mode      Visual Mode      Select M ...

  9. JAVA 多线程和并发学习笔记(四)

    1. 多进程 实现并发最直接的方式是在操作系统级别使用进程,进程是运行在它自己的地址空间内的自包容的程序.多任务操作系统可以通过周期性地将CPU从一个进程切换到另一个进程,来实现同时运行多个进程. 尽 ...

  10. 文本编辑的css常用属性

    white-space:pre //保留空格,不然又多个空格值显示一个 white-space:nowrap //强制不换行,知道遇到</br> letter-spacing //字母间的 ...