当前位置:网站首页>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");边栏推荐
- How to use strings as speed templates- How to use String as Velocity Template?
- 将截断字符串或二进制数据
- Analyze "C language" [advanced] paid knowledge [II]
- 长安链学习笔记-证书研究之证书模式
- 【LeetCode】Day97-移除链表元素
- 【唯一】的“万字配图“ | 讲透【链式存储结构】是什么?
- Flir Blackfly S USB3 工业相机:计数器和定时器的使用方法
- The mega version model of dall-e MINI has been released and is open for download
- Livox激光雷达硬件时间同步---PPS方法
- Curl command
猜你喜欢

Schedulx v1.4.0 and SaaS versions are released, and you can experience the advanced functions of cost reduction and efficiency increase for free!

How can I code for 8 hours without getting tired.

ROS学习(24)plugin插件

机器人队伍学习方法,实现8.8倍的人力回报

解密函数计算异步任务能力之「任务的状态及生命周期管理」

Flir Blackfly S 工业相机 介绍

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

ROS learning (XX) robot slam function package -- installation and testing of rgbdslam

企业中台建设新路径——低代码平台

阿里云中间件开源往事
随机推荐
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
Recognition of C language array
刨析《C语言》【进阶】付费知识【二】
最近小程序开发记录
ROS learning (XX) robot slam function package -- installation and testing of rgbdslam
Seconds understand the delay and timing function of wechat applet
Schedulx v1.4.0 and SaaS versions are released, and you can experience the advanced functions of cost reduction and efficiency increase for free!
Flir Blackfly S 工业相机:自动曝光配置及代码
ROS学习(21)机器人SLAM功能包——orbslam的安装与测试
Stm32f4 --- general timer update interrupt
Command injection of cisp-pte
【论文阅读|深读】DNGR:Deep Neural Networks for Learning Graph Representations
Analyze "C language" [advanced] paid knowledge [End]
将截断字符串或二进制数据
ROS学习(23)action通信机制
【论文阅读|深读】ANRL: Attributed Network Representation Learning via Deep Neural Networks
新一代云原生消息队列(一)
Ds-5/rvds4.0 variable initialization error
Sensor: introduction of soil moisture sensor (xh-m214) and STM32 drive code
Zhang Ping'an: accelerate cloud digital innovation and jointly build an industrial smart ecosystem