当前位置:网站首页>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");
边栏推荐
- 2022 system integration project management engineer examination knowledge point: Mobile Internet
- @Before, @after, @around, @afterreturning execution sequence
- Use nodejs to determine which projects are packaged + released
- Flir Blackfly S USB3 工业相机:白平衡设置方法
- LeetCode. Sword finger offer 62 The last remaining number in the circle
- 字符串转成日期对象
- C语言关于链表的代码看不懂?一篇文章让你拿捏二级指针并深入理解函数参数列表中传参的多种形式
- CISP-PTE实操练习讲解(二)
- NPM install compilation times "cannot read properties of null (reading 'pickalgorithm')“
- [unique] what is the [chain storage structure]?
猜你喜欢
Cat recycling bin
Flir Blackfly S 工业相机:配置多个摄像头进行同步拍摄
Stm32f4 --- general timer update interrupt
Batch delete data in SQL - set in entity
开发中对集合里面的数据根据属性进行合并数量时犯的错误
BigDecimal 的正确使用方式
百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (下)
New job insights ~ leave the old and welcome the new~
Centros 8 installation MySQL Error: The gpg Keys listed for the "MySQL 8.0 Community Server" repository are already ins
How did partydao turn a tweet into a $200million product Dao in one year
随机推荐
新工作感悟~辞旧迎新~
Flir Blackfly S USB3 工业相机:白平衡设置方法
Errors made in the development of merging the quantity of data in the set according to attributes
AcWing 1141. LAN problem solving (kruskalkruskal finding the minimum spanning tree)
CISP-PTE实操练习讲解(二)
ROS学习(22)TF变换
FLIR blackfly s industrial camera: synchronous shooting of multiple cameras through external trigger
Scenario practice: quickly build wordpress blog system based on function calculation
Command injection of cisp-pte
Redis configuration class redisconfig
The cradle of eternity
Recognition of C language array
New job insights ~ leave the old and welcome the new~
ROS学习(二十)机器人SLAM功能包——rgbdslam的安装与测试
Time synchronization of livox lidar hardware -- PPS method
C语言关于链表的代码看不懂?一篇文章让你拿捏二级指针并深入理解函数参数列表中传参的多种形式
制作带照明的DIY焊接排烟器
POJ 3177 redundant paths POJ 3352 road construction (dual connection)
C language [23] classic interview questions [Part 2]
字符串的相关编程题