SSH架构,dao层怎么调用HTTPSession中存储的值
如题,
在dao层,怎么调用到HTTPSession中存储的userId
不想从 action传参,因为很多地方调用dao层的方法,传参修改量非常大。
如果不用session,应该用什么能实现这个效果啊?
随时随地能取到userId??
------解决方案--------------------
过滤器里面 把session放到threadlocal中。
package com.shangwang.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 由于Spring 的AOP面向切面编程,与Servlet容器没有任何关联,所以想要获得Session会话比较麻烦。
在Servlet中或struts1中,可以通过ThreadLocal方式将session保存,Spring AOP中获得Session对象。
* @author Administrator
*
*/
public class SysContent {
private static ThreadLocal<HttpServletRequest> requestLocal= new ThreadLocal<HttpServletRequest>();
private static ThreadLocal<HttpServletResponse> responseLocal= new ThreadLocal<HttpServletResponse>();
public static HttpServletRequest getRequest() {
return (HttpServletRequest)requestLocal.get();
}
public static void setRequest(HttpServletRequest request) {
requestLocal.set(request);
}
public static HttpServletResponse getResponse() {
return (HttpServletResponse)responseLocal.get();
}
public static void setResponse(HttpServletResponse response) {
responseLocal.set(response);
}
public static HttpSession getSession() {
return (HttpSession)((HttpServletRequest)requestLocal.get()).getSession();
}
}
过滤器
package com.shangwang.filter;
import
java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.shangwang.util.SysContent;
public class GetContentFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws
IOException,
ServletException {
SysContent.setRequest((HttpServletRequest) arg0);
SysContent.setResponse((HttpServletResponse) arg1);
arg2.doFilter(arg0, arg1);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
这样就可以用sysContent.getSession了。