spring2 整合 Dwr(把DWR的配置写到Spring的配置文件)

Spring 2基于XML Schema的配置,Spring 2通过XML 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.0.xsd" >  

 

 

打开DWR的jar包中META-INF/spring.schemas文件,内容如下:

 

http\://www.directwebremoting.org/schema/spring-dwr-2.0.xsd=org/directwebremoting/spring/spring-dwr-2.0.xsd

 

 

我们在配置Spring 2时,应在名称空间中加入以下配置:

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
 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.0.xsd 
  http://www.directwebremoting.org/schema/spring-dwr
  http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">

 

以下是我整个spring的配置及一些java类

 

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
	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.0.xsd 
	 http://www.directwebremoting.org/schema/spring-dwr
	 http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">

	<!-- spring bean配置 -->
	<bean id="dwrHelper" class="com.DwrHelper">
		<!-- 定义调用的js文件 -->
		<dwr:remote javascript="dwrHelper"></dwr:remote>
		<property name="service">
			<ref bean="service"/>
		</property>
	</bean>

	<bean id="service" class="com.TestServiceImp"/>
	<!-- end spring bean配置 -->
</beans>

 

DwrHelper.java

package com;

public class DwrHelper {

	private TestServiceInf service;
	
	public String checkUsername(String name){
		return service.checkedUser(name)?"成功":"请正确输入用户名";
	}
	
	public TestServiceInf getService() {
		return service;
	}

	public void setService(TestServiceInf service) {
		this.service = service;
	}
}

TestServiceInf.java

package com;

public interface TestServiceInf {
	public boolean checkedUser(String userName);
}

 

TestServiceImp.java

package com;

public class TestServiceImp implements TestServiceInf{
	public boolean checkedUser(String userName){
		return userName.equals("callan");
	}
	
}

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="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">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	
	<servlet>
		<servlet-name>dwr-invoker</servlet-name>
		<servlet-class>
			org.directwebremoting.spring.DwrSpringServlet
		</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
	</servlet>

	<servlet-mapping>
		<servlet-name>dwr-invoker</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<html>
  <head>
 <script type='text/javascript' src='dwr/engine.js'></script>
 <script type='text/javascript' src='dwr/interface/dwrHelper.js'></script>
  </head>
 
  <body>
    <input type="text" name="username"/>
    <input type="button" οnclick='aa()'; value="Click">
  </body>
</html>
<script type="text/javascript">
<!--
 function aa(name){
  var v = document.getElementById("username").value;
  dwrHelper.checkUsername(v,function(data){
   alert(data);
  });
 }
//-->
</script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值