- Spring MVC 基礎
- Spring MVC - 首頁
- Spring MVC - 概述
- Spring MVC - 環境搭建
- Spring MVC - HelloWorld 示例
- Spring MVC - 表單處理
- Spring MVC - 表單處理
- Spring MVC - 頁面重定向
- Spring MVC - 靜態頁面
- Spring MVC - 表單標籤庫
- Spring MVC - 文字框
- Spring MVC - 密碼框
- Spring MVC - 文字域
- Spring MVC - 複選框
- Spring MVC - 多個複選框
- Spring MVC - 單選按鈕
- Spring MVC - 多個單選按鈕
- Spring MVC - 下拉列表
- Spring MVC - 列表框
- Spring MVC - 隱藏域
- Spring MVC - 錯誤處理
- Spring MVC - 檔案上傳
- Spring MVC - 處理器對映
- Bean 名稱 URL 處理器對映
- 控制器類名處理器對映
- 簡單 URL 處理器對映
- Spring MVC - 控制器
- Spring MVC - 多動作控制器
- 屬性方法名解析器
- 引數方法名解析器
- 可引數化檢視控制器
- Spring MVC - 檢視解析器
- 內部資源檢視解析器
- Spring MVC - Xml 檢視解析器
- 資源包檢視解析器
- 多個解析器對映
- Spring MVC - 整合
- Spring MVC - Hibernate 驗證器
- Spring MVC - 生成 RSS Feed
- Spring MVC - 生成 XML
- Spring MVC - 生成 JSON
- Spring MVC - 生成 Excel
- Spring MVC - 生成 PDF
- Spring MVC - 使用 log4j
- Spring 問題與解答
- Spring - 問題與解答
- Spring 有用資源
- Spring MVC - 快速指南
- Spring MVC - 有用資源
- Spring MVC - 討論
Spring MVC - HelloWorld 示例
以下示例演示瞭如何使用 Spring MVC 框架編寫簡單的基於 Web 的HelloWorld應用程式。首先,讓我們準備好一個可用的 Eclipse IDE,並按照後續步驟使用 Spring Web 框架開發一個動態 Web 應用程式。
| 步驟 | 描述 |
|---|---|
| 1 | 建立一個名為HelloWeb的動態 Web 專案,並在建立的專案中的 src 資料夾下建立一個 com.tutorialspoint 包。 |
| 2 | 將以下 Spring 及其他庫拖放到WebContent/WEB-INF/lib資料夾中。 |
| 3 | 在 com.tutorialspoint 包下建立一個名為HelloController的 Java 類。 |
| 4 | 在 WebContent/WEB-INF 資料夾下建立 Spring 配置檔案web.xml和HelloWeb-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";
}
}
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.tutorialspoint" />
<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>${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:https://:8080/HelloWeb/hello。如果 Spring Web 應用程式一切正常,我們將看到以下螢幕。
您應該注意,在給定的 URL 中,HelloWeb是應用程式名稱,hello 是虛擬子資料夾,我們在控制器中使用 @RequestMapping("/hello") 提到了它。您可以使用直接根目錄對映您的 URL,使用@RequestMapping("/"),在這種情況下,您可以使用簡短 URL https://:8080/HelloWeb/訪問同一頁面,但建議在不同的資料夾下具有不同的功能。