posts - 15,  comments - 8,  trackbacks - 0
由于Spring 2.5全面支持annotation方式,因此大大减少了XML的配置,提高了程序代码的维护和生产效率。下面是我做的一个简单的Hello World!,使用Spring mvc。所有配置全部用annotation,大家可以看看是多么的简单。

首先还是配置好web.xml文件,主要添加的代码有:

   1. <listener>  
   2.         
<listener-class>  
   3.             org.springframework.web.util.Log4jConfigListener  
   4.         
</listener-class>  
   5.     
</listener>  
   6.   
   7.     
<listener>  
   8.         
<listener-class>  
   9.             org.springframework.web.context.ContextLoaderListener  
  10.         
</listener-class>  
  11.     
</listener>  
  12.   
  13.     
<welcome-file-list>  
  14.         
<welcome-file>index.jsp</welcome-file>  
  15.     
</welcome-file-list>  
  16.   
  17.     
<servlet>  
  18.         
<servlet-name>sim</servlet-name>  
  19.         
<servlet-class>  
  20.             org.springframework.web.servlet.DispatcherServlet  
  21.         
</servlet-class>  
  22.     
</servlet>  
  23.   
  24.     
<servlet-mapping>  
  25.         
<servlet-name>sim</servlet-name>  
  26.         
<url-pattern>*.html</url-pattern>  
  27.     
</servlet-mapping> 

以上的配置和以前是一样的,所以没什么好说的。
然后接着我们建立一个applicationContext.xml,作为SPRING的配置文件。以往版本里面SPRING的 这个配置文件往往随着程序的增加而越来越臃肿,就我本人来说就十分不喜欢这种方式。因为程序员需要花费大量的时间去管理和维护自己的XML文件,大大的减 少了程序员的生产效率。并且由于大量的XML文件,使得新加入团队的人员学习成本的增加,往往造成了很多工时上的浪费。终于,2.5版本开始支持了annotation,使得这个问题可以得到一定的解决。那么好,下面我们来看看现在的配置文件。

   1. <?xml version="1.0" encoding="UTF-8"?>  
   2. 
<beans xmlns="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:annotation-config />  
  10. 
</beans> 

很好,很强大,简单一句话搞定。
搞定了IOC的配置文件,那么我们需要一个MVC的配置XML,如果没有annotation,那么这个文件一样会越来越臃肿。然后现在呢?
sim-servlet.xml

   1. <?xml version="1.0" encoding="UTF-8"?>  
   2. 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   3.         xmlns:p
="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"  
   4.         xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
   5.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
>  
   6.       
   7.     
<!-- 
   8.         - The controllers are autodetected POJOs labeled with the @Controller annotation. 
   9.     
-->  
  10.     
<context:component-scan base-package="com.sofmit.sim.wr.web"/>  
  11.       
  12.     
<!--  
  13.         - This bean configures the 'prefix' and 'suffix' properties of   
  14.         - InternalResourceViewResolver, which resolves logical view names   
  15.         - returned by Controllers. For example, a logical view name of "vets"   
  16.         - will be mapped to "/WEB-INF/jsp/vets.jsp".  
  17.     
-->  
  18.     
<bean  
  
19.         class="org.springframework.web.servlet.view.InternalResourceViewResolver"  
  20.         p:prefix
="/WEB-INF/jsp/" p:suffix=".jsp" />  
  21. 
</beans> 

同样十分简单,在这里只需要指定好CONTROLLER的包就可以了。

现在配置已经全部OK了,是不是感觉很轻松?哈哈,原来annotation真是如此美妙,为我们节约了大量的配置XML的时间。
下面就只需要一个controller和一个JSP页面就可以完成我们这儿APPLICATION了,那么我们继续。
首先就是controller的定义。

   1/** 
   2.  * com.sofmit.sim.wr.web 
   3.  * Hello.java 
   4.  
*/  
   
5package com.sofmit.sim.wr.web;  
   
6.   
   
7import org.springframework.stereotype.Controller;  
   
8import org.springframework.ui.Model;  
   
9import org.springframework.web.bind.annotation.RequestMapping;  
  
10import org.springframework.web.bind.annotation.RequestMethod;  
  
11.   
  
12/** 
  13.  * 
@author TianYe 
  14.  * 2008-2-14 
  15.  
*/  
  
16. @Controller  
  
17. @RequestMapping("/hello.html")  
  
18public class Hello {  
  
19.   
  
20.     @RequestMapping(method = RequestMethod.GET)  
  
21.     public String sayHello(Model model){  
  
22.         model.addAttribute("say","hello");  
  
23.         return "test";  
  
24.     }  
  
25. } 

在这里面,使用@Controller定义此类是一个Spring MVC的controller。然后定义好访问的路径"/hello.html"。在方法上面定义好通过GET方式访问时调用我们的sayHello方法,在方法中定义好要传给JSP页面的变量"say"以及内容"hello",然后定位到页面"test"。
一个简单的controller就实现了。

剩下的就没有什么技术含量了,就是一个简单的JSP页面。

   1. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
   2. 
<h2>Test Page say:${say}</h2> 

posted on 2008-09-22 11:39 lvq810 阅读(816) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: