- Spring MVC 基礎
- Spring MVC - 首頁
- Spring MVC - 概述
- Spring MVC - 環境搭建
- Spring MVC - Hello World 示例
- 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 - 隱藏欄位示例
以下示例描述瞭如何使用 Spring Web MVC 框架在表單中使用隱藏欄位。首先,假設我們已經準備好了一個可用的 Eclipse IDE,並考慮以下步驟來使用 Spring Web 框架開發一個基於動態表單的 Web 應用程式。
| 步驟 | 描述 |
|---|---|
| 1 | 建立一個名為 HelloWeb 的專案,位於 com.tutorialspoint 包下,如 Spring MVC - Hello World 章節中所述。 |
| 2 | 在 com.tutorialspoint 包下建立 Java 類 Student 和 StudentController。 |
| 3 | 在 jsp 子資料夾下建立檢視檔案 student.jsp 和 result.jsp。 |
| 4 | 最後一步是建立原始檔和配置檔案的內容,並匯出應用程式,如下所述。 |
Student.java
package com.tutorialspoint;
public class Student {
private Integer age;
private String name;
private Integer id;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
}
StudentController.java
package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.ui.ModelMap;
@Controller
public class StudentController {
@RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView student() {
return new ModelAndView("student", "command", new Student());
}
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public String addStudent(@ModelAttribute("SpringWeb")Student student,
ModelMap model) {
model.addAttribute("name", student.getName());
model.addAttribute("age", student.getAge());
model.addAttribute("id", student.getId());
return "result";
}
}
這裡,對於第一個服務方法 student(),我們在 ModelAndView 物件中傳遞了一個空的 Studentobject,其名稱為“command”,因為如果在 JSP 檔案中使用 <form:form> 標籤,Spring 框架期望有一個名為“command”的物件。因此,當呼叫 student() 方法時,它會返回 student.jsp 檢視。
第二個服務方法 addStudent() 將針對 HelloWeb/addStudent URL 上的 POST 方法被呼叫。您將根據提交的資訊準備模型物件。最後,服務方法將返回“result”檢視,這將導致渲染 result.jsp
student.jsp
<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
<head>
<title>Spring MVC Form Handling</title>
</head>
<body>
<h2>Student Information</h2>
<form:form method = "POST" action = "/HelloWeb/addStudent">
<table>
<tr>
<td><form:label path = "name">Name</form:label></td>
<td><form:input path = "name" /></td>
</tr>
<tr>
<td><form:label path = "age">Age</form:label></td>
<td><form:input path = "age" /></td>
</tr>
<tr>
<td>< </td>
<td><form:hidden path = "id" value = "1" /></td>
</tr>
<tr>
<td colspan = "2">
<input type = "submit" value = "Submit"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
在這裡,我們使用 <form:hidden /> 標籤來渲染 HTML 隱藏欄位。
例如 -
<form:hidden path = "id" value = "1"/>
它將渲染以下 HTML 內容。
<input id = "id" name = "id" type = "hidden" value = "1"/>
result.jsp
<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
<head>
<title>Spring MVC Form Handling</title>
</head>
<body>
<h2>Submitted Student Information</h2>
<table>
<tr>
<td>Name</td>
<td>${name}</td>
</tr>
<tr>
<td>Age</td>
<td>${age}</td>
</tr>
<tr>
<td>ID</td>
<td>${id}</td>
</tr>
</table>
</body>
</html>
完成原始檔和配置檔案的建立後,匯出您的應用程式。右鍵單擊您的應用程式並使用 匯出 → WAR 檔案 選項,並將 HelloWeb.war 檔案儲存到 Tomcat 的 webapps 資料夾中。
現在啟動您的 Tomcat 伺服器,並確保您可以使用標準瀏覽器從 webapps 資料夾訪問其他網頁。嘗試 URL – https://:8080/HelloWeb/student,如果 Spring Web 應用程式一切正常,我們將看到以下螢幕。
提交所需資訊後,單擊提交按鈕提交表單。如果 Spring Web 應用程式一切正常,我們將看到以下螢幕。
廣告