引用
SPRING2.X后引入了@Controller的方式标注Controller,大大简化了原来的XML方式的MVC配置,不过传统的DWR配置方式,虽然可以通过SpringCreator的方式将BEAN暴露给AJAX使用,但是仍然逃不出繁杂的XML配置,通过SPRING的annotation方式,可以简化DWR和SPRING之间的配置.
web.xml
<!-- Spring -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/webControllerContext.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
将DispatcherServlet配置成管理htm和dwr
webControllerContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xmlns:dwra="http://www.directwebremoting.org/schema/spring-dwr-annotations"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
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.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd
http://www.directwebremoting.org/schema/spring-dwr-annotations
http://www.directwebremoting.org/schema/spring-dwr-annotations.xsd">
<context:annotation-config/>
<context:component-scan base-package="mvc" />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!--
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="" p:suffix=".jsp" />
<!-- DWR -->
<dwr:configuration>
<dwr:convert class="mvc.User" type="bean" />
<dwr:signatures>
<![CDATA[
import java.util.Map;
]]>
</dwr:signatures>
</dwr:configuration>
<dwr:controller id="dwrController" debug="true" />
<!-- New DWR capabilities-->
<dwra:url-mapping />
<dwra:annotation-config />
</beans>
@Controller("helloController")
@RequestMapping("/helloController.htm")
@RemoteProxy(creator = SpringCreator.class, creatorParams = @Param(name = "beanName", value = "helloController"), name = "dwrHelloController")
public class Hello {
/**
* User Dao
*/
private UserDao userDao;
/**
* Employee dao
*/
private EmployeeDao employeeDao;
@RemoteMethod
@RequestMapping
public String sayHello() {
String str = "hello";
System.out.println(str);
return str;
}
@RemoteMethod
public User getUser() {
User user = this.getUserDao().getUser();
user.setName(user.getName() + " _ "
+ this.getEmployeeDao().getEmployeeName());
return user;
}
@RequestMapping(params = "method=sayBye")
public ModelAndView sayBye() {
String str = "bye";
System.out.println(str);
return null;
}
//...忽略set和get
}
可以看到,首先通过:
@Controller("helloController")
@RequestMapping("/helloController.htm")
将POJO暴露成一个SpringMvc的Controller,然后在DI给DWR,成为一个AJAX服务:
@RemoteProxy(creator = SpringCreator.class, creatorParams = @Param(name = "beanName", value = "helloController"), name = "dwrHelloController")
在指定的方法上面标注:
@RemoteMethod
@RequestMapping(params = "method=sayHello")
public String sayHello() {...}
这样,这个方法既可以成为一个controller资源,也可以被DWR访问,如:
http://localhost/mvc/helloController.htm?method=sayHello
或者在页面中用JS访问:
dwrHelloController.sayHello(function callbackFun(data){
alert(data);});
一举多得,实现代码的最小量
需要注意的地方:
spring的XML配置中:
http://www.directwebremoting.org/schema/spring-dwr-annotations
http://www.directwebremoting.org/schema/spring-dwr-annotations.xsd
<!-- New DWR capabilities-->
<dwra:url-mapping />
<dwra:annotation-config />
不是标准的,需要引入第三方包:DwrSpringAnnotations.jar,作为DWR.JAR的补充,可以使DWR引用spring-annntation方式实例化的bean,该jar在
http://www.nabble.com/attachment/15108217/0/annotations.war
下载
分享到:
相关推荐
将dwr集成到spring mvc(dwr的配置是基于xml)
配置整合DWR3.0和Spring2.5使用annotation注解
整合spring3mvc+hibernate4+spring-security3+dwr3的架包
spring-dwr-2.0.xsd spring 与 DWR进行配置
Dwr3+Spring3 全注解 annotation 方式实现
DWR整合Spring MVC ,前端jsp文件调用后台java方法,适合DWR学习
spring-hibernate-dwr做的AJAX操作CRUD实例 ...1.3.jar poi-2.5.1.jar cos.jar velocity-1.5.jar velocity-tools-view-1.3.jar commons-codec.jar dwr-2.0.rc2.jar asm.jar files.lst
这个不是我的原创,原文件出至这里:...原资源包含了,源代码和API Doc,只是美中不足的是没有整理成可用的zip文档,我这特分离了一下,并重新打好包了,初步测试了一下能用。
dwr+spring集成配置 dwr采用annotation自动扫描的方式
spring使用annotation整合dwr笔记 spring使用annotation整合dwr笔记 spring使用annotation整合dwr笔记
dwr资源包,包含dwr.jar/dwr-2.0.5-src.zip/dwr.zip
DWR入门程序---计算输入的两个数之和.rar DWR入门程序---计算输入的两个数之和.rar DWR入门程序---计算输入的两个数之和.rar
dwr-1.1.3-src.zip
dwr+spring 的消息发布小例子,增删改查都有,好东西要大家分享!
该文件中包括了dwr框架和spring框架结合使用方法,使用注解方式。
dwr与spring集成的两种方式,来之网上搜索的文档。。
DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX技术的网站,其中包含jar包,javadoc,例子等
这个jar是很难找的,是dwr在spring环境里面,使用注解的必须包,希望对你有用。