dwr3.0与Spring mvc的全注解集成方法( @RemoteMethod)(@RemoteProxy)

dwr3.0可以通过全注解的方式,极大的简化了配置,所有xml配置加在一起不超过20行,而且使用更加简单,bean注入的问题也都解决。配置步骤如下:

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

<servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/appServlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/dwr/*
    

dispatcherServlet的配置文件appServlet-context.xml


<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dwr="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 http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
    
    
    <annotation-driven />
    
    <resources mapping="/resources/**" location="/resources/" />
    <interceptors>
        <beans:bean class="com.panguso.op.data.manager.interceptor.LogInterceptor">beans:bean>
    interceptors>
    
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    beans:bean>
    
    <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 name="allowScriptTagRemoting"
            value="true" />
        <dwr:config-param name="crossDomainSessionSecurity"
            value="false" />
    dwr:controller>
    <beans:bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <beans:property name="order" value="1" />
    beans:bean>
    <beans:bean
        class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
        <beans:property name="order" value="2" />
    beans:bean>
    
beans:beans>

配置完毕,使用的方法和上文类似,注意的是@RemoteProxy标注的远程类一定要加上name属性,不然启动会报错,如:

@Service
@RemoteProxy(name = "GeneralContentManagementService")
public class GeneralContentManagementService {

    @RemoteMethod
    public void aaa()
    {
    }
}

你可能感兴趣的:(JAVA,web)