我想在request的上下文中存一些数据,有没有现成的类可以用的?
9 回复
这是我自己包装的类,主要是基础参数的处理,这里已经把有问题的那一行注释掉了
public class ClientUtils
{
private static final ClientUtils instance = new ClientUtils();
private static final ThreadLocal<ClientInfo> clientInfos = new ThreadLocal<ClientInfo>();
private ClientUtils(){}
public static ClientUtils me(){
return instance;
}
public ClientInfo getClientInfo(){
// ClientInfo clientInfo = clientInfos.get();
//不同的请求,会共用一个thread,导致有些请求参数没有更新,每次从请求取一遍算了
ClientInfo clientInfo = null;
if(clientInfo == null){
clientInfo = new ClientInfo();
HttpServletRequest request = Mvcs.getActionContext().getRequest();
if(request != null){
clientInfo.setIp(request.getParameter("ip"));
clientInfo.setCityCode(request.getParameter("cityCode"));
clientInfo.setUa(request.getHeader("user-agent"));
String lon = request.getParameter("lon");
String lat = request.getParameter("lat");
if(StringUtils.isNotBlank(lon) && StringUtils.isNotBlank(lat)){
clientInfo.setLon(Double.valueOf(lon));
clientInfo.setLat(Double.valueOf(lat));
}
if(StringUtils.isBlank(clientInfo.getIp())){
clientInfo.setIp(getRemoteHost(request));
}
}
clientInfos.set(clientInfo);
}
return clientInfo;
}
}
添加回复
请先登陆