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

Posted 大饼酥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>
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代码的主要内容,如果未能解决你的问题,请参考以下文章

dwr与ssh框架整合教程

使用Spring的DWR3.0 RC2启动项目时发生错误。

java.lang.InstantiationException: DWR can't find a spring config. See the logs for solutions

java如何将html静态模板写入数据库+和如何从数据库中读出

Spring整合Ehcache管理缓存

Spring整合Ehcache管理缓存