spring与dwr整合实现js直接使用java代码

此文章是基于  搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台

一. jar包介绍

  1. dwr-3.0.1.jar,支持 spring 4.3.4 的最低版本

二. 相关文件介绍

  1. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd
http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.4"> <servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>allowScriptTagRemoting</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping> </web-app>

 

  2. applicationContextSys.xml

<?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:context="http://www.springframework.org/schema/context" 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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"> <dwr:annotation-config id="dwrid"/>
<dwr:annotation-scan scanRemoteProxy="true" base-package="com.ims.service.sys.impl" />
</beans>

  

  3. TestBSImpl.java

package com.ims.service.sys.impl;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy; import com.ims.service.sys.TestBS; @RemoteProxy(name="testBS")
public class TestBSImpl implements TestBS{ @RemoteMethod
public String sayHello(){
return "hello!";
}
}

  

  4. dwr.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>dwr访问测试</title>
<%@ include file="/common/basePath.jsp"%>
</head>
<body>
~~~~~~~~~~~~~~~~~~~~~~dwr访问测试~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<br><br>
<button type="button" onclick="sayHello();">你好</button> <script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/interface/testBS.js"></script> <script type="text/javascript">
function sayHello(){
dwr.engine.beginBatch();
testBS.sayHello(function(value){
alert(value);
});
dwr.engine.endBatch();
}
</script> </body>
</html>

三. 测试

  访问:http://localhost:8080/ims/test/dwr.do

spring与dwr整合实现js直接使用java代码

  点击按钮,会到访问后台的 sayHello 函数,弹出返回字符串的对话框

  

上一篇:Android网络编程1


下一篇:Mol Cell Proteomics. |王欣然| 基于微粒的蛋白聚合物捕获技术让能满足多种不同需求的蛋白质组学样品制备方法成为可能