当前位置:网站首页>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");
边栏推荐
- ROS学习(21)机器人SLAM功能包——orbslam的安装与测试
- Flir Blackfly S工业相机:颜色校正讲解及配置与代码设置方法
- Mysqlbackup restores specific tables
- FLIR blackfly s usb3 industrial camera: white balance setting method
- ROS learning (25) rviz plugin
- Zabbix 5.0:通过LLD方式自动化监控阿里云RDS
- JS ES5也可以創建常量?
- Make DIY welding smoke extractor with lighting
- 永久的摇篮
- MySQL execution process and sequence
猜你喜欢
3D激光SLAM:Livox激光雷达硬件时间同步
Flir Blackfly S 工业相机:配置多个摄像头进行同步拍摄
Set WordPress pseudo static connection (no pagoda)
Blackfly s usb3 industrial camera: buffer processing
AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
永久的摇篮
Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 2)
ROS learning (26) dynamic parameter configuration
Mongodb checks whether the table is imported successfully
Centros 8 installation MySQL Error: The gpg Keys listed for the "MySQL 8.0 Community Server" repository are already ins
随机推荐
Date processing tool class dateutils (tool class 1)
Time synchronization of livox lidar hardware -- PPS method
ROS learning (21) robot slam function package -- installation and testing of orbslam
Appium automation test foundation uiautomatorviewer positioning tool
AcWing 345. Cattle station solution (nature and multiplication of Floyd)
【LeetCode】Day97-移除链表元素
Mongodb checks whether the table is imported successfully
刨析《C语言》【进阶】付费知识【一】
Analyze "C language" [advanced] paid knowledge [II]
刨析《C语言》【进阶】付费知识【二】
Compile command line terminal swift
ROS学习(21)机器人SLAM功能包——orbslam的安装与测试
centos8安裝mysql報錯:The GPG keys listed for the “MySQL 8.0 Community Server“ repository are already ins
uva 1401 dp+Trie
2022/0524/bookstrap
AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
刨析《C语言》【进阶】付费知识【完结】
STM32F4---通用定时器更新中断
JVM memory model
The use of video in the wiper component causes full screen dislocation