spring mvc 第四天【注解实现springmvc 配合使用Exception Resolver 的配置】
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 的配置】的更多相关文章
- spring mvc 第一天【注解实现springmvc的基本配置】
创建pojo,添加标识类的注解@Controller,亦可添加该Handler的命名空间:设置类的@RequestMapping(value="/hr") 该类中的方法(Handl ...
- spring mvc 第二天【注解实现springmvc Handler处理ajax简单请求 的配置】
这里使用的是在前台发起ajax请求Handler,后台伪造数据响应给前台, 配置对应ajax请求的Handler信息如下 @Controller public class MyController { ...
- J2EE进阶(十三)Spring MVC常用的那些注解
Spring MVC常用的那些注解 前言 Spring从2.5版本开始在编程中引入注解,用户可以使用@RequestMapping, @RequestParam,@ModelAttribute等等这样 ...
- Hibernate Validation,Spring mvc 数据验证框架注解
1.@NotNull:不能为 Null,但是可以为Empty:用在基本数据类型上. @NotNull(message="{state.notnull.valid}", groups ...
- Spring MVC(十四)--SpringMVC验证表单
在Spring MVC中提供了验证器可以进行服务端校验,所有的验证都必须先注册校验器,不过校验器也是Spring MVC自动加载的,在使用Spring MVC校验器之前首先要下载相关的jar包,下面是 ...
- Spring MVC 梳理 - 四种HandlerMapping
总结 (1)SpringMVC导入jar包,配置web.xml 这些都是基础的操作. (2)四种HandlerMapping:DefaultAnnotationHandlerMapping;Simpl ...
- Spring MVC (二)注解式开发使用详解
MVC注解式开发即处理器基于注解的类开发, 对于每一个定义的处理器, 无需在xml中注册. 只需在代码中通过对类与方法的注解, 即可完成注册. 定义处理器 @Controller: 当前类为处理器 @ ...
- SpringMVC学习总结(四)——基于注解的SpringMVC简单介绍
SpringMVC是一个基于DispatcherServlet的MVC框架,每一个请求最先访问的都是 DispatcherServlet,DispatcherServlet负责转发每一个Request ...
- Spring MVC工作原理 及注解说明
SpringMVC框架介绍 1) spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面. Spring 框架提供了构建 Web 应用程序的全功 ...
随机推荐
- JavaScript的学习4
一.Array对象 ArrayObject.reverse() 将数组进行翻转 ArrayObject.shift() 删除数组中的第一个元素并将其返回 ArrayObject.unshi ...
- [杂] 将高版本iTunes备份恢复到低版本iOS设备中
除非开发测试用设备,自用设备不要随便升iOS beta,不要随便升iOS beta,不要随便升iOS beta. 对于升级了高版本iOS的用户,默认情况下重刷低版本iOS时,iTunes不允许向低版本 ...
- super语句
1.super是对当前对象的父类对象的默认引用, 2.super必须出现在子类中(方法或构造函数),而不是其他位置 3.super无法访问private成员属性和方法
- Spark1.3使用外部数据源时条件过滤只要是字符串类型的值均报错
CREATE TEMPORARY TABLE spark_tbls USING org.apache.spark.sql.jdbc OPTIONS ( url 'jdbc:mysql://hadoop ...
- 在 Arch Linux 玩百度 Flash 战曲游戏乱码
#!/bin/sh #From: http://hi.baidu.com/imtinge/item/3516761d314481542b3e22f0 #Info: CJK Unicode font M ...
- unity自定义菜单面板开发
using UnityEditor;using UnityEngine;using CreateTerrainDLL; public class CreateTerrainMenu : EditorW ...
- 使用Struts2搭建登录注册示例
使用Struts2来搭建mvc网站框架还是比较容易的,Struts2提供了各项辅助功能,保证了web开发的快速方便.下面使用struts2来搭建一个登录注册示例. 0 项目结构截图 1 搭建Strut ...
- dubbo+zookeeper集群配置
集群服务注册到多台zookeeper配置: <dubbo:registry protocol="zookeeper" address="10.20.153.10:2 ...
- 块状元素(div)与内联元素(span)
<pre class="html" name="code"><html xmlns="http://www.w3.org/1999/ ...
- uva11292 Dragon of Loowater
水题,排序遍历即可 #include<iostream> #include<cstdio> #include<algorithm> using namespace ...