标签:
1.web.xml中添加如下代码:
<!-- 配置DWR前端控制器 --> <servlet> <servlet-name>dwrServlet</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <!-- 指定配置文件 --> <init-param> <param-name>config</param-name> <!-- 如果有多个用","分开 --> <param-value>WEB-INF/classes/dwr.xml</param-value> </init-param> <init-param> <param-name>debug</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>activeReverseAjaxEnabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>initApplicationScopeCreatorsAtStartup</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>maxWaitAfterWrite</param-name> <param-value>500</param-value> </init-param> <init-param> <param-name>logLevel</param-name> <param-value>WARN</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwrServlet</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
2.新建dwr.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> <!-- 通用dwr配置 --> <dwr> <allow> <!-- 从Spring中获取Java对象 --> <create javascript="talk" creator="spring"> <param name="beanName" value="talkService"></param> </create> <!-- <create javascript="talk" creator="new"> <param name="beanName" value="com.dailywork.service.impl.TalkServiceImpl"></param> </create> --> <!-- 指定针对于特定对象的转换器 --> <convert match="entity.*" converter="bean"></convert> <convert match="java.lang.Throwable" converter="bean"> <param name="include" value="message"></param> </convert> </allow> </dwr>
3.applicationContext.xml中配置bean
<bean id="talkAction" class="com.dailywork.action.TalkAction"></bean>
4.需要推送数据的java中
//java推送到jsp public void talk(){ HttpSession session = WebContextFactory.get().getSession(); WebContext contex = WebContextFactory.get(); Collection<ScriptSession> scriptSessions = contex.getScriptSessionsByPage(contex.getCurrentPage()); ScriptSession scriptSession = WebContextFactory.get().getScriptSession(); //像当前session推送 Util util = new Util(scriptSession); ScriptBuffer sb = new ScriptBuffer(); sb.appendScript("show("); sb.appendData("欢迎" + session.getAttribute("username") + "进入对话"); sb.appendScript(")"); util.addScript(sb); //像所有session推送 Util utilAll = new Util(scriptSessions); ScriptBuffer sbAll = new ScriptBuffer(); sbAll.appendScript("show("); sbAll.appendData(session.getAttribute("username") + "进入了对话"); sbAll.appendScript(")"); utilAll.addScript(sbAll); } //jsp请求java public String 方法名(参数){ return 返回值 }
5.被推送的jsp中:
<script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript" src="dwr/util.js"></script> <script type="text/javascript" src="dwr/interface/talk.js"></script> <script> dwr.engine._execute("dwr", ‘talk‘, ‘这里填java中的方法‘, ‘参数‘, {callback:function(data){data}}); function show(data){ console.log(data); } </script>
标签:
原文地址:http://my.oschina.net/chenhao901007/blog/373077