博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot 整合 DWR ,实现 js 直接调用后端 Service
阅读量:7183 次
发布时间:2019-06-29

本文共 3664 字,大约阅读时间需要 12 分钟。

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

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

DWR 官方主页:

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@RemoteProxypublic 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=falsespring.freemarker.charset=UTF-8spring.freemarker.check-template-location=truespring.freemarker.content-type=text/htmlspring.freemarker.expose-request-attributes=truespring.freemarker.expose-session-attributes=truespring.freemarker.request-context-attribute=requestspring.freemarker.suffix=.ftl

spring.xml

index.ftl

            hello

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

转载地址:http://apykm.baihongyu.com/

你可能感兴趣的文章
二叉树(层次遍历)非递归
查看>>
Go 生成图片
查看>>
命令-df/du
查看>>
整合 Tachyon 运行 Apache Flink(译)
查看>>
写给走在IT路上的我。。
查看>>
新手初识webservice-理论篇
查看>>
成功者的共同特点
查看>>
iOS中的多线程
查看>>
Oracle客户端配置说明 centos
查看>>
鸟哥学习笔记---NIS
查看>>
spring framework最新发布压缩包的下载问题
查看>>
hadoop-1.2.1集群安装
查看>>
c++类中的const
查看>>
markdown使用*
查看>>
13年下半年信息系统项目管理师考试总结
查看>>
为什么受伤的总是技术大牛
查看>>
我的友情链接
查看>>
Composer 笔记(PHPconf 台湾)
查看>>
linux双网卡绑定
查看>>
Python 三元运算符与lambda
查看>>