81,118
社区成员




package com.gem.struts.core;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
public class ActionContext {
private ThreadLocal<Map<String,Object>> threadLocal = new ThreadLocal<>();
private ActionContext() {
Map<String,Object> map = new HashMap<>();
threadLocal.set(map);
}
private static ActionContext instance = new ActionContext();
public static ActionContext getInstance() {
return instance;
}
public void setRequest(HttpServletRequest request) {
threadLocal.get().put("request",request);
}
public void setResponse(HttpServletResponse response) {
threadLocal.get().put("response",response);
}
public HttpServletRequest getRequest() {
return (HttpServletRequest) threadLocal.get().get("request");
}
public HttpServletResponse getResponse() {
return (HttpServletResponse) threadLocal.get().get("response");
}
}
package com.gem.struts.core;
import com.gem.fruit.util.StringUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
/**
* 中央控制器
*/
@WebServlet(name = "PrepareAndExecuteServlet", urlPatterns = "*.action")
public class PrepareAndExecuteServlet extends HttpServlet {
private ActionMapper mapper = new ActionMapper();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
ActionContext.getInstance().setRequest(request);
ActionContext.getInstance().setResponse(response);
//获取统一 request 参数 保存在 Map 中去
Map<String, String> paramsMap = showParams(request);
String servletPath = request.getServletPath();
if (StringUtil.isNoEmpty(servletPath)) {
String path = servletPath.substring(1);
int index = path.lastIndexOf(".action");
path = path.substring(0, index);
//获取 path 所对应的 action 类 实例对象
Object actionObj = mapper.getMap().get(path);
//回调set方法 将对象的
invokeSetMethod(actionObj, paramsMap);
if (actionObj != null) {
ReflectUtil.invokeMethod(actionObj, "execute");
} else {
response.sendRedirect("error.jsp");
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
private void invokeSetMethod(Object actionObj, Map<String, String> params) {
Set<String> keySet = params.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String propertyName = iterator.next();
String propertyValue = params.get(propertyName);
String setMethodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
ReflectUtil.invokeMethod(actionObj, setMethodName, propertyValue);
}
}
private Map<String, String> showParams(HttpServletRequest request) {
Map<String, String> params = new HashMap<>();
Enumeration<String> parameterNames = request.getParameterNames();
while (parameterNames.hasMoreElements()) {
String paramName = parameterNames.nextElement();
String paramValue = request.getParameter(paramName);
params.put(paramName, paramValue);
}
return params;
}
}
这里是我 PrepareAndExecuteServlet 页面package question;
import java.util.HashMap;
import java.util.Map;
public class Tests {
public static void main(String[] args) {
ActionContext.getInstance().setRequest(1);
ActionContext.getInstance().setRequest(2);
new Thread() {
@Override
public void run() {
ActionContext.getInstance().setRequest(3);
}
}.start();
}
}
class ActionContext {
private ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<>();
private ActionContext() {
Map<String, Object> map = new HashMap<>();
threadLocal.set(map);
}
private static ActionContext instance = new ActionContext();
public static ActionContext getInstance() {
return instance;
}
public void setRequest(Object request) {
Map<String, Object> map = threadLocal.get();
System.out.println(map);
map.put("request", request);
}
public void setResponse(Object response) {
threadLocal.get().put("response", response);
}
}
Exception in thread "Thread-0" java.lang.NullPointerException
at question.ActionContext.setRequest(Tests.java:38)
at question.Tests$1.run(Tests.java:15)