浏览 808 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
最后更新时间:2008-06-28
引用 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 下载 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
最后更新时间:2008-06-28
哦,DWR官方已经有Annotation了?那我的可以考虑不用了
|
|
| 返回顶楼 | |
|
最后更新时间:2008-08-05
|
|
| 返回顶楼 | |
|
最后更新时间:2008-08-06
貌似没怎么简化
|
|
| 返回顶楼 | |
|
最后更新时间:2008-08-08
还行吧,dwr跟spring是紧密结合了
|
|
| 返回顶楼 | |
|
最后更新时间:2008-09-20
<dwra:url-mapping /> <dwra:annotation-config /> 需要下载第三方的JAR,地址: http://www.nabble.com/attachment/15108217/0/annotations.war |
|
| 返回顶楼 | |






