spring+dwr 客官°小女子只卖身不卖艺 2022-08-19 15:14 187阅读 0赞 最近在研究dwr推技术,想要实现服务器端在某个条件触发时,自动向指定页面传送数据。 以spring+dwr为例,采用maven管理jar包 项目的结构图如下: ![Center][] spring.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <bean id="dwrtest" class="com.bjsxt.dwrtest.Test0"></bean> <bean id="dwrtest2" class="com.bjsxt.dwrtest.Test1"></bean> <bean id="dwrtest3" class="com.bjsxt.dwrtest.SendMsg"></bean> </beans> 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> <allow> <create javascript="T0" creator="spring" > <param name="beanName" value="dwrtest"></param> </create> <create javascript="T1" creator="spring" > <param name="beanName" value="dwrtest2"></param> </create> <create javascript="SendMsg" creator="spring"> <param name="beanName" value="dwrtest3"></param> </create> <convert converter="bean" match="com.bjsxt.bean.User"/> <!-- <param name="include" value="id,name"></param> </convert> --> </allow> </dwr> pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com</groupId> <artifactId>dwrTest</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>dwrTest Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.1.4.RELEASE</spring.version> <jackson.version>2.5.0</jackson.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!-- dwr --> <dependency> <groupId>org.directwebremoting</groupId> <artifactId>dwr</artifactId> <version>3.0.M1</version> </dependency> <!-- common logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!-- json --> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20140107</version> </dependency> </dependencies> <build> <finalName>dwrTest</finalName> </build> </project> User类 package com.bjsxt.bean; public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } } SendMsg类 package com.bjsxt.dwrtest; import java.util.Collection; import org.directwebremoting.ScriptBuffer; import org.directwebremoting.ScriptSession; import org.directwebremoting.WebContext; import org.directwebremoting.WebContextFactory; import org.directwebremoting.proxy.dwr.Util; public class SendMsg { public void sendMsg(String msg){ //得到上下文 WebContext contex = WebContextFactory.get(); //得到要推送到 的页面 dwr3为项目名称 , 一定要加上。 Collection<ScriptSession> sessions = contex.getScriptSessionsByPage("/dwrTest/showMsg.jsp"); //不知道该怎么解释这个 , Util util = new Util(sessions); //下面是创建一个javascript脚本 , 相当于在页面脚本中添加了一句 show(msg); ScriptBuffer sb = new ScriptBuffer(); sb.appendScript("show("); sb.appendData(msg); sb.appendScript(")"); //推送 util.addScript(sb); } } Test0类 package com.bjsxt.dwrtest; import com.bjsxt.bean.User; public class Test0 { public User method1(){ /*JSONObject jsonObj=new JSONObject(); jsonObj.put("id", 12); jsonObj.put("name","longyu");*/ User user=new User(); user.setId(1); user.setName("ly"); return user; } /* public static void main(String[] args) { System.out.println(method1()); }*/ } Test1类 package com.bjsxt.dwrtest; public class Test1 { public String method1(){ return "调用方法111111"; } public String method2(){ return "调用方法222222"; } } jQuery.1.7.1.js下载地址: 项目下载地址: [Center]: /images/20220731/f488c9f05a834e8c8eb98b3800e8a118.png
还没有评论,来说两句吧...