Interceptor
@Slf4j
@Component
public class XxxxInterceptor implements HandlerInterceptor {
@Override
@SneakyThrows
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
....
throw new DefineException("xxxxx....");
....
request.setAttribute("userInfo",userInfo);
return true;
}
Aspect
@Aspect
@Component
@Slf4j
@Order(2)
public class DataLimitAspect {
@Autowired
private XxxxService xxxService;
@Pointcut("@annotation(xxx.annotation.DataLimit)")
public void dataLimitPointcut() {
}
@Before("dataLimitPointcut()")
public void before(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
UserDTO userInfo = (UserDTO)request.getAttribute("userInfo");
....
DataLimit annotation = method.getAnnotation(DataLimit.class);
if (annotation.idOnly()) {
request.setAttribute("id",id);
return;
}
....
for (Object arg : joinPoint.getArgs()) {
if (arg instanceof String){
functionAaa((String) arg ,id);
}
}
....
}
void functionAaa(String str ,Integer id){
....
xxxService.count()...
};
}
GET请求
@DataLimit
@GetMapping("/xxx")
@ApiOperation("...")
public AjaxResult getXxxx(@RequestParam String str , HttpServletRequest request ) {
Object idObj = request.getAttribute("id");
if (null!=idObj) {
...
}
}
POST请求
@DataLimit
@PostMapping("/xxx")
@ApiOperation("...")
public AjaxResult postXxxx(@RequestPart("file") MultipartFile file,
@RequestAttribute("userInfo") UserDTO userInfo ,
HttpServletRequest request ) {
Object idObj = request.getAttribute("id");
if (null!=idObj) {
...
}
}
本站资源均来自互联网,仅供研究学习,禁止违法使用和商用,产生法律纠纷本站概不负责!如果侵犯了您的权益请与我们联系!
转载请注明出处: 免费源码网-免费的源码资源网站 » springboot 用request.setAttribute 传值到控制层
发表评论 取消回复