SpringBoot 整合 DWR ,实现 js 直接调用后端 Service

SpringBoot 整合 dwr ,实现 js 直接调用后端 Service

听小伙伴提到一个技术,就自己试了试,下面是一个入门小 demo, 想深入了解的话可以自行研究

DWR 官方主页:http://directwebremoting.org/...
DWR 是一个 Java 库, 它使服务器上的 Java 和浏览器中的 JavaScript 能够尽可能简单地相互交互和调用。

pom.xml



    4.0.0

    com.junbaor
    dwr
    0.0.1-SNAPSHOT
    jar

    dwr
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.9.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-freemarker
        
        
            org.directwebremoting
            dwr
            3.0.2-RELEASE
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
    

DwrApplication.java

package com.junbaor.dwr;

import org.directwebremoting.spring.DwrSpringServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@SpringBootApplication
@ImportResource(locations = "classpath:spring.xml")
public class DwrApplication {

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

    @RequestMapping
    public String index() {
        return "index";
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        DwrSpringServlet servlet = new DwrSpringServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(servlet, "/dwr/*");
        registrationBean.addInitParameter("debug", "true");
        return registrationBean;
    }
}

DemoService.java

package com.junbaor.dwr;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.stereotype.Service;

@Service
@RemoteProxy
public class DemoService {

    // 这里也可以使用 @Autowired 注入依赖的其他服务

    @RemoteMethod
    public String hello() {
        return "hello";
    }

    @RemoteMethod
    public String echo(String string) {
        return string;
    }

}

application.properties

spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl

spring.xml




    

    

    

index.ftl



    
    
    

hello

刷新网页, 就会执行后端的 echo 方法, 前端接受响应后会执行回调函数

你可能感兴趣的:(springboot)