dwr3.0可以通过全注解的方式,极大的简化了配置,所有xml配置加在一起不超过20行,而且使用更加简单,bean注入的问题也都解决。配置步骤如下:
web.xml的配置文件中,在默认的DispatcherServlet的mapping里加上dwr
<@H_502_7@servlet>@H_502_7@
<@H_502_7@servlet-name@H_502_7@>@H_502_7@appServlet<@H_502_7@/servlet-name@H_502_7@>@H_502_7@
<@H_502_7@servlet-class@H_502_7@>@H_502_7@org.@H_502_7@springframework.@H_502_7@web.@H_502_7@servlet.@H_502_7@DispatcherServlet<@H_502_7@/servlet-class@H_502_7@>@H_502_7@
<@H_502_7@init-param@H_502_7@>@H_502_7@
<@H_502_7@param-name@H_502_7@>@H_502_7@contextConfigLocation<@H_502_7@/param-name@H_502_7@>@H_502_7@
<@H_502_7@param-value@H_502_7@>@H_502_7@/WEB-INF@H_502_7@/spring/appServlet/appServlet-context@H_502_7@.@H_502_7@xml@H_502_7@<@H_502_7@/param-value@H_502_7@>@H_502_7@
<@H_502_7@/init-param@H_502_7@>@H_502_7@
<@H_502_7@load-on@H_502_7@-startup@H_502_7@>@H_502_7@1@H_502_7@<@H_502_7@/load-on@H_502_7@-startup@H_502_7@>@H_502_7@
<@H_502_7@/servlet>@H_502_7@
<@H_502_7@servlet-mapping@H_502_7@>@H_502_7@
<@H_502_7@servlet-name@H_502_7@>@H_502_7@appServlet<@H_502_7@/servlet-name@H_502_7@>@H_502_7@
<@H_502_7@url-pattern@H_502_7@>@H_502_7@/@H_502_7@<@H_502_7@/url-pattern@H_502_7@>@H_502_7@
<@H_502_7@/servlet-mapping@H_502_7@>@H_502_7@
<@H_502_7@servlet-mapping@H_502_7@>@H_502_7@
<@H_502_7@servlet-name@H_502_7@>@H_502_7@appServlet<@H_502_7@/servlet-name@H_502_7@>@H_502_7@
<@H_502_7@url-pattern@H_502_7@>@H_502_7@/dwr/*</url-pattern> </servlet-mapping>@H_502_7@
dispatcherServlet的配置文件appServlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>@H_502_7@
<beans:beans@H_502_7@ xmlns@H_502_7@="http://www.springframework.org/schema/mvc"@H_502_7@ xmlns:xsi@H_502_7@="http://www.w3.org/2001/XMLSchema-instance"@H_502_7@ xmlns:beans@H_502_7@="http://www.springframework.org/schema/beans"@H_502_7@ xmlns:context@H_502_7@="http://www.springframework.org/schema/context"@H_502_7@ xmlns:dwr@H_502_7@="http://www.directwebremoting.org/schema/spring-dwr"@H_502_7@ xsi:schemaLocation@H_502_7@="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"@H_502_7@>@H_502_7@
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->@H_502_7@
<!-- Enables the Spring MVC @Controller programming model -->@H_502_7@
<annotation-driven@H_502_7@ />@H_502_7@
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->@H_502_7@
<resources@H_502_7@ mapping@H_502_7@="/resources/**"@H_502_7@ location@H_502_7@="/resources/"@H_502_7@ />@H_502_7@
<interceptors@H_502_7@>@H_502_7@
<beans:bean@H_502_7@ class@H_502_7@="com.panguso.op.data.manager.interceptor.LogInterceptor"@H_502_7@>@H_502_7@</beans:bean@H_502_7@>@H_502_7@
</interceptors@H_502_7@>@H_502_7@
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->@H_502_7@
<beans:bean @H_502_7@ class@H_502_7@="org.springframework.web.servlet.view.InternalResourceViewResolver"@H_502_7@>@H_502_7@
<beans:property@H_502_7@ name@H_502_7@="prefix"@H_502_7@ value@H_502_7@="/WEB-INF/views/"@H_502_7@ />@H_502_7@
<beans:property@H_502_7@ name@H_502_7@="suffix"@H_502_7@ value@H_502_7@=".jsp"@H_502_7@ />@H_502_7@
</beans:bean@H_502_7@>@H_502_7@
<!--<view-controller path="/" view-name="index" /> -->@H_502_7@
<context:component-scan@H_502_7@ base-package@H_502_7@="com.panguso.op.data.manager"@H_502_7@ />@H_502_7@
<!-- 从这行往下是要添加的 -->@H_502_7@
<context:annotation-config@H_502_7@ />@H_502_7@
<dwr:configuration@H_502_7@ />@H_502_7@
<dwr:annotation-config@H_502_7@ />@H_502_7@
<dwr:url-mapping@H_502_7@ />@H_502_7@
<dwr:controller@H_502_7@ id@H_502_7@="dwrController"@H_502_7@ debug@H_502_7@="true"@H_502_7@>@H_502_7@
<dwr:config-param@H_502_7@ name@H_502_7@="allowScriptTagRemoting"@H_502_7@ value@H_502_7@="true"@H_502_7@ />@H_502_7@
<dwr:config-param@H_502_7@ name@H_502_7@="crossDomainSessionSecurity"@H_502_7@ value@H_502_7@="false"@H_502_7@ />@H_502_7@
</dwr:controller@H_502_7@>@H_502_7@
<beans:bean @H_502_7@ class@H_502_7@="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"@H_502_7@>@H_502_7@
<beans:property@H_502_7@ name@H_502_7@="order"@H_502_7@ value@H_502_7@="1"@H_502_7@ />@H_502_7@
</beans:bean@H_502_7@>@H_502_7@
<beans:bean @H_502_7@ class@H_502_7@="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"@H_502_7@>@H_502_7@
<beans:property@H_502_7@ name@H_502_7@="order"@H_502_7@ value@H_502_7@="2"@H_502_7@ />@H_502_7@
</beans:bean@H_502_7@>@H_502_7@
<!--添加结束-->@H_502_7@
</beans:beans@H_502_7@>@H_502_7@
配置完毕,使用的方法和上文类似,注意的是@RemoteProxy标注的远程类一定要加上name属性,不然启动会报错,如:
@Service@H_502_7@
@RemoteProxy@H_502_7@(name = "GeneralContentManagementService"@H_502_7@)
public@H_502_7@ class@H_502_7@ GeneralContentManagementService@H_502_7@ {@H_502_7@
@RemoteMethod@H_502_7@
public@H_502_7@ void@H_502_7@ aaa@H_502_7@()
{
}
}