dwr3.0与Spring mvc的全注解集成方法

用上面的方法集成虽然可行,但是比较麻烦,需要定义dwrServlet以及把要注册的类写到web.xml中,而且实际应用中会出现注册的远程类里Autowired的bean不能实例化的问题。dwr3.0可以通过全注解的方式,极大的简化了配置,所有xml配置加在一起不超过20行,而且使用更加简单,bean注入的问题也都解决。配置步骤如下:

  1. web.xml的配置文件中,在认的dispatcherServlet的mapping里加上dwr

    < servlet >
    servlet-name >appServlet</ >
    servlet-class >org.springframework.web.servlet.dispatcherServlet</ >
    init-param >
    param-name >contextConfigLocation</ >
    param-value >/WEB-INF/spring/appServlet/appServlet-context.xml</ >
    </ >
    load-on-startup >1</ >
    >
    >
    url-pattern >/</ >
    >
    >
    >
    >/dwr/*</ >
    >
  2. dispatcherServlet的配置文件appServlet-context.xml

    <?xml version="1.0" encoding"UTF-8"?>
    beans:beans xmlns "<a href="http://www.springframework.org/schema/mvc" "="" style="outline: none; color: rgb(0,109,175); border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.2em !important; margin: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-size: 1em !important; min-height: auto !important;">http://www.springframework.org/schema/mvc"
    xmlns:xsi "<a href="http://www.w3.org/2001/XMLSchema-instance" "="" style="outline: none; color: rgb(0,175); border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.2em !important; margin: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-size: 1em !important; min-height: auto !important;">http://www.w3.org/2001/XMLSchema-instance" xmlns:beans "<a href="http://www.springframework.org/schema/beans" "="" style="outline: none; color: rgb(0,175); border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.2em !important; margin: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-size: 1em !important; min-height: auto !important;">http://www.springframework.org/schema/beans"
    xmlns:context "<a href="http://www.springframework.org/schema/context" "="" style="outline: none; color: rgb(0,175); border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.2em !important; margin: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-size: 1em !important; min-height: auto !important;">http://www.springframework.org/schema/context"
    xmlns:dwr "<a href="http://www.directwebremoting.org/schema/spring-dwr" "="" style="outline: none; color: rgb(0,175); border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.2em !important; margin: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-size: 1em !important; min-height: auto !important;">http://www.directwebremoting.org/schema/spring-dwr"
    http://www.directwebremoting.org/schema/spring-dwr <a href="http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd" "="" style="outline: none; color: rgb(0,175); border-top-left-radius: 0px !important; border-top-right-radius: 0px !important; border-bottom-right-radius: 0px !important; border-bottom-left-radius: 0px !important; background-image: none !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; line-height: 1.2em !important; margin: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; Box-sizing: content-Box !important; font-size: 1em !important; min-height: auto !important;">http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
    <!-- dispatcherServlet Context: defines this servlet's request-processing
    infrastructure -->
    <!-- Enables the Spring MVC @Controller programming model -->
    annotation-driven />
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving
    up static resources in the ${webappRoot}/resources directory -->
    resources mapping "/resources/**" location "/resources/" />
    interceptors beans:bean class "com.panguso.op.data.manager.interceptor.LogInterceptor" ></ beans:bean >
    >
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources
    in the /WEB-INF/views directory -->
    beans:bean
    class "org.springframework.web.servlet.view.InternalResourceViewResolver" >
    beans:property name "prefix" value "/WEB-INF/views/" />
    "suffix" ".jsp" />
    >
    <!--<view-controller path="/" view-name="index" /> -->
    context:component-scan base-package "com.panguso.op.data.manager" />
    <!-- 从这行往下是要添加的 -->
    context:annotation-config />
    dwr:configuration />
    dwr:annotation-config />
    dwr:url-mapping />
    dwr:controller id "dwrController" debug "true" >
    dwr:config-param "allowScriptTagRemoting"
    value />
    "crossDomainSessionSecurity"
    "false" />
    dwr:controller >
    beans:bean
    "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
    "order" "1" />
    >
    beans:bean
    "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" >
    "2" />
    >
    beans:bean
    "org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
    "3" />
    "true" "alwaysUseFullPath" />
    "mappings" >
    beans:props >
    beans:prop key "/dwr/**" >dwrController</ beans:prop >
    >
    beans:property >
    >
    <!--添加结束-->
    beans:beans >
  • 配置完毕,使用的方法和上文类似,注意的是@RemoteProxy标注的远程类一定要加上name属性,不然启动会报错,如:

    @Service
    @RemoteProxy (name = "GeneralContentManagementService" )
    public class GeneralContentManagementService {
    @RemoteMethod
    void aaa()
    {
    }
    }
  • 相关文章

    IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
    PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
    使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
    Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
    本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
    Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...