码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-04-11 17:22:48      阅读:569      评论:0      收藏:0      [点我收藏+]

标签:rtu   version   begin   ack   sys   load   logs   xsd   framework   

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

 

一. 准备工作

  1. 点击此下载相关文件,并把文件放到 ims 工程对应的文件夹下

 

二. 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>
View Code

 

  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>
View Code

  

  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!";
    }
}
View Code

  

  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>
View Code

 

四. 测试

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

技术分享

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

  

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

标签:rtu   version   begin   ack   sys   load   logs   xsd   framework   

原文地址:http://www.cnblogs.com/Mr-kevin/p/6693764.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!