当前位置:网站首页>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");
边栏推荐
猜你喜欢
Analyze "C language" [advanced] paid knowledge [End]
ROS学习(十九)机器人SLAM功能包——cartographer
Flir Blackfly S 工业相机 介绍
AcWing 345. Cattle station solution (nature and multiplication of Floyd)
Set WordPress pseudo static connection (no pagoda)
Stm32f4 --- general timer update interrupt
【唯一】的“万字配图“ | 讲透【链式存储结构】是什么?
ROS learning (22) TF transformation
New job insights ~ leave the old and welcome the new~
LeetCode. Sword finger offer 62 The last remaining number in the circle
随机推荐
刨析《C语言》【进阶】付费知识【完结】
The cradle of eternity
RC振荡器和晶体振荡器简介
Yiwen takes you into [memory leak]
Flir Blackfly S 工业相机:配置多个摄像头进行同步拍摄
The use of video in the wiper component causes full screen dislocation
ROS学习(26)动态参数配置
Date processing tool class dateutils (tool class 1)
Analyze "C language" [advanced] paid knowledge [II]
刨析《C语言》【进阶】付费知识【二】
红外相机:巨哥红外MAG32产品介绍
First experience of JSON learning - the third-party jar package realizes bean, list and map to create JSON format
Add PDF Title floating window
ROS学习(十九)机器人SLAM功能包——cartographer
Centros 8 installation MySQL Error: The gpg Keys listed for the "MySQL 8.0 Community Server" repository are already ins
Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 1)
Twenty or thirty thousand a leaf? "Yang Mou" behind the explosion of plant consumption
JS Es5 can also create constants?
ROS学习(24)plugin插件
猫猫回收站