springmvc+webSocket 整合

maven 建立簡單的springmvc項目連接html

連接:http://www.cnblogs.com/mybest/p/4265872.htmljava

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

Maven建完springMVC後 須要在pom.xml中添加tomgcat7的插件不然報錯spring

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

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

Jdk1.7spring-mvc

須要在pom.xml增長tomcat

Tomcat7的插件websocket

<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以上才支持mvc

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

相關文章
相關標籤/搜索