当前位置:网站首页>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");
边栏推荐
- Word wrap when flex exceeds width
- Flir Blackfly S 工业相机:通过外部触发实现多摄像头同步拍摄
- centos8安装mysql报错:The GPG keys listed for the “MySQL 8.0 Community Server“ repository are already ins
- ROS学习(23)action通信机制
- Flir Blackfly S 工业相机:自动曝光配置及代码
- Integrated navigation: product description and interface description of zhonghaida inav2
- 处理streamlit库上传的图片文件
- 微服务架构介绍
- Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem
- ROS learning (21) robot slam function package -- installation and testing of orbslam
猜你喜欢
Correct use of BigDecimal
Vingt - trois mille feuilles? "Yang mou" derrière l'explosion de la consommation végétale
Errors made in the development of merging the quantity of data in the set according to attributes
Sensor: introduction of soil moisture sensor (xh-m214) and STM32 drive code
大咖云集|NextArch基金会云开发Meetup来啦!
Scenario practice: quickly build wordpress blog system based on function calculation
Shell script quickly counts the number of lines of project code
UC伯克利助理教授Jacob Steinhardt预测AI基准性能:AI在数学等领域的进展比预想要快,但鲁棒性基准性能进展较慢
@Before, @after, @around, @afterreturning execution sequence
Make DIY welding smoke extractor with lighting
随机推荐
Flir Blackfly S工业相机:颜色校正讲解及配置与代码设置方法
Chang'an chain learning notes - certificate model of certificate research
Freeswitch dials extension number source code tracking
Introduction to microservice architecture
Big guys gather | nextarch foundation cloud development meetup is coming!
Processing image files uploaded by streamlit Library
CISP-PTE实操练习讲解(二)
6 seconds to understand the book to the Kindle
处理streamlit库上传的图片文件
低代码平台中的数据连接方式(上)
Robot team learning method to achieve 8.8 times human return
ROS learning (22) TF transformation
ROS学习(21)机器人SLAM功能包——orbslam的安装与测试
ROS学习(22)TF变换
Several classes and functions that must be clarified when using Ceres to slam
Scenario practice: quickly build wordpress blog system based on function calculation
机器人队伍学习方法,实现8.8倍的人力回报
ROS学习(23)action通信机制
Date processing tool class dateutils (tool class 1)
Unicode string converted to Chinese character decodeunicode utils (tool class II)