zoukankan      html  css  js  c++  java
  • 迁移应用进入基于Annotation MVC的spring 2.5

    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. package demo;  
    2.       
    3. import org.springframework.stereotype.Controller;  
    4. import org.springframework.web.bind.annotation.RequestMapping;  
    5.       
    6. @Controller  
    7. public class SimpleController {  
    8.       
    9.     @RequestMapping("/index.html")  
    10.     public void indexHandler() {  
    11.     }  
    12.       
    13.     @RequestMapping("/about.html")  
    14.     public void aboutHandler() {  
    15.     }  
    16.       
    17.     @RequestMapping("/admin.html")  
    18.     public void adminHandler() {  
    19.     }  
    20. }  


    即 使这是一个最简单的例子,有一些重要的地方需要注意,尤其你使用的是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. <?xml version="1.0" encoding="UTF-8"?>  
    2.    
    3. <beans xmlns="http://www.springframework.org/schema/beans"  
    4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    5.     xmlns:context="http://www.springframework.org/schema/context"  
    6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
    7.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    8.         http://www.springframework.org/schema/context  
    9.         http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
    10.       
    11.     <context:component-scan base-package="demo"/>  
    12.       
    13.     <bean id="viewResolver"  
    14.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    15.         <property name="prefix" value="/WEB-INF/jsp/"/>  
    16.         <property name="suffix" value=".jsp"/>  
    17.     </bean>  
    18. </beans>  


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

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

    来自:wheelersoftware.com
  • 相关阅读:
    一波骚操作,我把 SQL 执行效率提高了 10,000,000 倍!
    如何优雅地根治null值引起的Bug!
    解锁新姿势:探讨复杂的 if-else 语句“优雅处理”的思路
    39 个奇葩代码注释,看完笑哭了。。。
    只要学会它,再多 Bug 也不怕
    SpringBoot 快速整合Mybatis(去XML化+注解进阶)
    Java 并发异步编程,原来十个接口的活现在只需要一个接口就搞定!
    微服务 2.0 技术栈选型手册
    如何设计 API 接口,实现统一格式返回?
    别在 Java 代码里乱打日志了,这才是打印日志的正确姿势!
  • 原文地址:https://www.cnblogs.com/lanzhi/p/6470545.html
Copyright © 2011-2022 走看看