对象绑定验证以及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";
}
@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";
}
相关文章推荐
- 绑定新浪微博API 实现验证 登录 返回接口获取信息
- Python自动化之ajax返回表单验证的错误信息和序列化扩展
- ContentProvider获取系统的电话号码绑定到ListView以及跳转通讯录获区联系人信息绑定到前台。
- AOP实现拦截对象以及获取切入目标方法和注解
- AOP拦截+权限验证+返回默认接口对象
- AOP实现拦截对象以及获取切入目标方法和注解
- JavaScript验证cron表达式是否正确(返回错误信息)
- 在linux中获取错误返回信息
- AOP实现拦截对象以及获取切入目标方法和注解
- Apache支持.htaccess以及“No input file specified” 和“ 重新载入页面以获取源代码”错误信息解决方案
- Dom 中绑定onclick时间获取对象以及event的方法
- AOP实现拦截对象以及获取切入目标方法和注解
- jsp获取后台返回的对象中包含的list以及el获取后台json对象并且解析
- VC中GetLastError()获取错误信息的使用,以及错误代码的含义
- 在linux中获取错误返回信息
- AOP实现拦截对象以及获取切入目标方法和注解
- 自定义Struts2表单验证后的错误信息返回到网页的显示格式/样式
- 在linux中获取错误返回信息 &…
- AOP实现拦截对象以及获取切入目标方法和注解
- Spring MVC Controler层获取errors验证信息 同时返回