首先還是配置好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>
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>
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>
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. */
5. package com.sofmit.sim.wr.web;
6.
7. import org.springframework.stereotype.Controller;
8. import org.springframework.ui.Model;
9. import org.springframework.web.bind.annotation.RequestMapping;
10. import org.springframework.web.bind.annotation.RequestMethod;
11.
12. /**
13. * @author TianYe
14. * 2008-2-14
15. */
16. @Controller
17. @RequestMapping("/hello.html")
18. public class Hello {
19.
20. @RequestMapping(method = RequestMethod.GET)
21. public String sayHello(Model model){
22. model.addAttribute("say","hello");
23. return "test";
24. }
25. }
2. * com.sofmit.sim.wr.web
3. * Hello.java
4. */
5. package com.sofmit.sim.wr.web;
6.
7. import org.springframework.stereotype.Controller;
8. import org.springframework.ui.Model;
9. import org.springframework.web.bind.annotation.RequestMapping;
10. import org.springframework.web.bind.annotation.RequestMethod;
11.
12. /**
13. * @author TianYe
14. * 2008-2-14
15. */
16. @Controller
17. @RequestMapping("/hello.html")
18. public 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>
2. <h2>Test Page say:${say}</h2>