当前位置:网站首页>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");边栏推荐
- First experience of JSON learning - the third-party jar package realizes bean, list and map to create JSON format
- XML to map tool class xmlmaputils (tool class V)
- Analyze "C language" [advanced] paid knowledge [End]
- Errors made in the development of merging the quantity of data in the set according to attributes
- When grep looks for a process, it ignores the grep process itself
- Cat recycling bin
- Box stretch and pull (left-right mode)
- [unique] what is the [chain storage structure]?
- Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 1)
- AcWing 345. Cattle station solution (nature and multiplication of Floyd)
猜你喜欢

3D激光SLAM:Livox激光雷达硬件时间同步

一片葉子兩三萬?植物消費爆火背後的“陽謀”

ROS学习(25)rviz plugin插件

爬虫实战(六):爬笔趣阁小说

Analyze "C language" [advanced] paid knowledge [End]

Appium automation test foundation uiautomatorviewer positioning tool

JVM memory model

Flir Blackfly S 工业相机:通过外部触发实现多摄像头同步拍摄

Flir Blackfly S 工业相机:自动曝光配置及代码

MySQL execution process and sequence
随机推荐
How did partydao turn a tweet into a $200million product Dao in one year
New job insights ~ leave the old and welcome the new~
AcWing 1142. Busy urban problem solving (minimum spanning tree)
3D激光SLAM:Livox激光雷达硬件时间同步
红外相机:巨哥红外MAG32产品介绍
2022/0524/bookstrap
AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
C language [23] classic interview questions [Part 2]
Blue Bridge Cup 2022 13th provincial competition real topic - block painting
centos8安裝mysql報錯:The GPG keys listed for the “MySQL 8.0 Community Server“ repository are already ins
糊涂工具类(hutool)post请求设置body参数为json数据
Reptile practice (VI): novel of climbing pen interesting Pavilion
Shell script quickly counts the number of lines of project code
How can I code for 8 hours without getting tired.
Time synchronization of livox lidar hardware -- PPS method
LeetCode. Sword finger offer 62 The last remaining number in the circle
uva 1401 dp+Trie
Hutool post requests to set the body parameter to JSON data
MySQL's most basic select statement
Modify the system time of Px4 flight control