Spring MVC Hello World 示例



以下示例演示瞭如何使用 Spring MVC 框架編寫一個簡單的基於 Web 的 Hello World 應用程式。首先,我們需要一個可用的 Eclipse IDE,並按照以下步驟使用 Spring Web 框架開發一個動態 Web 應用程式:

步驟 描述
1 建立一個名為 HelloWeb動態 Web 專案,並在建立的專案的 src 資料夾下建立一個包 com.tutorialspoint
2 將下面提到的 Spring 及其他庫拖放到 WebContent/WEB-INF/lib 資料夾中。
3 com.tutorialspoint 包下建立一個 Java 類 HelloController
4 WebContent/WEB-INF 資料夾下建立 Spring 配置檔案 web.xmlHelloWeb-servlet.xml
5 WebContent/WEB-INF 資料夾下建立一個名為 jsp 的子資料夾。在此子資料夾下建立一個檢視檔案 hello.jsp
6 最後一步是建立所有原始檔和配置檔案的內容,並按如下所述匯出應用程式。

以下是 HelloController.java 檔案的內容

package com.tutorialspoint;

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";
   }
}

以下是 Spring Web 配置檔案 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>

以下是另一個 Spring Web 配置檔案 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.tutorialspoint" />

   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/jsp/" />
      <property name = "suffix" value = ".jsp" />
   </bean>

</beans>

以下是 Spring 檢視檔案 hello.jsp 的內容

<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
   <head>
      <title>Hello World</title>
   </head>
   
   <body>
      <h2>${message}</h2>
   </body>
</html>

最後,以下是需要包含在 Web 應用程式中的 Spring 及其他庫的列表。只需將這些檔案拖放到 WebContent/WEB-INF/lib 資料夾中。

  • commons-logging-x.y.z.jar
  • org.springframework.asm-x.y.z.jar
  • org.springframework.beans-x.y.z.jar
  • org.springframework.context-x.y.z.jar
  • org.springframework.core-x.y.z.jar
  • org.springframework.expression-x.y.z.jar
  • org.springframework.web.servlet-x.y.z.jar
  • org.springframework.web-x.y.z.jar
  • spring-web.jar

建立完原始檔和配置檔案後,匯出應用程式。右鍵單擊應用程式,使用匯出 > WAR 檔案選項,並將 HelloWeb.war 檔案儲存到 Tomcat 的 webapps 資料夾中。

現在啟動 Tomcat 伺服器,並確保您可以使用標準瀏覽器從 webapps 資料夾訪問其他網頁。嘗試訪問 URL https://:8080/HelloWeb/hello,如果您的 Spring Web 應用程式一切正常,您應該會看到以下結果:

Spring Web Hello World

請注意,在給定的 URL 中,HelloWeb 是應用程式名稱,hello 是我們在控制器中使用 @RequestMapping("/hello") 提到的虛擬子資料夾。您可以使用直接根在使用 @RequestMapping("/") 對映 URL 時對映 URL。在這種情況下,您可以使用簡短 URL https://:8080/HelloWeb/ 訪問同一頁面,但建議在不同的資料夾下進行不同的功能。

spring_web_mvc_framework.htm
廣告