让Dwr和Spring-MVC通过Annotation方式共同工作

[quote][/quote]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访问,如:
[url]http://localhost/mvc/helloController.htm?method=sayHello[/url]
或者在页面中用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在
[url]http://www.nabble.com/attachment/15108217/0/annotations.war[/url]
下载
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值