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
点击按钮,会到访问后台的 sayHello 函数,弹出返回字符串的对话框
spring与dwr整合实现js直接使用java代码的更多相关文章
- 在js中嵌套java代码
jsp中有时候在js中操作某些java后台传递过来的数据逻辑比较复杂,比如list内容的遍历,可以直接在页面上添加java脚本来执行内容,代码如下: //在js中插入java代码操作 <% // ...
- 关于JS里面写JAVA代码的问题
最近做项目需要在JS脚本里面调用一个JAVA的函数得到数据,在网上查了很久,发现JS脚本里面不能写JAVA函数.只能把JS脚本里面的代码写进JSP文件里面的<script>标签内,然后写J ...
- 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)
轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)(国家级奖项获奖作品升级版,四版累计印刷27次发行量超10万册的轻量级Jav ...
- js调用java代码返回解决方案
版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 今天封装一个加密标签,遇到一个问题,我需要对页面上的数据调用java后台代码进行解密,而标签里只能通过js获取到数据,所以就遇 ...
- 九 spring和mybatis整合
1 spring和mybatis整合 1.1 整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用Sq ...
- 框架篇:Spring+SpringMVC+hibernate整合开发
前言: 最近闲的蛋疼,搭个框架写成博客记录下来,拉通一下之前所学知识,顺带装一下逼. 话不多说,我们直接步入正题. 准备工作: 1/ IntelliJIDEA的安装配置:jdk/tomcat等..(本 ...
- 框架篇:Spring+SpringMVC+Mybatis整合开发
前言: 前面我已搭建过ssh框架(http://www.cnblogs.com/xrog/p/6359706.html),然而mybatis表示不服啊. Mybatis:"我抗议!" ...
- Spring+MyBatis+SpringMvc整合Demo
客户关系管理系统demo 项目分析 该demo使用技术及环境:ssm+maven+bootstrap+jsp+mysql+idea+jdk1.8 需求:客户管理,实现客户列表分页显示如下图 项目开始 ...
- spring+springmvc+hibernate 整合
三大框架反反复复搭了很多次,虽然每次都能搭起来,但是效率不高.最近重新搭了一次,理顺了思路,整理了需要注意的地方,分享出来. 工具:Eclipse(jdk 1.7) spring和hibernate版 ...
随机推荐
- HTML Strip Char Filter
The html_strip character filter strips HTML elements from the text and replaces HTML entities with t ...
- Elasticsearch 5 Ik+pinyin分词配置详解
版权声明:本文为博主原创文章,地址:http://blog.csdn.net/napoay,转载请留言. 一.拼音分词的应用 拼音分词在日常生活中其实很常见,也许你每天都在用.打开淘宝看一看吧,输入拼 ...
- Elasticsearch入门CRUD(新增、查询、修改、删除)
1. 项目中引用 Elasticsearch.NET Elasticsearch 其他版本可在: http://www.nuget.org/ 找到对应的项目以源码 ! 本文以 2.4 ...
- centos和ubuntu配置路由的三种方式
本篇总结三种修改路由的方式:route, ip route, 以及通过修改文件来配置路由,前2种命令行形式适用于ubuntu和centos,重启失效,最后一种永久有效. 一. route命令 ...
- UVALive-7457-Discrete Logarithm Problem(取模运算)
原题链接 额,一直在理解题意在纠结看不懂,后来才恍然大悟 题意:定义一种新运算 a × b = a * b mod p : 已知条件给定一个p 求 x 这里用到同余与模运算乘法公式:a * b % n ...
- leetcode-6-Z字形变换
题目描述: 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数: P A H N A P L S I I G Y I R 之后从左往右,逐行读取字符:" ...
- VS2015编译器按F6不能够重新生成
工具-->选项-->环境-->键盘-->应用以下其他键盘映射方案,下拉选择 Visual C# 2005
- (C/C++) string / *char / int 基本轉換
網路上有許 string / *char / integer 基本轉換方式 string 與 *char 互相轉換的方法 /* string to *char */ string ssbuf1 = & ...
- ReactNative 打包 APK
ReactNative打包步骤: 1.在项目的根目录执行下面这行命令: keytool -genkey -v -keystore my-release-key.keystore -alias my-k ...
- vue.js - axios Get、Post方法传参给 .net core webapi。
一:axios中是 Get请求: 1:在vue项目中通过params属性携带数据: let _self = this; axios({ method:'get', url:'http://localh ...