加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:00:23 所属栏目:百科 来源:网络整理
导读:用上面的方法集成虽然可行,但是比较麻烦,需要定义dwrServlet以及把要注册的类写到web.xml中,而且实际应用中会出现注册的远程类里Autowired的bean不能实例化的问题。dwr3.0可以通过全注解的方式, 极大的简化了配置,所有xml配置加在一起不超过20行, 而且

用上面的方法集成虽然可行,但是比较麻烦,需要定义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</ >
    >
    servlet-mapping >
    >
    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"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    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 >
  3. 配置完毕,使用的方法和上文类似,注意的是@RemoteProxy标注的远程类一定要加上name属性,不然启动会报错,如:

    @Service
    @RemoteProxy (name = "GeneralContentManagementService" )
    public class GeneralContentManagementService {
    @RemoteMethod
    void aaa()
    {
    }
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读