论坛首页 Web前端技术论坛

配置整合DWR3.0和Spring2.5使用annotation注解

浏览 14391 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-06-16  
DWR

这里使用 DWR3.rc1, Spring2.5 and Spring MVC

 

在Spring2.5中,使用了许多annotation, 新版本的DWR也支持annotation了, 下面看一下配置过程

 

1. 先写一个Controller

 

package com.myapp.web.controller;

import javax.servlet.http.HttpServletRequest;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RemoteProxy
public class UserController {

	@RemoteMethod
	public String getUserName(int id) {
		System.out.println("user id is " + id);
		return "UserName: " + id;
	}
	
	@RequestMapping("/user/add.do")
	public String addUser(HttpServletRequest request) {
		System.out.println("this is action method");
		return "/user/list.jsp";
	}
}

 

@RemoteProxy注解告诉DWR,这个Class是我们想暴露出来的。@RemoteMethod注解告诉DWR去暴露这个指定的方法,只有加了RemoteMethod注解的方法会被暴露,其它的不会。

这里也可以使用@RemoteProxy(name="userRemote")的方式指定DWR接口的名字

 

2. 接下来看web.xml的配置

 

	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
    </servlet>

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

    <servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

 

注意,这里没有使用org.directwebremoting.spring.DwrSpringServlet或org.directwebremoting.servlet.DwrServlet,并且请注意这里使用spring的dispatcher servlet来映射/dwr/*请求。

 

3. 最后看针对DispatcherServlet的配置文件action-servlet.xml

 

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd 
        http://www.directwebremoting.org/schema/spring-dwr  
        http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"
        default-autowire="byName">

	<context:annotation-config />
	
	<!-- 注意这里新增加的dwr tag, 为使其生效,文件头中要声明namespace -->
	<dwr:configuration />
	<dwr:annotation-config />
	<dwr:url-mapping />
	
	<!-- 部署项目时, 请把debug设为false -->
	<dwr:controller id="dwrController" debug="true" />
	
	<!-- 多个包名用逗号隔开, 但不能有空格 -->
	<context:component-scan base-package="com.myapp.web.controller" />
	
	<!-- order值越小, 优先级越高 -->
	<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
		<property name="order" value="1" />
	</bean>
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
		<property name="order" value="2" />
	</bean>
    
 </beans>

 

简单解释一下这些配置

  • <dwr:annotation-config /> 要求DWR在Spring容器中检查拥有@RemoteProxy 和 @RemoteMethod注解的类。注意它不会去检查Spring容器之外的类。
  • <dwr:url-mapping /> 要求DWR将util.js和engine.js映射到dwrController
  • <dwr:controller id="dwrController" debug="true" /> 定义dwrController
  • <dwr:configuration /> 此标签在这个例子中不是必须的,如果你想配置Spring容器之外的类,就需要它了。

最后一件事,DWR的测试页面 http://localhost:8080/myapp/dwr 在这里不好用。

请使用 http://localhost:8080/myapp/dwr/test/YOUR-BEAN-NAME 的方式来测试你的DWR接口,

例如这里使用 http://localhost:8080/myapp/dwr/test/UserController

 

OK 运行测试一下吧

 

原文 http://www.codercorp.com/blog/spring/configuring-dwr-30-with-spring-using-annotations.html

   发表时间:2009-07-29  
在spring配置文件最后加上
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	 <property name="order" value="3" /> 
	  <property value="true" name="alwaysUseFullPath"></property>
	  <property name="mappings">
	    <props>
	      <prop key="/dwr/**">dwrController</prop>
	    </props>
	 </property>
	</bean> 


就可以通过http://localhost:8080/myApp/dwr来访问test page了
2 请登录后投票
   发表时间:2010-01-22  
spring2.5 和 dwr3 结合,在<离线>部署的时候报错:
严重: StandardWrapper.Throwable
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 18 in XML document from ServletContext resource [/WEB-INF/XXX-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dwr:annotation-config'.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:404)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:92)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:123)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:422)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:402)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:316)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:282)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:126)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:806)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'dwr:annotation-config'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator$XSIErrorReporter.reportError(XMLSchemaValidator.java:410)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.reportSchemaError(XMLSchemaValidator.java:3165)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement(XMLSchemaValidator.java:1898)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.emptyElement(XMLSchemaValidator.java:705)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:377)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2747)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:225)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:283)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:75)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
... 27 more

是找不到:http://www.directwebremoting.org/schema/spring-dwr??
0 请登录后投票
   发表时间:2010-04-01  
annotation 后确实方便很多.

@xiuying:
你是否确定用的是dwr3.x?
另外, namespace有没有?

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd  
        http://www.directwebremoting.org/schema/spring-dwr   
        http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd

        default-autowire="byName">



@wangcheng
我还喜欢把context:component-scan 具体化一点...

<context:component-scan base-package="com.zr6r"
		annotation-config="true">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
	</context:component-scan>

	<dwr:annotation-scan base-package="com.zr6r" />
1 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics