Spring容器中的Bean可以被DWR使用:既然使用到了Spring,所以就要多加一个jar包spring.jar到lib下去。
?
使用到了Spring容器来产生bean,就得修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!--
默认找的位置是:/WEB-INF/applicationContext.xml
-->
<!--
<param-value>/WEN-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
-->
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 配置DWR的核心Servlet -->
<servlet>
<!-- 指定DWR核心Servlet的名字 -->
<servlet-name>dwr-invoker</servlet-name>
<!-- 指定DWR核心Servlet的实现类 -->
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<!-- 指定DWR核心Servlet处于调试状态 -->
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<!-- 指定核心Servlet的URL映射 -->
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<!-- 指定核心Servlet映射的URL -->
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
?beans.xml文件的配置
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 定义了一个bean -->
<bean id="test" class="com.lbx.dwr.spring.TestSpring" />
</beans>
?Java处理类
package com.lbx.dwr.spring;
public class TestSpring {
//服务器处理方法
public String test(String name)
{
return name + "您好,您已经会调用Spring中的Bean了...";
}
}
?dwr.xml文件的配置
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="spring" javascript="Demo">
<!-- 指定使用Spring容器中的hello Bean -->
<param name="beanName" value="test"/>
</create>
</allow>
</dwr>
?客户端JSP代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>DWR整合Spring</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type='text/javascript' src='dwr/interface/Demo.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script type="text/javascript">
function sendMessage(){
//调用远程的hello方法,使用了dwr.util的getValue方法获取HTML元素的值
Demo.test(dwr.util.getValue('name') , cb);
}
function cb(data){
//使用dwr.util的setValue方法设置HTML元素的值
dwr.util.setValue('show' ,data);
}
</script>
</head>
<body>
<h3>
DWR整合Spring
</h3>
请输入您的名字
<input id="name" name="name" type="text" />
<br />
<input type="button" value="调用Spring Bean" onclick="sendMessage();" />
下面是服务器的回应:
<hr>
<div id="show"></div>
</body>
</html>
?
?
?