当前位置:网站首页>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");边栏推荐
- 初识MySQL
- MySQL execution process and sequence
- centos8安装mysql报错:The GPG keys listed for the “MySQL 8.0 Community Server“ repository are already ins
- NPM install compilation times "cannot read properties of null (reading 'pickalgorithm')“
- 猫猫回收站
- JVM 内存模型
- Treadpoolconfig thread pool configuration in real projects
- First experience of JSON learning - the third-party jar package realizes bean, list and map to create JSON format
- Blackfly s usb3 industrial camera: buffer processing
- ROS learning (XIX) robot slam function package cartographer
猜你喜欢

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

Time synchronization of livox lidar hardware -- PPS method

CISP-PTE之命令注入篇

Recognition of C language array

MySQL execution process and sequence

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

LeetCode. Sword finger offer 62 The last remaining number in the circle

Ros Learning (23) Action Communication Mechanism

Errors made in the development of merging the quantity of data in the set according to attributes

微服务架构介绍
随机推荐
Use nodejs to determine which projects are packaged + released
HDU 4661 message passing (wood DP & amp; Combinatorics)
JS es5 peut également créer des constantes?
AcWing 344. Solution to the problem of sightseeing tour (Floyd finding the minimum ring of undirected graph)
ROS learning (22) TF transformation
ROS learning (23) action communication mechanism
How to use strings as speed templates- How to use String as Velocity Template?
ROS learning (21) robot slam function package -- installation and testing of orbslam
LeetCode. Sword finger offer 62 The last remaining number in the circle
Add PDF Title floating window
MySQL execution process and sequence
How did partydao turn a tweet into a $200million product Dao in one year
ROS learning (26) dynamic parameter configuration
PartyDAO如何在1年内把一篇推文变成了2亿美金的产品DAO
Vingt - trois mille feuilles? "Yang mou" derrière l'explosion de la consommation végétale
centos8 用yum 安装MySQL 8.0.x
Image watermarking, scaling and conversion of an input stream
ROS学习(24)plugin插件
Halcon knowledge: segment_ contours_ XLD operator
Blackfly S USB3工业相机:缓冲区处理