springmvc-exception



定义处理器

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>




























评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值