dwr与springboot使用reverse ajax java推送消息给浏览器技术

这是tomcat,servlet实现,可以对比修改,与理解
dwr(Direct Web Remoting) reverse ajax java推送信息给浏览器技术

dwr停止更新,所以改为结合springboot主要转换配置与类,web.xml dwr.xml配置的转换

全部项目的目录结构
这里写图片描述

springboot项目 maven

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>com.ddddd</groupId>
    <artifactId>dwrspringboot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/>
    </parent>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.directwebremoting</groupId>
            <artifactId>dwr</artifactId>
            <version>3.0.1-RELEASE</version>
        </dependency>
    </dependencies>
</project>

application.properties

server.port=8088
spring.application.name=dwr_springboot

dwr.html springboot默认可以访问resource下static目录的静态文件

<html>
<head>
    <title>dwr html</title>
    <script type='text/javascript' src='/myhtml/dwr/engine.js'></script>
    <script type='text/javascript' src='/myhtml/dwr/interface/dwrPush.js'></script>
    <script type='text/javascript' src='/myhtml/dwr/util.js'></script>

    <script>
        window.onload = function () {
            dwr.engine.setActiveReverseAjax(true);

        }
        function onDwrData(data) {
            console.log(data);
        }
    </script>
</head>
<body>

dwr html
</body>
</html>

App.java 启动类

package com.ddddd.dwr;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * <p>Description: 类说明</p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: 7qb</p>
 *
 * @author hui
 * @version 1.0 2018/7/26 10:28
 */
@SpringBootApplication
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

TestController.java 控制模拟,访问来触发推送给浏览器消息,浏览器console可以看到信息

package com.ddddd.dwr.controller;

import com.ddddd.dwr.config.DwrPush;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>Description: 类说明</p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: 7qb</p>
 *
 * @author hui
 * @version 1.0 2018/7/26 11:02
 */
@RestController
public class TestController {

    @RequestMapping("/test")
    public String dd(){
        DwrPush.sendMsg("/myhtml/dwr.html","hello");
        return "dd";
    }
}

DwrConfig.java 配置类,添加servlet与listener

package com.ddddd.dwr.config;

import org.directwebremoting.servlet.DwrListener;
import org.directwebremoting.servlet.DwrServlet;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>Description: 类说明</p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: 7qb</p>
 *
 * @author hui
 * @version 1.0 2018/7/26 10:29
 */
@Configuration
public class DwrConfig {

    @Bean
    public ServletRegistrationBean dwr(){
        ServletRegistrationBean servlet = new ServletRegistrationBean(new DwrServlet(),"/myhtml/dwr/*");
        Map<String,String> initParam = new HashMap<String, String>();
        initParam.put("crossDomainSessionSecurity","false");
        initParam.put("allowScriptTagRemoting","true");
        initParam.put("classes","java.lang.Object");
        initParam.put("activeReverseAjaxEnabled","true");
        initParam.put("initApplicationScopeCreatorsAtStartup","true");
        initParam.put("maxWaitAfterWrite","60000");
        initParam.put("debug","true");
        initParam.put("logLevel","WARN");
        initParam.put("logLevel","WARN");
        //自定义配置,org.directwebremoting.impl.StartupUtil#configureFromInitParams name.equals("customConfigurator")
        //DwrServlet#init 初始化this.container
        initParam.put("customConfigurator","com.ddddd.dwr.config.Mydwr");
        servlet.setInitParameters(initParam);
        return servlet;
    }

    @Bean
    public ServletListenerRegistrationBean dwrListener(){
        ServletListenerRegistrationBean listener = new ServletListenerRegistrationBean(new DwrListener());
        return listener;
    }
}

DwrPush.java 推送工具类,实现任意html地址推送(相关页面需要加入js与js回调方法)

package com.ddddd.dwr.config;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptSessions;

public class DwrPush {
    public static void sendMsg(final String html,final String msg) {
      Browser.withPage(html, new Runnable() {
          public void run() {
              ScriptSessions.addFunctionCall("onDwrData", msg);
          }
      });

  }
}

Mydwr.java 替代dwr.xml的配置类

package com.ddddd.dwr.config;

import org.directwebremoting.Container;
import org.directwebremoting.create.NewCreator;
import org.directwebremoting.extend.Configurator;
import org.directwebremoting.extend.CreatorManager;

/**
 * <p>Description: 类说明</p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company: 7qb</p>
 *
 * @author hui
 * @version 1.0 2018/7/26 11:54
 */
public class Mydwr implements Configurator {
    @Override
    public void configure(Container container) {
        CreatorManager creatorManager= container.getBean(CreatorManager.class);
        NewCreator creator = new NewCreator();
        creator.setClass("com.ddddd.dwr.config.DwrPush");
        creator.setJavascript("dwrPush");
        creatorManager.addCreator(creator);
    }
}

浏览器访问
127.0.0.1:8088/myhtml/dwr.html 访问静态页面,打开浏览器调试工具
127.0.0.1:8088/test 访问,开始推送一条消息,上面的可以在浏览器控制台看到
127.0.0.1:8088/myhtml/dwr 看dwr是否加载有modules显示

注:会有一个报错,说一个线程调用什么已关闭的资源,可能是与springboot结合时的线程问题,暂不影响正常推送,推送会产生新的线程

完整项目地址 gitee

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页