spring與dwr整合實現js直接使用java代碼

來源:互聯網
上載者:User

標籤: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代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.