springMVC学习memo

下面代码的注释部分、若把APIBase类的访问限制改为private、或者protected、则在login requestmapping方法上throw出的ParameterErrorException不能被APIBase里的handleParameterErrorException方法捕获(将会被其父类Base的handleException方法捕获)。
[color=red][b]故在使用JAVA的注解时一定要注意所注解的类是否为public的。[/b][/color]


public class UserController {
public static class Base {

@ExceptionHandler(Exception.class)
public @ResponseBody
Object handleException(HttpServletResponse response, Exception e) throws Exception {
System.out.print("handle exception");
return "ok";
}
}

public static class ParameterErrorException extends Exception{}

//APIBase类可见性改为private或protected都会有问题
//private static class APIBase extends Base {
//protected static class APIBase extends Base {
public static class APIBase extends Base {
@ExceptionHandler(value={ParameterErrorException.class})
public @ResponseBody
Object handleParameterErrorException(ParameterErrorException e) throws Exception {
System.out.print("handle ParameterErrorException");
return "ok";
}

}

public static class Ajaxs{
@Controller
public static class Login extends APIBase {

@RequestMapping(value = Urls.USER.AJAX_LOGIN, method = RequestMethod.POST)
@ResponseBody
public Object login(@ModelAttribute LoginVO loginVO, BindingResult br)
throws Exception {
if(br.hasErrors()){
throw new Exception();
}
if(loginVO.getLoginName() == null){
throw new ParameterErrorException();
}
return "OK";
}

}

@Controller
public static class Logout extends APIBase {
@RequestMapping(value = Urls.USER.AJAX_LOGOUT, method = RequestMethod.GET)
@ResponseBody
public Object logout()
throws Exception {
return "OK";
}
}
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值