springmvc+webSocket 整合

maven 創建簡單的springmvc項目鏈接

鏈接:http://www.cnblogs.com/mybest/p/4265872.html

http://www.cnblogs.com/dorothychai/p/4669808.html

Maven建完springMVC後 需要在pom.xml中添加tomgcat7的插件否則報錯

報:·  No plugin found for prefix 'tomcat' in the current project and in the plug……

Springmvc+webSocket 需在tomcat7.0以上才支持 6 不支持

Jdk1.7

需要在pom.xml增加

Tomcat7的插件

<build>
<plugins>
<plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <port>9090</port>
          <path>/</path>
          <uriEncoding>UTF-8</uriEncoding>
          <finalName>ROOT</finalName>
          <server>tomcat7</server>
        </configuration>
      </plugin>
</plugins>
</build>
 <dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-websocket</artifactId>  
    <version>4.0.1.RELEASE</version>  
 </dependency>  
 <dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-messaging</artifactId>  
    <version>4.0.1.RELEASE</version>  
</dependency>


版本要是4.0以上才支持

Web.xml中

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javeee/web-app_3_0.xsd"

要是3.0以上版本纔可支持

報錯時將 web-app中http://java........中Java改爲大寫 JAVA


第一種:配置方式

Spring-mvc.xml中要加入

xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd"

以便支持配置文件

<bean id="websocket" class="com.ultra.common.socketHandler.WebsocketEndPoint"/>
    <websocket:handlers>
      <websocket:mapping path="/websocket" handler="websocket"/>
      <websocket:handshake-interceptors>
         <bean class="com.ultra.common.socketHandler.HandshakeInterceptor"/>
      </websocket:handshake-interceptors>
   </websocket:handlers>

Bean的ID 要和path對應 否則報bean找不到的錯誤

第二種:註解方式

添加註解類

import com.ultra.common.socketHandler.HandshakeInterceptor;
import com.ultra.common.socketHandler.WebsocketEndPoint;
 
@Configuration
@EnableWebSocket
public class WebsocketConfig implements WebSocketConfigurer{
 
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(myhandler(), "/websocket")
        .addInterceptors(myInterceptors());
    }
 
    @Bean
    public WebSocketHandler myhandler() {
        return new WebsocketEndPoint();
    }
 
    @Bean
    public HandshakeInterceptor myInterceptors() {
        return new HandshakeInterceptor();
    }
}

這只是配置中我發現的要注意的東西

剩下的配置方式可從網上找到

URL:http://blog.csdn.net/gisredevelopment/article/details/38392629

http://www.cnblogs.com/lilinxuan/p/3759744.html

http://www.cnblogs.com/qq931399960/p/4730493.html

http://segmentfault.com/a/1190000000610456


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章