迁移应用进入基于Annotation MVC的spring 2.5

本文介绍如何将Spring2.0应用迁移到Spring2.5的MVC控制器,特别是使用基于注解的配置。展示了如何定义多动作控制器,并提供了一个简单的示例。
Spring 2.5引入了基于Annotation配置的MVC controllers。这篇简短的文章介绍了需要如何迁移你的spring 2.0应用到spring 2.5,至少是需要迁移MVC相关的应用。

首先保证你已经将spring-webmvc.jar放在你的classpath内,DispatcherServlet不再是spring.jar的一部分,现在是在一个单独的模块内。

任何controller class能够通过一到两种方式设置,controller能够控制一个或者多个action。下面是一个包含三个独立action基本的多action controller例子。

Java代码 复制代码
  1. packagedemo;
  2. importorg.springframework.stereotype.Controller;
  3. importorg.springframework.web.bind.annotation.RequestMapping;
  4. @Controller
  5. publicclassSimpleController{
  6. @RequestMapping("/index.html")
  7. publicvoidindexHandler(){
  8. }
  9. @RequestMapping("/about.html")
  10. publicvoidaboutHandler(){
  11. }
  12. @RequestMapping("/admin.html")
  13. publicvoidadminHandler(){
  14. }
  15. }
package demo; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class SimpleController { @RequestMapping("/index.html") public void indexHandler() { } @RequestMapping("/about.html") public void aboutHandler() { } @RequestMapping("/admin.html") public void adminHandler() { } }

即 使这是一个最简单的例子,有一些重要的地方需要注意,尤其你使用的是spring早期版本。第一,你应该注意到controller是POJO,它没有扩 展AbstractController,或者其他controller class,你在spring早期版本会这么做,第二,注意annotations,我已经通过@Controller annotation来标记处controller本身,用@RequestMapping annotations标记独立的methods。我也通过annotation做URL mapping。最后,用request URL来定位logic view name,如不指定DispatcherServlet会自动匹配/index.htm到logical name "index"等.

application context config file 配置如下:


Java代码 复制代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  9. <context:component-scanbase-package="demo"/>
  10. <beanid="viewResolver"
  11. class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  12. <propertyname="prefix"value="/WEB-INF/jsp/"/>
  13. <propertyname="suffix"value=".jsp"/>
  14. </bean>
  15. </beans>
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="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"> <context:component-scan base-package="demo"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>

如果你想了解spring MVC深度配置请看 Annotated Web MVC Controllers in Spring 2.5.

如果你想配置应用程序其他层,请看 Annotation-Based Autowiring in Spring 2.5


来自:wheelersoftware.com
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值