dwr 反推 ScriptSessionListener 不起作用?

javaersummer 2015-03-04 09:56:46
框架:struts22+ spring+ mybatis + dwr
tomcat启动时初始化了dwr ScriptSessionManager监听器,在登陆时把用户id放入session中后ScriptSessionManager 没有监听,大神帮我看下!
web.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>jsp/frame/index.jsp</welcome-file>
</welcome-file-list>
<!-- Spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- dwr test -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<!-- 开启后将可以在浏览器里输入 (http://localhost:8080/dwrdemo/dwr)来查看可用自动生成的js -->
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>allowScriptTagRemoting</param-name>
<param-value>true</param-value>
</init-param>
<!-- 这里设定1是为了让DwrServlet 在 InitServlet之前启动,不然无法初始化InitServlet中的监听器-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!--
<servlet>
<servlet-name>DWRTestServlet</servlet-name>
<servlet-class>dwr.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DWRTestServlet</servlet-name>
<url-pattern>/DWRTestServlet</url-pattern>
</servlet-mapping>
-->

<!-- Struts2的系统过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 启动一个dwr scriptsession的监听器,用于监听scriptsession的创建和销毁-->
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>com.test.InitServlet</servlet-class>
<load-on-startup>100</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>InitServlet</servlet-name>
<url-pattern>/InitServlet</url-pattern>
</servlet-mapping>
</web-app>

web.xml 中监听器 scriptsession InitServlet的代码
package com.test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;

import org.directwebremoting.Container;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.event.ScriptSessionEvent;
import org.directwebremoting.event.ScriptSessionListener;
import org.directwebremoting.extend.ScriptSessionManager;
import org.directwebremoting.servlet.DwrServlet;


/**
* 当启动tomcat时候启动该servlet容器
*/
public class InitServlet extends DwrServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public InitServlet() {
super();
// TODO Auto-generated constructor stub
}

@Override
public void init() throws ServletException {
initDWRListener();
}

/**
* 初始化dwr监听器,用于将用户id与dwr的session进行绑定
*/
public void initDWRListener() {
System.out.println("init DWR Listener success");
Container container = ServerContextFactory.get().getContainer();
ScriptSessionManager manager = container.getBean(ScriptSessionManager.class);
ScriptSessionListener listener = new ScriptSessionListener() {
public void sessionCreated(ScriptSessionEvent ev) {
HttpSession session = WebContextFactory.get().getSession();
String user = (String) session.getAttribute("username");
ev.getSession().removeAttribute("username");
ev.getSession().setAttribute("username", user);
//TaskPush.sessionManager.put(session, ev.getSession());
System.out.println("a scriptsession create------------------------------------------------------------------------,"
+ user); //打印没有效果

}

public void sessionDestroyed(ScriptSessionEvent ev) {
}
};
manager.addScriptSessionListener(listener);
}
}

登陆时验证成功后在session中放入userid
response.getWriter().print("1");
Map session = ActionContext.getContext().getSession();
session.put("username",username);
ServletActionContext.getRequest().getSession().setAttribute("username", username) ;
System.out.println(ServletActionContext.getRequest().getSession().getAttribute("username"));

放入后没有监控到session的变化,求大神帮忙看下,不胜感激!!!
...全文
128 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧