加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

dwr+spring集成

发布时间:2020-12-15 22:09:16 所属栏目:百科 来源:网络整理
导读:dwr+spring 集成 DWR ( DirectWebRemoting )是一个用于改善 web 页面与 Java 类交互的远程服务器端 Ajax 开源框架 , 可以帮助开发人员开发包含 AJAX 技术的网站 . 它可以允许在 浏览器 里的代码使用运行在 WEB 服务器 上的 JAVA 函数 , 就像它就在浏览器里

dwr+spring集成

DWRDirectWebRemoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX技术的网站.它可以允许在浏览器里的代码使用运行在WEB服务器上的JAVA函数,就像它就在浏览器里一样。

以下模拟一个简单的dwr入门案例,重点理解dwr是如何跟java后台服务器打交道的

模拟效果如下

该功能说明了dwr是怎么跟后台服务器打交道的

模拟从服务器加载下拉列表数据

模拟保存功能

模拟查询功能

接下来为dwr+spring集成步骤:

1、新建一个web工程,导入dwr+spring所需jar,如下图

目录结构图

修改web.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="2.4"

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<!--Spring上下文-->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:resource/app*.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!--配置DWR前端控制器-->

<servlet>

<servlet-name>dwrServlet</servlet-name>

<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>

<!--指定配置文件-->

<init-param>

<param-name>config</param-name>

<!--如果有多个用","分开-->

<param-value>

/WEB-INF/classes/config/dwr.xml

</param-value>

</init-param>

<init-param>

<param-name>debug</param-name>

<param-value>true</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>dwrServlet</servlet-name>

<url-pattern>/dwr/*</url-pattern>

</servlet-mapping>

</web-app>

新建实体类Dept

packageentity;

publicclassDept{

privateLongid;

privateStringname;

publicDept(){

}

publicDept(Longid,Stringname){

this.id=id;

this.name=name;

}

publicLonggetId(){

returnid;

}

publicvoidsetId(Longid){

this.id=id;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

}

新建业务逻辑类

DeptServices

packageservices;

importjava.util.ArrayList;

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

importentity.Dept;

@SuppressWarnings("unchecked")

publicclassDeptServices{

publicListfindDept(){

thrownewRuntimeException("查找失败!");

}

publicvoiddeleteDept(Longid){

System.out.println("Deletedept"+id);

}

publicListgetDeptsForPo(){

Listdepts=newArrayList();

depts.add(newDept(1l,"教质部"));

depts.add(newDept(2l,"学术部"));

depts.add(newDept(3l,"就业部"));

depts.add(newDept(4l,"咨询部"));

returndepts;

}

publicvoidsaveDept(List<Dept>depts){

//System.out.println(dept.getId()+":"+dept.getName());

System.out.println(depts);

}

publicListgetDepts(){

Listdepts=newArrayList();

Mapmap=newHashMap();

map.put("id","01");

map.put("name","教质部");

depts.add(map);

map=newHashMap();

map.put("id","02");

map.put("name","学术部");

depts.add(map);

map=newHashMap();

map.put("id","03");

map.put("name","就业部");

depts.add(map);

map=newHashMap();

map.put("id","04");

map.put("name","咨询部");

depts.add(map);

try{

Thread.sleep(1000);

}catch(InterruptedExceptione){

e.printStackTrace();

}

returndepts;

}

}

HelloServices

packageservices;

/**

*@authordlwu

*

*/

publicclassHelloServices{

publicStringsayHello(Stringname){

System.out.println("Hellonow!");

return"Hello"+name+"!";

}

}

LoginService

packageservices;

importorg.directwebremoting.WebContext;

importorg.directwebremoting.WebContextFactory;

publicclassLoginService{

publicvoidcheckUserLogin(Stringuserid,Stringpwd){

WebContextctx=WebContextFactory.get();

ctx.getSession().setAttribute("userid",userid);

}

publicStringgetLoginUser(){

WebContextctx=WebContextFactory.get();

return(String)ctx.getSession().getAttribute("userid");

}

}

配置applicationContext.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<!--

配置系统基础环境

-->

<beans

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd"

>

<beanid="deptServices"class="services.DeptServices"></bean>

<beanid="loginSrv"class="services.LoginService"></bean>

</beans>

配置dwr.xml,dwr.xml是前台jsjava服务器沟通的桥梁

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEdwrPUBLIC"-//GetAheadLimited//DTDDirectWebRemoting2.0//EN""http://getahead.org/dwr/dwr20.dtd">

<!--通用dwr配置-->

<dwr>

<allow>

<!--建立JS对象,将目标对象的方法转换成JS对象的方法-->

<createjavascript="helloSrv"creator="new">

<paramname="class"value="services.HelloServices"></param>

</create>

<!--Spring中获取Java对象-->

<createjavascript="deptSrv"creator="spring">

<paramname="beanName"value="deptServices"></param>

<!--禁止执行-->

<excludemethod="deleteDept"/>

</create>

<createjavascript="loginSrv"creator="spring">

<paramname="beanName"value="loginSrv"></param>

</create>

<!--指定针对于特定对象的转换器-->

<convertmatch="entity.*"converter="bean"></convert>

<convertmatch="java.lang.Throwable"converter="bean">

<paramname="include"value="message"></param>

</convert>

</allow>

</dwr>

页面

<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>

<%

Stringpath=request.getContextPath();

StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

<title>MyJSP'hello.jsp'startingpage</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

<metahttp-equiv="description"content="Thisismypage">

</head>

<!--记得引入js,测试地址:http://localhost:8083/dwrweb/dwr/-->

<scripttype="text/javascript"src="dwr/engine.js"></script>

<scripttype="text/javascript"src="dwr/interface/helloSrv.js"></script>

<scripttype="text/javascript"src="dwr/util.js"></script>

<scripttype="text/javascript">

functionhello(){

//方法一

//返回处理后的结果信息

/*varfn=function(result){

$("msg").innerHTML=result;

}

helloSrv.sayHello($("name").value,fn);*/

//方法二

helloSrv.sayHello($("name").value,function(result){

$("msg").innerHTML=result;

});

//方法三

//使用如下的好处为:不用导入如上三个js

//第一个参数:dwr访问路径,在web.xml中配置,如:<url-pattern>/dwr/*</url-pattern>

//第二个参数:dwrjava服务器通信变量,在dwr.xml中声明

//第三个参数:服务器方法名

//第四个参数:页面请求参数,即服务器方法名得参数

//第五个参数:回调函数

//dwr.engine._execute("dwr",'helloSrv','sayHello',$("name").value,fn);

}

</script>

<body>

<divid="msg"></div>

<inputtype="text"id="name"/>

<inputtype="button"value="Hello"onclick="hello();"/>

</body>

</html>

dept.jsp页面

<%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>

<%

Stringpath=request.getContextPath();

StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

<title>MyJSP'hello.jsp'startingpage</title>

<metahttp-equiv="pragma"content="no-cache">

<metahttp-equiv="cache-control"content="no-cache">

<metahttp-equiv="expires"content="0">

<metahttp-equiv="keywords"content="keyword1,keyword3">

<metahttp-equiv="description"content="Thisismypage">

</head>

<!--记得引入js,测试地址:http://localhost:8083/dwrweb/dwr/-->

<scripttype="text/javascript"src="dwr/engine.js"></script>

<scripttype="text/javascript"src="dwr/interface/helloSrv.js"></script>

<scripttype="text/javascript"src="dwr/util.js"></script>

<scripttype="text/javascript">

functionloadDept(){

//说明已经加载,不必重新加载

if($('depts').options.length>0){

return;

}

DWRUtil.addOptions('depts',[{id:'',name:'正在下载...'}],'id','name');

dwr.engine._execute("dwr",'deptSrv','getDepts',function(depts){

DWRUtil.removeAllOptions('depts');

DWRUtil.addOptions('depts',depts,'name');

});

}

functionloadDept2(){

if($('depts2').options.length>0){

return;

}

DWRUtil.addOptions('depts2','getDeptsForPo',function(depts){

DWRUtil.removeAllOptions('depts2');

DWRUtil.addOptions('depts2','name');

});

}

functionsaveDept(){

//声明dept对象

vardept={

id:$("deptid").value,

name:$("deptname").value

};

dwr.engine._execute("dwr",'saveDept',[dept],function(){

alert('保存成功!');

});

}

functionfind(){

dwr.engine._execute("dwr",'findDept',{

callback:function(results){

alert('查询成功!');

},

errorHandler:function(e){

alert("查询失败:"+e);

}

});

}

</script>

<body>

<selectid="depts"onclick="loadDept();"></select>

<selectid="depts2"onclick="loadDept2();"></select>

<hr/>

ID:<inputid="deptid"type="text"size="8"/>

Name:<inputid="deptname"type="text"size="8"/>

<inputvalue="保存部门"type="button"onclick="saveDept();"/>

<inputvalue="查找"type="button"onclick="find();"/>

</body>

</html>

访问路径:

测试路径:http://localhost:8083/dwrweb/dwr/

http://localhost:8083/dwrweb/hello.jsp

http://localhost:8083/dwrweb/dept.jsp

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读