当前位置:网站首页>Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
2022-07-07 02:14:00 【InfoQ】
Web A little trouble in development
public class XXController() {
public void addUser() {
// Get operator
Integer opUserId = Integer.parseInt(getHeader("opUserId"));
// Get other parameters ...
service.addUser(...., opUserId);
renderAppMsg(" Add user successfully ");
}
}
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) {
// ...
}
}
reflection
Use in interceptor ThreadLocal Staging request parameters
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();
}
}
Using parameter
String para = RequestPool.localRequest.get().getParameter("username");
边栏推荐
- Chang'an chain learning notes - certificate model of certificate research
- 强化学习如何用于医学影像?埃默里大学最新《强化学习医学影像分析》综述,阐述最新RL医学影像分析概念、应用、挑战与未来方向
- 15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
- Dall-E Mini的Mega版本模型发布,已开放下载
- Livox激光雷达硬件时间同步---PPS方法
- Add PDF Title floating window
- 1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
- Make DIY welding smoke extractor with lighting
- 机器人队伍学习方法,实现8.8倍的人力回报
- 【论文阅读|深读】ANRL: Attributed Network Representation Learning via Deep Neural Networks
猜你喜欢
Batch delete data in SQL - set in entity
Flir Blackfly S USB3 工业相机:计数器和定时器的使用方法
Livox激光雷达硬件时间同步---PPS方法
低代码平台中的数据连接方式(上)
How can I code for 8 hours without getting tired.
[unique] what is the [chain storage structure]?
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
开发中对集合里面的数据根据属性进行合并数量时犯的错误
Integrated navigation: product description and interface description of zhonghaida inav2
STM32F4---通用定时器更新中断
随机推荐
How to use strings as speed templates- How to use String as Velocity Template?
FLIR blackfly s industrial camera: explanation and configuration of color correction and code setting method
ROS学习(23)action通信机制
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
Recognition of C language array
Domestic images of various languages, software and systems. It is enough to collect this warehouse: Thanks mirror
First experience of JSON learning - the third-party jar package realizes bean, list and map to create JSON format
低代码平台中的数据连接方式(上)
Batch delete data in SQL - set in entity
The foreground downloads network pictures without background processing
Flir Blackfly S工业相机:颜色校正讲解及配置与代码设置方法
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
Flir Blackfly S 工业相机 介绍
3D激光SLAM:Livox激光雷达硬件时间同步
Scenario practice: quickly build wordpress blog system based on function calculation
Redis configuration class redisconfig
String to date object
建議收藏!!Flutter狀態管理插件哪家强?請看島上碼農的排行榜!
Jacob Steinhardt, assistant professor of UC Berkeley, predicts AI benchmark performance: AI has made faster progress in fields such as mathematics than expected, but the progress of robustness benchma
MySQL's most basic select statement