以下示例演示如何使用Spring MVC框架編寫一個簡單的基於Web的Hello World
應用程式。首先使用Eclipse IDE,並按照以下步驟使用Spring Web Framework開發一個動態Web應用程式:
創建一個名為
HelloWeb
的動態Web專案,並在創建的專案中的src
檔夾下創建一個包com.zaixian.springmvc
。將下麵提到的Spring和其他庫拖放到檔夾
WebContent/WEB-INF/lib
中。- 在
com.zaixian.springmvc
包下創建一個Java類HelloController
。 - 在
WebContent/WEB-INF
檔夾下創建Spring配置檔web.xml
和HelloWeb-servlet.xml
。 - 在
WebContent/WEB-INF
檔夾下創建一個名為jsp
的子檔夾。在此子檔夾下創建視圖檔hello.jsp
。 - 最後一步是創建所有源和配置檔的內容並導出應用程式或直接在Eclipse中運行,如下所述。
創建完成後,整個工程的目錄結構如下圖所示 -
HelloController.java
package com.zaixian.springmvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
@Controller
@RequestMapping("/hello")
public class HelloController{
@RequestMapping(method = RequestMethod.GET)
public String printHello(ModelMap model) {
model.addAttribute("message", "Hello Spring MVC Framework!");
return "hello";
}
}
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloWeb-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.zaixian" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
hello.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>Hello, ${message}</h2>
</body>
</html>
最後,以下是要包括在Web應用程式中的Spring和其他庫的列表。只需拖動這些檔並將其放在WebContent/WEB-INF/lib
檔夾中。
- servlet-api-x.y.z.jar
- commons-logging-x.y.z.jar
- spring-aop-x.y.z.jar
- spring-beans-x.y.z.jar
- spring-context-x.y.z.jar
- spring-core-x.y.z.jar
- spring-expression-x.y.z.jar
- spring-webmvc-x.y.z.jar
- spring-web-x.y.z.jar
完成創建源和配置檔後,導出應用程式。右鍵單擊您的應用程式,並使用導出> WAR檔選項,並將 HelloWeb.war
檔保存在Tomcat的webapps
檔夾中。
現在啟動 Tomcat 伺服器,並確保能夠使用標準流覽器訪問到 webapps
檔夾的其他網頁。 現在嘗試訪問URL => http://localhost:8080/HelloWeb/hello
,如果一切都沒有問題,Spring Web應用程式,應該看到以下結果:
應該要注意,在給定的URL中,HelloWeb
是應用程式名稱,hello
是在控制器中使用@RequestMapping(“/hello”)
提到的虛擬子檔夾。在使用@RequestMapping(“/”)
映射到URL時,可以直接使用根(“/“),在這種情況下,可以使用短URL => http://localhost:8080/HelloWeb/
訪問同一頁面,但建議使用不同的檔夾。
注意:如果沒有在Eclipse上安裝配置過Tomcat伺服器,在 Window -> Preference -> Server -> Runtime Environments 設置。