当前位置:网站首页>Web开发小妙招:巧用ThreadLocal规避层层传值
Web开发小妙招:巧用ThreadLocal规避层层传值
2022-07-06 18:34:00 【InfoQ】
Web开发中的一点麻烦事
public class XXController() {
public void addUser() {
// 获取操作人
Integer opUserId = Integer.parseInt(getHeader("opUserId"));
// 获取其他参数...
service.addUser(...., opUserId);
renderAppMsg("添加用户成功");
}
}
public class XXService() {
public void addUser(String tel, String name, String password, Integer opUserId) {
checkTel(tel, opUserId);
checkName(name, opUserId);
checkPassword(password, opUserId);
}
public void checkTel(String tel, Integer opUserId) {
check(tel, opUserId);
}
publc void check(..., Integer opUserId) {
// ...
}
}
思考
在拦截器中使用ThreadLocal暂存请求参数
package com.holdoa.core.interceptor;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import javax.servlet.http.HttpServletRequest;
public class RequestPool implements Interceptor {
public static ThreadLocal<HttpServletRequest> localRequest= new ThreadLocal<>();
@Override
public void intercept(Invocation inv) {
localRequest.set(inv.getController().getRequest());
inv.invoke();
localRequest.remove();
}
public static HttpServletRequest getRequest() {
return localRequest.get();
}
}
使用参数
String para = RequestPool.localRequest.get().getParameter("username");
边栏推荐
- Introduction to microservice architecture
- ROS learning (24) plugin
- AcWing 904. Wormhole solution (SPFA for negative rings)
- 2022/0524/bookstrap
- centos8安装mysql报错:The GPG keys listed for the “MySQL 8.0 Community Server“ repository are already ins
- Right mouse button customization
- ROS learning (23) action communication mechanism
- STM32F4---通用定时器更新中断
- ROS learning (XIX) robot slam function package cartographer
- JS ES5也可以创建常量?
猜你喜欢
我如何编码8个小时而不会感到疲倦。
The GPG keys listed for the "MySQL 8.0 community server" repository are already ins
张平安:加快云上数字创新,共建产业智慧生态
Time synchronization of livox lidar hardware -- PPS method
猫猫回收站
JS how to quickly create an array with length n
永久的摇篮
ROS learning (25) rviz plugin
ROS学习(26)动态参数配置
Flir Blackfly S USB3 工业相机:白平衡设置方法
随机推荐
RC振荡器和晶体振荡器简介
New job insights ~ leave the old and welcome the new~
FLIR blackfly s industrial camera: configure multiple cameras for synchronous shooting
C language [23] classic interview questions [Part 2]
微服务架构介绍
FLIR blackfly s industrial camera: synchronous shooting of multiple cameras through external trigger
Basic introduction and use of dvajs
Flir Blackfly S 工业相机:自动曝光配置及代码
When grep looks for a process, it ignores the grep process itself
Centros 8 installation MySQL Error: The gpg Keys listed for the "MySQL 8.0 Community Server" repository are already ins
AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)
Get to know MySQL for the first time
Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 1)
Long press the button to execute the function
454 Baidu Mianjing 1
JVM 内存模型
使用Ceres进行slam必须要弄清楚的几个类和函数
AcWing 1142. Busy urban problem solving (minimum spanning tree)
AcWing 1141. LAN problem solving (kruskalkruskal finding the minimum spanning tree)
Flir Blackfly S 工业相机:通过外部触发实现多摄像头同步拍摄