DAO层和Service层访问session
2014-01-20 22:59
260 查看
多情况下,我们需要在DAO或者Service层拿到Session中的值,比如下面这个应用,session中存放了当前用户的账号,在DAO层中需要insert一条record,这条record需要记录当前用户(该记录是由谁创建的),对于这样的应用,我们一般可以在Action层中通过request拿到session里的用户账号,然后传入service,再传入DAO层,就可以解决了。
今天,我在这里记录一种方式,利用ThreadLocal来存入sesion,然后可以在任何业务层,DAO层获取Session的方式,首先建立一个CSession来存放session的值,只放了2个属性,用户的账号和姓名
Java代码
public class CSession {
private String username;
private String userId;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
建立SessionUser类,用来存放在整个运行期CSession的存在
Java代码
public class SessionUser {
@SuppressWarnings("unchecked")
static ThreadLocal sessionUser =
new ThreadLocal();
@SuppressWarnings("unchecked")
public static
void setSessionUser(CSession cSession) {
sessionUser.set(cSession);
}
public static CSession getSessionUser(){
return (CSession )sessionUser.get();
}
public static String getSessionUserId(){
return getSessionUser().getUserId();
}
public static String getSessionUserName(){
return getSessionUser().getUsername();
}
}
在登录的Action里,登录成功后,加Session里的用户信息,放入CSession中,
Java代码
HttpSession session = request.getSession(true);
CSession cs = new CSession();
cs.setUserId(userId);
cs.setUsername(userName);
session.setAttribute("C_SESSION",cs);
最后,在session check的Filter中,把CSession注入到SessionUser中,
Java代码
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest hrequest = (HttpServletRequest) request;
HttpServletResponse hresponse = (HttpServletResponse) response;
.......
CSession cs = (CSession) hrequest.getSession(true).getAttribute("C_SESSION");
SessionUser.setSessionUser(cs);
.......
}
下面我们就可以再DAO层中直接从SessionUser中获取 userid 和 username,
Java代码
xxxTO.setUserId(SessionUser.getSessionUserId()); xxxTO.setUserName(SessionUser.getSessionUserName());
页面上,
Java代码
<bean:write name="C_SESSION" property="username"/>
<bean:write name="C_SESSION" property="userId"/>
今天,我在这里记录一种方式,利用ThreadLocal来存入sesion,然后可以在任何业务层,DAO层获取Session的方式,首先建立一个CSession来存放session的值,只放了2个属性,用户的账号和姓名
Java代码
public class CSession {
private String username;
private String userId;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
public class CSession { private String username; private String userId; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } }
建立SessionUser类,用来存放在整个运行期CSession的存在
Java代码
public class SessionUser {
@SuppressWarnings("unchecked")
static ThreadLocal sessionUser =
new ThreadLocal();
@SuppressWarnings("unchecked")
public static
void setSessionUser(CSession cSession) {
sessionUser.set(cSession);
}
public static CSession getSessionUser(){
return (CSession )sessionUser.get();
}
public static String getSessionUserId(){
return getSessionUser().getUserId();
}
public static String getSessionUserName(){
return getSessionUser().getUsername();
}
}
public class SessionUser { @SuppressWarnings("unchecked") static ThreadLocal sessionUser = new ThreadLocal(); @SuppressWarnings("unchecked") public static void setSessionUser(CSession cSession) { sessionUser.set(cSession); } public static CSession getSessionUser(){ return (CSession )sessionUser.get(); } public static String getSessionUserId(){ return getSessionUser().getUserId(); } public static String getSessionUserName(){ return getSessionUser().getUsername(); } }
在登录的Action里,登录成功后,加Session里的用户信息,放入CSession中,
Java代码
HttpSession session = request.getSession(true);
CSession cs = new CSession();
cs.setUserId(userId);
cs.setUsername(userName);
session.setAttribute("C_SESSION",cs);
HttpSession session = request.getSession(true); CSession cs = new CSession(); cs.setUserId(userId); cs.setUsername(userName); session.setAttribute("C_SESSION",cs);
最后,在session check的Filter中,把CSession注入到SessionUser中,
Java代码
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest hrequest = (HttpServletRequest) request;
HttpServletResponse hresponse = (HttpServletResponse) response;
.......
CSession cs = (CSession) hrequest.getSession(true).getAttribute("C_SESSION");
SessionUser.setSessionUser(cs);
.......
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest hrequest = (HttpServletRequest) request; HttpServletResponse hresponse = (HttpServletResponse) response; ....... CSession cs = (CSession) hrequest.getSession(true).getAttribute("C_SESSION"); SessionUser.setSessionUser(cs); ....... }
下面我们就可以再DAO层中直接从SessionUser中获取 userid 和 username,
Java代码
xxxTO.setUserId(SessionUser.getSessionUserId()); xxxTO.setUserName(SessionUser.getSessionUserName());
xxxTO.setUserId(SessionUser.getSessionUserId()); xxxTO.setUserName(SessionUser.getSessionUserName());
页面上,
Java代码
<bean:write name="C_SESSION" property="username"/>
<bean:write name="C_SESSION" property="userId"/>
<bean:write name="C_SESSION" property="username"/> <bean:write name="C_SESSION" property="userId"/>
相关文章推荐
- yum的使用示例
- 如何远程连接他人的电脑
- svn 错误 以及 中文翻译
- 从java的HelloWorld我们能学到什么?
- AFNetWorking 在请求时出现code=-1016错误解决办法
- JavaScript模态窗口传值刷新页面
- Java Web开发字符编码小记
- validateJarFile jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class
- GCC __attribute__((constructor)|(destructor))
- asp获取文件名和扩展名的函数代码
- 豪迪qq群发器2014破解版去除尾巴注册机2014最新qq群发
- Codeforces 379D - New Year Letter
- Stanford-CV华人教授李飞飞写给她学生的一封信,如何做好研究以及写好PAPER,受益匪浅
- SqlServer 杂记 不断补充中
- win7设置定时关机
- 3718. 批改作业 不难 开了个二维数组
- 【rails3教材】博客构建过程2
- bash 定时任务
- ProGuard的Maven插件配置
- MongoDB