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" onclick='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>

 

你可能感兴趣的:(JavaScript,spring,bean,servlet,DWR)