Controller层aop

利用@Around通知修改Controller的返回值

自定义一个注解@OperationBtn

在切入点Controller上加上自定义注解

接下来就是重点了,AspectJ写切面类,对该Controller 1 @Component
 2 @Aspect
 3 public class OperationBtnAspect {
 4 
 5     @Autowired
 6     private AppTableOperationServiceI appTableOperationService;
 7     
 8     
 9     @Around(value = "@annotation(anno)", argNames = "anno")
10     public AjaxJson aroundMthod(ProceedingJoinPoint point, OperationBtn anno) throws Throwable{
11         RequestAttributes ra = RequestContextHolder.getRequestAttributes();
12         ServletRequestAttributes sra = (ServletRequestAttributes) ra;
13         HttpServletRequest request = sra.getRequest();
14         
15         String url = request.getRequestURL().toString(); // http://localhost:8080/5U/appDataController.do (URL:返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它不包含请求参数。)
16         String method = request.getMethod();  // POST
17         String uri = request.getRequestURI();  // /5U/appDataController.do 
18         String queryString = request.getQueryString();  // addOrUpdateData&operId=402881e8645f580501645f61202c0044&optId=402881e8645f580501645f61203d004c(返回url路径后面的查询字符串)
19         queryString = queryString.substring(queryString.indexOf("operId"));
20         queryString = queryString.substring(7,queryString.indexOf("&"));
21         AppTableOperationEntity operationEntity = appTableOperationService.getEntity(AppTableOperationEntity.class, queryString);
22         AjaxJson json = (AjaxJson) point.proceed();  //执行Controller中方法,得到返回值json
23         if(json.isSuccess() && operationEntity !=null && operationEntity.getCallbackSucMsg() !=null){
24             json.setMsg(operationEntity.getCallbackSucMsg());
25         }else if(!json.isSuccess() && operationEntity !=null && operationEntity.getCallbackSucMsg() !=null){
26             json.setMsg(operationEntity.getCallbackFailMsg());
27         }
28         return json;
29     }
30 }

 

转载于:https://www.cnblogs.com/Eeexiang/p/9273929.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值