1、引入pom
org.directwebremoting
dwr
3.0.2-RELEASE
2、dwr.xml东西移动到java
后台服务(@RemoteProxy @RemoteMethod)
bash
public interface DemoServiceI {
public String hello();
public String echo(String string) ;
}
@Service("DemoService")
@RemoteProxy(name = "DemoService")
public class DemoServiceImpl implements DemoServiceI{
// 这里也可以使用 @Autowired 注入依赖的其他服务
@RemoteMethod
public String hello() {
return "hello";
}
@RemoteMethod
public String echo(String string) {
return string;
}
}
3、配置DwrSpringServlet
```bash
@Configuration
public class DwrConfig {
@Bean
public ServletRegistrationBean servletRegistrationBean() {
DwrSpringServlet servlet = new DwrSpringServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(servlet, "/dwr/*");
registrationBean.addInitParameter("debug", "true");
return registrationBean;
}
}
4、xml扫描
```bash
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
<dwr:annotation-config/>
<dwr:annotation-scan scanRemoteProxy="false" base-package="com.aa.dwr"/>
<dwr:configuration/>
</beans>
<!-- 如果有类型转换的 -->
<dwr:configuration>
<dwr:convert type="bean" class="com.UnionOrderType">
</dwr:convert>
</dwr:configuration>
4、js中引入
bash
<html>
<head>
<title></title>
<script type='text/javascript' src='/dwr/engine.js'></script>
<script type='text/javascript' src='/dwr/interface/DemoService.js'></script>
</head>
hello
<script>
DemoService.echo('测试', function (str) {
alert(str);
});
</script>
</html>