- 浏览: 3420449 次
- 性别:
- 来自: 珠海
文章分类
- 全部博客 (1633)
- Java (250)
- Android&HTML5 (111)
- Struts (10)
- Spring (236)
- Hibernate&MyBatis (115)
- SSH (49)
- jQuery插件收集 (55)
- Javascript (145)
- PHP (77)
- REST&WebService (18)
- BIRT (27)
- .NET (7)
- Database (105)
- 设计模式 (16)
- 自动化和测试 (19)
- Maven&Ant (43)
- 工作流 (36)
- 开源应用 (156)
- 其他 (16)
- 前台&美工 (119)
- 工作积累 (0)
- OS&Docker (83)
- Python&爬虫 (28)
- 工具软件 (157)
- 问题收集 (61)
- OFbiz (6)
- noSQL (12)
最新评论
-
HEZR曾嶸:
你好博主,这个不是很理解,能解释一下嘛//左边+1,上边+1, ...
java 两字符串相似度计算算法 -
天使建站:
写得不错,可以看这里,和这里的这篇文章一起看,有 ...
jquery 遍历对象、数组、集合 -
xue88ming:
很有用,谢谢
@PathVariable映射出现错误: Name for argument type -
jnjeC:
厉害,困扰了我很久
MyBatis排序时使用order by 动态参数时需要注意,用$而不是# -
TopLongMan:
非常好,很实用啊。。
PostgreSQL递归查询实现树状结构查询
官方文档:WebSocket Support
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html
WebSocket结合hibernate+spring+json 实际经验 http://www.cnblogs.com/lucious/archive/2013/06/05/3118394.html
Spring Framework 4.0 M2 中的 WebSocket 消息架构 http://www.oschina.net/translate/spring-framework-4-0-m2-websocket-messaging-architectures
Spring 4.0 中的WebSocket 架构 http://developer.51cto.com/art/201311/418069.htm
spring4 + WebSocket笔记 http://blog.csdn.net/sl543001/article/details/19343005
基于Tomcat7、Java、WebSocket的服务器推送聊天室 http://blog.csdn.net/leecho571/article/details/9707497
Using websockets in Java using Spring 4 http://syntx.io/using-websockets-in-java-using-spring-4/
STOCK TICKER DEMO WEBAPP USING SPRING 4 WEBSOCKET http://gerrydevstory.com/2014/03/04/stock-ticker-demo-webapp-using-spring-4-websocket/
Using Spring 4 WebSocket, sockJS and Stomp support to implement two way server client communication
http://raymondhlee.wordpress.com/2014/01/19/using-spring-4-websocket-sockjs-and-stomp-support-to-implement-two-way-server-client-communication/
Set up a full broker for Spring 4 STOMP over WebSocket messaging using ActiveMQ http://raymondhlee.wordpress.com/2014/04/12/set-up-a-full-broker-for-spring-4-stomp-over-websocket-messaging-using-activemq/
spring-websocket-portfolio源码 https://github.com/rstoyanchev/spring-websocket-portfolio
WebSocket的JavaScript例子 http://www.xyhtml5.com/websocket-javascript-example.html
默认配置:
自定义配置:
The "STOMP broker relay" in the above configuration is a Spring MessageHandler that handles messages by forwarding them to an external message broker. To do so it establishes TCP connections to the broker, forwards all messages to it, and reversely forwards all messages received from the broker to clients through their WebSocket sessions. Essentially it acts as a "relay" forwarding messages in both directions.
一个例子的源码
附件是两个用到的两个js
web.xml
========================================
log4j.xml
===================================
log4j.properties
===========================================
log4j.rootCategory=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
log4j.appender.stdout.Threshold=trace
log4j.category.org.hibernate.SQL=trace
log4j.category.org.hibernate.type=trace
# log4jdbc
log4j.logger.jdbc.sqlonly=INFO
log4j.logger.jdbc.sqltiming=INFO
log4j.logger.jdbc.audit=OFF
log4j.logger.jdbc.resultset=OFF
log4j.logger.jdbc.connection=OFF
jdbc.properties
==========================================
#mysql
#jdbc.dataSource=org.logicalcobwebs.proxool.ProxoolDataSource
#jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/jbpmtest?useUnicode=true&characterEncoding=utf8
#log4jdbc
jdbc.dataSource=org.springframework.jdbc.datasource.DriverManagerDataSource
jdbc.driver=net.sf.log4jdbc.DriverSpy
jdbc.url=jdbc:log4jdbc:mysql://localhost:3306/jbpmtest?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
sqlserver 2008
#jdbc.dataSource=com.alibaba.druid.pool.DruidDataSource
#jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
#jdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=jbpmtest
#jdbc.username=sa
#jdbc.password=sa
proxool.alias=ds
proxool.prototypeCount=2
proxool.minimumConnectionCount=5
proxool.maximumConnectionCount=20
proxool.houseKeepingTestSql=select 1
applicationContext.xml
========================================
applicationContext-spring.xml
==========================================
springmvc-servlet.xml
======================================
三个domain
==============================================
两个服务器处理信息的controller
==========================================
两个测试客户端
index.jsp
================================================
hello.jsp
=================================================
附加:pom.xml
===================================
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html
WebSocket结合hibernate+spring+json 实际经验 http://www.cnblogs.com/lucious/archive/2013/06/05/3118394.html
Spring Framework 4.0 M2 中的 WebSocket 消息架构 http://www.oschina.net/translate/spring-framework-4-0-m2-websocket-messaging-architectures
Spring 4.0 中的WebSocket 架构 http://developer.51cto.com/art/201311/418069.htm
spring4 + WebSocket笔记 http://blog.csdn.net/sl543001/article/details/19343005
基于Tomcat7、Java、WebSocket的服务器推送聊天室 http://blog.csdn.net/leecho571/article/details/9707497
Using websockets in Java using Spring 4 http://syntx.io/using-websockets-in-java-using-spring-4/
STOCK TICKER DEMO WEBAPP USING SPRING 4 WEBSOCKET http://gerrydevstory.com/2014/03/04/stock-ticker-demo-webapp-using-spring-4-websocket/
Using Spring 4 WebSocket, sockJS and Stomp support to implement two way server client communication
http://raymondhlee.wordpress.com/2014/01/19/using-spring-4-websocket-sockjs-and-stomp-support-to-implement-two-way-server-client-communication/
Set up a full broker for Spring 4 STOMP over WebSocket messaging using ActiveMQ http://raymondhlee.wordpress.com/2014/04/12/set-up-a-full-broker-for-spring-4-stomp-over-websocket-messaging-using-activemq/
spring-websocket-portfolio源码 https://github.com/rstoyanchev/spring-websocket-portfolio
WebSocket的JavaScript例子 http://www.xyhtml5.com/websocket-javascript-example.html
默认配置:
<websocket:message-broker application-destination-prefix="/app"> <websocket:stomp-endpoint path="/hello"> <websocket:sockjs/> </websocket:stomp-endpoint> <websocket:simple-broker prefix="/topic,/queue"/> </websocket:message-broker>
自定义配置:
<websocket:message-broker application-destination-prefix="/app"> <websocket:stomp-endpoint path="/hello"> <websocket:sockjs/> </websocket:stomp-endpoint> <websocket:stomp-broker-relay prefix="/topic,/queue" relay-host="localhost" relay-port="61613" heartbeat-send-interval="20000" heartbeat-receive-interval="20000"/> </websocket:message-broker>
The "STOMP broker relay" in the above configuration is a Spring MessageHandler that handles messages by forwarding them to an external message broker. To do so it establishes TCP connections to the broker, forwards all messages to it, and reversely forwards all messages received from the broker to clients through their WebSocket sessions. Essentially it acts as a "relay" forwarding messages in both directions.
一个例子的源码
附件是两个用到的两个js
web.xml
========================================
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
log4j.xml
===================================
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <!-- Appenders --> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p: %c - %m%n" /> </layout> </appender> <!-- Application Loggers --> <logger name="com"> <level value="info" /> </logger> <!-- 3rdparty Loggers --> <logger name="org.springframework.core"> <level value="info" /> </logger> <logger name="org.springframework.beans"> <level value="info" /> </logger> <logger name="org.springframework.context"> <level value="info" /> </logger> <logger name="org.springframework.web"> <level value="info" /> </logger> <!-- Root Logger --> <root> <priority value="warn" /> <appender-ref ref="console" /> </root> </log4j:configuration>
log4j.properties
===========================================
log4j.rootCategory=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
log4j.appender.stdout.Threshold=trace
log4j.category.org.hibernate.SQL=trace
log4j.category.org.hibernate.type=trace
# log4jdbc
log4j.logger.jdbc.sqlonly=INFO
log4j.logger.jdbc.sqltiming=INFO
log4j.logger.jdbc.audit=OFF
log4j.logger.jdbc.resultset=OFF
log4j.logger.jdbc.connection=OFF
jdbc.properties
==========================================
#mysql
#jdbc.dataSource=org.logicalcobwebs.proxool.ProxoolDataSource
#jdbc.driver=com.mysql.jdbc.Driver
#jdbc.url=jdbc:mysql://localhost:3306/jbpmtest?useUnicode=true&characterEncoding=utf8
#log4jdbc
jdbc.dataSource=org.springframework.jdbc.datasource.DriverManagerDataSource
jdbc.driver=net.sf.log4jdbc.DriverSpy
jdbc.url=jdbc:log4jdbc:mysql://localhost:3306/jbpmtest?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
sqlserver 2008
#jdbc.dataSource=com.alibaba.druid.pool.DruidDataSource
#jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
#jdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=jbpmtest
#jdbc.username=sa
#jdbc.password=sa
proxool.alias=ds
proxool.prototypeCount=2
proxool.minimumConnectionCount=5
proxool.maximumConnectionCount=20
proxool.houseKeepingTestSql=select 1
applicationContext.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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com"> <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" /> </context:component-scan> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="dataSource" class="${jdbc.dataSource}"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> </beans>
applicationContext-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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jbpm="http://drools.org/schema/drools-spring" xmlns:kie="http://drools.org/schema/kie-spring" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://drools.org/schema/drools-spring http://drools.org/schema/drools-spring-1.3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://drools.org/schema/kie-spring http://drools.org/schema/kie-spring-2.0.0.xsd"> <!-- 使用Spring + Hibernate对非JBPM代码的事务管理 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" p:packagesToScan="com.pandy.model"> <property name="dataSource"> <ref bean="dataSource"/> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">false</prop> <!--<prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop> <prop key="hibernate.cache.use_structured_entries">true</prop> <prop key="hibernate.generate_statistics">true</prop>--> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> </beans>
springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:websocket="http://www.springframework.org/schema/websocket" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/> </context:component-scan> <mvc:annotation-driven/> <mvc:resources mapping="/js/**" location="/js/"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> <websocket:message-broker application-destination-prefix="/app"> <websocket:stomp-endpoint path="/ws"> <websocket:sockjs/> </websocket:stomp-endpoint> <websocket:simple-broker prefix="/topic"/> </websocket:message-broker> </beans>
三个domain
==============================================
package com.pandy.domain; import java.io.Serializable; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class Stock implements Serializable { private static final long serialVersionUID = 1L; private String code = ""; private double price = 0.0; private Date time = new Date(); //.................. }
package com.pandy.domain; public class Greeting { private String content; public Greeting(String content) { this.content = content; } public String getContent() { return content; } }
package com.pandy.domain; public class HelloMessage { private String name; public String getName() { return name; } }
两个服务器处理信息的controller
==========================================
package com.pandy.controller; import com.pandy.domain.Stock; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Random; /** * 服务器定时发送信息到客户端的例子 */ @Controller public class HomeController { @Autowired private SimpMessagingTemplate template; private TaskScheduler scheduler = new ConcurrentTaskScheduler(); private List<Stock> stockPrices = new ArrayList<Stock>(); private Random rand = new Random(System.currentTimeMillis()); /** * 处理信息并发送给客户端 */ private void updatePriceAndBroadcast() { for (Stock stock : stockPrices) { double chgPct = rand.nextDouble() * 5.0; if (rand.nextInt(2) == 1) chgPct = -chgPct; stock.setPrice(stock.getPrice() + (chgPct / 100.0 * stock.getPrice())); stock.setTime(new Date()); } template.convertAndSend("/topic/price", stockPrices); } /** * controller初始化的时候,就初始化一个定时任务 */ @PostConstruct private void broadcastTimePeriodically() { scheduler.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println("自动任务执行====================================" + new Date()); updatePriceAndBroadcast(); } }, 1000); } /** * 增加信息 */ @MessageMapping("/addStock") public void addStock(Stock stock) throws Exception { stockPrices.add(stock); updatePriceAndBroadcast(); } /** * 移除所有 */ @MessageMapping("/removeAllStocks") public void removeAllStocks() { stockPrices.clear(); updatePriceAndBroadcast(); } @RequestMapping(value = "/", method = RequestMethod.GET) public String home() { return "home"; } }
package com.pandy.controller; import com.pandy.domain.Greeting; import com.pandy.domain.HelloMessage; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.stereotype.Controller; import java.util.Date; /** * 由客户端触发,并接受服务器发送信息的例子 */ @Controller public class MessageController { @MessageMapping("/greeting") @SendTo("/topic/greetings") public Greeting greeting(HelloMessage message) throws Exception { System.out.println("MessageController====================================>客户端连接"); return new Greeting("["+(new Date())+"] 服务器返回: Hello,客户端输入信息< " + message.getName() + ">"); } }
两个测试客户端
index.jsp
================================================
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page session="false" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Spring + WebSocket 例子</title> <script src="/SpringWebSocket/js/sockjs-0.3.min.js"></script> <script src="/SpringWebSocket/js/stomp.js"></script> <script src="/SpringWebSocket/js/jquery-1.10.2.js"></script> <script src="/SpringWebSocket/js/jquery-ui-1.10.4.custom.js"></script> <script src="/SpringWebSocket/js/jquery.json.js"></script> <style> table{ border: 1px solid #000000; } table th{ border: 1px solid #000000; background-color: #889966; } table td{ border: 1px solid #000000; background-color: #00ff00; } </style> <script> //创建sockJS协议 var socket = new SockJS("/SpringWebSocket/ws"); var stompClient = Stomp.over(socket); //连接服务器 stompClient.connect("guest", "guest", function () { //成功连接后,设定接受服务器的地址和处理方法 stompClient.subscribe('/topic/price', function (frame) { //服务器返回请求处理 var prices = JSON.parse(frame.body); $('#price').empty(); for (var i in prices) { var price = prices[i]; $('#price').append( $('<tr>').append( $('<td>').html(price.code), $('<td>').html(price.price.toFixed(2)), $('<td>').html(price.timeStr) ) ); } }); }, function (error) { //连接出现错误回调函数 alert(error.headers.message); }); // Register handler for add button $(document).ready(function () { $('.add').click(function (e) { e.preventDefault(); var code = $('.new .code').val(); var price = Number($('.new .price').val()); var jsonstr = JSON.stringify({ 'code': code, 'price': price }); //发送信息给服务器 stompClient.send("/app/addStock", {}, jsonstr); return false; }); $('.remove-all').click(function (e) { e.preventDefault(); //发送信息给服务器 stompClient.send("/app/removeAllStocks"); return false; }); }); </script> </head> <body> <h1>Spring + WebSocket 例子</h1> <p class="new"> 编码: <input type="text" class="code"/> 价格: <input type="text" class="price"/> <button class="add">增加</button> <button class="remove-all">移除所有</button> </p> <table style="width: 1000px;"> <thead> <tr> <th style="width: 30%">编码</th> <th style="width: 30%">价格</th> <th style="width: 40%">时间</th> </tr> </thead> <tbody id="price"> </tbody> </table> 测试方式: 提交信息到服务器,服务器会定时发送数据到页面,不管多少个浏览器打开都能接收服务器发送来的信息。 </body> </html>
hello.jsp
=================================================
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ page session="false" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Spring + WebSocket Hello world例子</title> <script src="/SpringWebSocket/js/sockjs-0.3.min.js"></script> <script src="/SpringWebSocket/js/stomp.js"></script> <script src="/SpringWebSocket/js/jquery-1.10.2.js"></script> <script src="/SpringWebSocket/js/jquery-ui-1.10.4.custom.js"></script> <script src="/SpringWebSocket/js/jquery.json.js"></script> <script> //创建sockJS协议 var socket = new SockJS("/SpringWebSocket/ws"); var stompClient = Stomp.over(socket); //连接服务器 stompClient.connect("guest", "guest", function () { $("#recFromServer").append("<br>" + "成功连接服务器.!"); //成功连接后,设定接受服务器的地址和处理方法 stompClient.subscribe('/topic/greetings', function (greeting) { var content = JSON.parse(greeting.body).content; $("#recFromServer").append("<br>" + content); }); }, function (error) { //连接出现错误回调函数 alert(error.headers.message); }); function sendMessage() { //发送信息给服务器 stompClient.send("/app/greeting", {}, JSON.stringify({ 'name': $("#message").val() })); } </script> </head> <body> 输入名称: <input id="message" type="text"> <input type="button" onclick="sendMessage()" value="发送到服务器"> <div id="recFromServer"></div> 测试方式: 用两个浏览器打开这个页面,然后一个页面提交信息,它能接收到服务器的数据,同时另一个页面也能接收到服务器发送的数据。 </body> </html>
附加:pom.xml
===================================
<?xml version="1.0" encoding="UTF-8"?> <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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SpringWebSocket</groupId> <artifactId>SpringWebSocket</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <cglib.version>2.2.2</cglib.version> <mysql.version>5.1.30</mysql.version> <slf4j.version>1.6.1</slf4j.version> <log4j.version>1.2.16</log4j.version> <spring.version>4.0.5.RELEASE</spring.version> <spring.asm.version>3.1.4.RELEASE</spring.asm.version> <aspectj.version>1.6.2</aspectj.version> <hibernate.version>4.3.5.Final</hibernate.version> <hibernateCommAnn.version>3.2.0.Final</hibernateCommAnn.version> <hibernateAnn.version>3.5.6-Final</hibernateAnn.version> <jdbc.groupId>com.alibaba</jdbc.groupId> <jdbc.artifactId>druid</jdbc.artifactId> <jdbc.version>1.0.4</jdbc.version> <commons.lang.version>2.6</commons.lang.version> <commons.lang3.version>3.1</commons.lang3.version> <tomcat.ws.version>7.0.53</tomcat.ws.version> </properties> <dependencies> <!-- Spring 3 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-asm</artifactId> <version>${spring.asm.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</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-web</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-webmvc</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-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!--<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <optional>true</optional> <scope>test</scope> </dependency>--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> <!-- <dependency> <groupId>org.apache.openjpa</groupId> <artifactId>openjpa-persistence</artifactId> <version>2.2.2</version> </dependency> --> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-commons-annotations</artifactId> <version>${hibernateCommAnn.version}</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-annotations</artifactId> <version>${hibernateAnn.version}</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>${cglib.version}</version> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> </dependency> <!-- JBOSS Cache & Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>4.2.1.Final</version> </dependency> <!--<dependency> <groupId>hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>1.8.0.4</version> </dependency>--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-testing</artifactId> <version>${hibernate.version}</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.4.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!--<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency>--> <!-- WebSocket for tomcat --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-coyote</artifactId> <version>${tomcat.ws.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-catalina</artifactId> <version>${tomcat.ws.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-annotations-api</artifactId> <version>${tomcat.ws.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-websocket</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-messaging</artifactId> <version>${spring.version}</version> </dependency> <!-- Log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> <exclusions> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.lazyluke</groupId> <artifactId>log4jdbc-remix</artifactId> <version>0.2.7</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>runtime</scope> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>15.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>sqljdbc</groupId> <artifactId>sqljdbc</artifactId> <version>4.0</version> </dependency> <dependency> <groupId>${jdbc.groupId}</groupId> <artifactId>${jdbc.artifactId}</artifactId> <version>${jdbc.version}</version> </dependency> <!-- JSON --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.3.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.3.2</version> </dependency> <!--<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-jaxrs</artifactId> <version>1.7.1</version> </dependency>--> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>${commons.lang.version}</version> </dependency> <!--<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons.lang3.version}</version> </dependency>--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${commons.lang3.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <url>http://localhost:8080/manager/text</url> <username>admin</username> <password>admin</password> <path>/SpringWebSocket</path> </configuration> </plugin> </plugins> </build> </project>
- sockjs_stomp.rar (14.3 KB)
- 下载次数: 49
- SpringWebSocket.rar (306.9 KB)
- 下载次数: 94
发表评论
-
Spring Boot 属性配置
2016-06-24 11:04 1180Spring Boot 属性配置和使用 http://blog ... -
Spring Boot 集成MyBatis
2016-06-24 10:55 2024Spring Boot 集成MyBatis http://bl ... -
Spring MVC防重复提交
2016-06-17 15:47 1643http://my.oschina.net/zyqjustin ... -
Spring容器加载完之后执行特定任务
2016-06-17 15:36 2281http://my.oschina.net/simpleton ... -
使用spring-session和shiro来代理session的配置
2016-06-16 11:21 12055使用spring-session和redis来代理sessio ... -
JSTL 的 if else : 有 c:if 没有 else 的处理
2016-06-14 09:52 1332http://blog.csdn.net/xiyuan1999 ... -
spring mvc 请求转发和重定向
2016-06-14 09:48 1396http://blog.csdn.net/jackpk/art ... -
mvc:view-controller
2016-05-18 10:26 1080http://blog.csdn.net/lzwglory/a ... -
spring配置事物的方式:注解和aop配置
2016-05-14 00:26 4101参考: Spring AOP中pointcut express ... -
分布式任务调度组件 Uncode-Schedule
2016-05-13 14:47 2286http://www.oschina.net/p/uncode ... -
写个mybatis的拦截插件,实现将所有执行的sql写入文件里
2016-05-12 15:59 5103原文 http://3131854.blog.51cto.co ... -
Mybatis分库分表扩展插件
2016-05-12 15:47 1620http://fangjialong.iteye.com/bl ... -
spring+mybatis+atomikos 实现JTA事务
2016-05-11 22:00 5521sping配置多个数据源 不同用户操作不同数据库 http:/ ... -
Spring中使用注解 @Scheduled执行定时任务
2016-05-10 09:39 1565原文:http://dwf07223.blog.51cto.c ... -
Spring中配置Websocket
2016-05-05 16:55 1275spring+websocket整合(springMVC+sp ... -
redis 集群中Session解决方案之Spring Session
2016-05-04 08:54 1313集群中Session解决方案之Spring Session h ... -
使用Spring-data进行Redis操作
2016-05-04 08:54 4790使用Spring-data进行Redis操作 http://z ... -
Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC
2016-05-03 13:35 1059Spring4新特性——集成Bean Validation 1 ... -
SpringMVC介绍之Validation
2016-05-03 13:10 982SpringMVC介绍之Validation http://h ... -
spring 注解方式下使用commons-validator 验证表单
2016-05-03 11:08 3075原文: http://www.programgo.com/ar ...
相关推荐
本项目框架搭建基于"spring5.x+springmvc5.x+mybatis3.5x+tomcat9+jdk8+maven",这是一个经典且广泛使用的Java开发配置。下面将详细阐述这些组件的功能、相互关系以及如何进行集成。 1. **Spring 5.x**: Spring是...
前端使用Vue 2.x + Websocket axiso与ElementUI 实现页面展示与在线聊天。 具备以下功能:用户登录注册,在线聊天功能。 基于SpringBoot整合SpringMvc Websocket实现注册登录于在线聊天功能,使用JWT做...
【标题】"springMVC4.1.4+shiro1.2.3+spring4.x+Mybaits3.2.8+Ajax+html5" 提及的是一个基于Java的Web开发技术栈,其中包含了多个核心组件。下面将详细阐述这些技术在Web开发中的作用及其相互间的整合。 Spring MVC...
Spring-MVC-React-WebSocket Spring MVC 4.x + React.js + WebSocket + Sock.js首先启动客户端cd客户端纱线安装纱线起头后端设置tomcat服务器并部署战争。
请注意,这里的`spring.version`应该设置为4以上的版本,因为WebSocket支持是在Spring 4.x中引入的。 接下来,我们需要创建一个握手处理器(`HandshakeInterceptor`)。这个拦截器在客户端与服务器建立WebSocket...
使用WebSocket作为前后端连接方式, SpringMVC+JSP+WebSocket, 即时通讯, 最近聊天记录, 好友列表. 需要配置D盘下创建好对应文件, 图片需要配置nginx才可以访问: server { listen 8888; server_name localhost; ...
Spring 5.x引入了对Java 8的全面支持,增强了对反应式编程的支持,以及改进了对WebSocket和HTTP/2协议的处理。 **Spring MVC 5.x** Spring MVC 是Spring框架的一部分,专门用于处理Web请求。它提供了模型-视图-控制...
这些JAR包是Spring 4.3.13版本的组件,该版本在2017年发布,是Spring 4.x系列的一个稳定版本。 1. **spring-context-4.3.13.RELEASE.jar**:这是Spring的核心模块,提供了环境感知能力,如Bean工厂、应用上下文、...
随着技术的发展,Spring 和 SpringMVC 不断推出新版本,5.x.x 版本引入了一些重要改进: 1. **Java 8 支持**:5.x.x 版本开始全面支持Java 8的新特性,如Lambda表达式。 2. **反应式编程**:Spring 5 引入了Reactor...
在Spring 4.x中,SpringMVC引入了更多优化,比如支持WebSocket、简化了RESTful服务的实现,以及改进了视图解析器的性能。通过模型绑定、数据验证和异常处理,SpringMVC能够构建出高效且易于维护的Web应用。 整合这...
本专题课程针对SpringBoot 2.x、Spring 5、SpringMVC和Mybatis这四个关键框架进行源码解析,旨在帮助开发者从底层原理出发,提升技术水平,更好地应用和优化这些工具。 首先,我们来探讨SpringBoot 2.x。SpringBoot...
aopalliance-1.0.jar commons-logging-1.1.1.jar spring-aop-4.2.4.RELEASE.jar spring-aspects-4.2.4.RELEASE.jar spring-beans-4.2.4.RELEASE.jar spring-context-4.2.4....spring-websocket-4.2.4.RELEASE.jar
Spring 4.0.x版本包含Spring Core、Spring Beans、Spring Context、Spring AOP等模块,支持Java EE 7,提供对WebSocket和JMS的支持。 3. **Spring MVC 4.0.x**:Spring MVC是Spring框架的一部分,用于构建Web应用...
【标题】"基于Spring 4.X搭建的SpringMVC多模块架构模板"是一个适用于初学者和专业开发者的项目模板,它充分利用了Spring框架的核心特性,为构建大型、复杂的企业级应用提供了一个良好的起点。该模板基于Spring ...
首先,我们来看看核心的SpringMVC jar,通常包括`spring-webmvc-x.x.x.RELEASE.jar`。这个文件包含了SpringMVC的主要实现,如DispatcherServlet、Controller接口、ModelAndView类等,它们负责处理HTTP请求,将业务...
SSM 4.0版本通常依赖于Spring Framework 4.x,而WebSocket功能在Spring 4.0及以上版本中已经得到支持。因此,你需要在`pom.xml`文件中添加Spring WebSocket的依赖,例如: ```xml <groupId>org.springframework....
在 "springmvc4_springmvc_mainlyyb8_" 这个项目中,我们可以预见到它将涉及到 Spring MVC 4.x 版本的相关知识。 1. **Spring MVC 概述** Spring MVC 提供了一个灵活的架构,可以处理 HTTP 请求、路由请求到相应的...
6. **Servlet容器相关**: 包括Servlet API(如`servlet-api.jar`)、JSP和JSTL相关jar包,以及WebSocket等现代Web技术的API。 7. **其他依赖**: 如Jackson或Gson用于JSON序列化,Log4j或SLF4J进行日志记录,Apache ...