jw的github地址是https://github.com/menyouping/jw
现在IndexController中的方法都只有两个参数,request和response,和Servlet的参数保持一致。但是,这个很不Spring啊!@RequestParam,@PathValue,@ModelAtrribute,Model都嗨起来!要把这几个嗨起来等慢慢来,本篇介绍一个最简单也是最基本的,每个请求维护一个上下文,把request,response,session都单独保存在SessionContext中, 也可以保存其他信息。
新增类jw-core/src/main/java/com/jw/util/SessionContext.java
package com.jw.util;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
*
* @author jay
*
*/
public class SessionContext implements java.io.Serializable {
private static final long serialVersionUID = -8726882324373214664L;
public final static String REQUEST = "request";
public final static String RESPONSE = "response";
public final static String SESSION = "session";
private final static ThreadLocal<SessionContext> context = new ThreadLocal<SessionContext>();
private Map<String, Object> map = new HashMap<String, Object>();
public static SessionContext buildContext() {
context.set(new SessionContext());
return context.get();
}
public static void setContext(SessionContext ctx) {
context.set(ctx);
}
public static SessionContext getContext() {
return context.get();
}
public static void removeContext() {
context.remove();
}
public SessionContext() {
}
public static HttpServletRequest getRequest() {
return (HttpServletRequest) getContext().map.get(REQUEST);
}
public static HttpServletResponse getResponse() {
return (HttpServletResponse) getContext().map.get(RESPONSE);
}
public static HttpSession getSession() {
return (HttpSession) getContext().map.get(SESSION);
}
public boolean containsKey(String key) {
return map.containsKey(key);
}
public void put(String key, Object value) {
map.put(key, value);
}
public SessionContext set(String key, Object value) {
map.put(key, value);
return this;
}
public Object get(String key) {
return map.get(key);
}
public Object get(String key, Object defaultValue) {
if (map.containsKey(key))
return map.get(key);
return defaultValue;
}
public String getString(String key) {
return (String) map.get(key);
}
public String getString(String key, String defaultValue) {
return map.containsKey(key) ? getString(key) : defaultValue;
}
public boolean getBoolean(String key) {
return (Boolean) map.get(key);
}
public boolean getBoolean(String key, boolean defaultValue) {
return map.containsKey(key) ? getBoolean(key) : defaultValue;
}
public int getInt(String key) {
return (Integer) map.get(key);
}
public int getInt(String key, int defaultValue) {
return map.containsKey(key) ? getInt(key) : defaultValue;
}
}
修改类jw-core/src/main/java/com/jw/web/servlet/DispatcherServlet.java
保存request,response,session等信息
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String appName = request.getSession().getServletContext().getContextPath();
request.setAttribute("root", appName);// 在jsp页面中添加root变量
//...
String[] paths = path.split("/", 2);
// build context
SessionContext.buildContext()
.set(SessionContext.REQUEST, request)
.set(SessionContext.RESPONSE, response)
.set(SessionContext.SESSION, request.getSession())
.set("requestUrl", path);
String controllerClazeName = ConfigUtils.getProperty("package.scan") + "." + StringUtils.upperFirst(paths[0])
+ "Controller";
String methodName = paths[1];
//...
}
上下文已经准备好了,本篇结束。