定义处理器
package com.abc.handlers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.abc.exceptions.AgeException;
import com.abc.exceptions.NameException;
@Controller
@RequestMapping("/some")
public class SomeHandler {
@RequestMapping("/first.do")
public String doFirst(String name, int age) {
int a= 3 / 0;
return "/WEB-INF/jsp/welcome.jsp";
}
@RequestMapping("/second.do")
public String doSecond(String name, int age) throws NameException, AgeException {
if(!"pms".equals(name)){
throw new NameException("用户错误");
}
if(age <=0 || age > 100){
throw new AgeException("年龄错误");
}
return "/WEB-INF/jsp/welcome.jsp";
}
}
package com.abc.exceptions;
public class AgeException extends Exception {
public AgeException() {
super();
// TODO Auto-generated constructor stub
}
public AgeException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}
package com.abc.exceptions;
public class NameException extends Exception {
public NameException() {
super();
// TODO Auto-generated constructor stub
}
public NameException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}
<?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"
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">
<!-- 注册组件扫描器 -->
<context:component-scan base-package="com.abc.handlers"/>
<!--注册异常通知 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="/erros.jsp"/> 默认异常
<property name="exceptionMappings">
<props > 自定义异常
<prop key="NameExcepion">/ageErro.jsp</prop>
<prop key="NameExcepion">/nameErro.jsp</prop>
</props>
</property>
</bean>
</beans>