controller包下的HelloController
-----------------------------------------------------------------------------
package controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
/**
*
*演示异常处理
*/
@Controller
public class HelloController {
//异常处理的注解
@ExceptionHandler
public String handler(Exception e,HttpServletRequest request) {
/**
* 依据异常类型,进行不同处理
*/
if(e instanceof NumberFormatException) {
request.setAttribute("message", "请输入正确的数字");
return "message";
}else if(e instanceof StringIndexOutOfBoundsException) {
request.setAttribute("message", "数组越界");
return "message";
}//如果下面还有异常,继续else if,处理
return "error3";//如果都不是,可能是系统异常,返会稍后重试页面
}
@RequestMapping("hello.do")
public String hello() {
System.out.println("hello()的方法");
String str="123a";
/*
* 下面会出异常,
*/
Integer.parseInt(str);
return "hello";
}
@RequestMapping("hello2.do")
public String hello2() {
System.out.println("这是hello2的方法");
String str="abcd";
str.charAt(10);
return "hello";
}
}
=====================spring-mvc.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 配置组件扫描,会扫描Controller下面所有的包,子包下的所有类,当有发现类上有注解,会当成bean来管理 -->
<context:component-scan
base-package="controller"></context:component-scan>
<!--mvc注解扫描,组件扫描只管4个注解(service,conponent,controller ,repostly).RequestMapping属于springmvc特定的注解,需要配置mvc注解扫描
不然RequestMapping注解就不起作用了 -->
<mvc:annotation-driven />
<!-- 配置视图解析器 。prefix表示前缀 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置异常处理 ,注释掉,换种方法处理
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.NumberFormatException">error</prop>
<prop key="java.lang.StringIndexOutOfBoundsException">error2</prop>
</props>
</property>
</bean>
-->
</beans>
---------------------=================error.jsp==================
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>first spring-mvc</title>
</head>
<body style="font-size: 30px;font-style: italic;">
我草,请输入数字,OK?
</body>
</html>
======================error2.jsp==========================
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>first spring-mvc</title>
</head>
<body style="font-size: 30px;font-style: italic;color:red;">
数组下标越界
</body>
</html>
==========================error3.jsp=============================
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>first spring-mvc</title>
</head>
<body style="font-size: 30px;font-style: italic;color:red;">
系统异常,稍后重试,谢谢!!!
</body>
</html>
======================hello.jsp==========================
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>first spring-mvc</title>
</head>
<body style="font-size: 30px;font-style: italic;">
Hello,SpringMVC!
</body>
</html>
==========================message.jsp=======================
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>first spring-mvc</title>
</head>
<body style="font-size: 30px;font-style: italic;color:red;">
错误提示: ${message}
</body>
</html>
=============================web.xml===============================
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- DispatcherServlet的初始化方法会启动spring
容器,所以需要配置初始化参数来指定spring配置文件的位置
如果没有配置初始化参数,则会找WEB-INF/*-servlet.xml ,*与前端控制器的name一样 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>