您的位置:首页 > 运维架构

对象绑定验证以及aop环绕获取错误信息并返回

2018-02-28 11:46 351 查看
SpringMVC Action 接口方法
@RequestMapping(value = "addstudent", method = RequestMethod.POST)
public String AddStudent(@Valid Student student, BindingResult result,
HttpServletResponse response,HttpServletRequest request)
throws IOException {

studentService.AddStudent(student);
response.sendRedirect("LimitStudent?page=1");
return null;

}

Aop切面类获取错误信息
private Vector<String> v = new Vector<String>();

@Around("execution(* com.meng.test.action.StudentAction.AddStudent(..))")
public Object before(ProceedingJoinPoint p) throws Throwable {
Object[] obj = p.getArgs();      //获取目标方法参数列表
for (Object object : obj) {
if (object instanceof BindingResult) {
BindingResult b = (BindingResult) object;
List<FieldError> list = b.getFieldErrors();
if (list != null) {
for (FieldError fieldError : list) {
v.add(fieldError.getDefaultMessage() + "   ");
}
}
}

}
if (v.isEmpty()) {      //集合中没有错误信息调用目标方法
Object obj2 = p.proceed();
return obj2;
}

for (Object object2 : obj) {        //集合中有错误信息把错误信息打印到添加学生信息页面中
if (object2 instanceof HttpServletRequest) {
HttpServletRequest request = (HttpServletRequest) object2;
request.setAttribute("error", v.toString());

}
}

v.clear();
return "AddStudent";

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: