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 物件中傳遞了一個空的 Student 物件,名稱為 "command",因為 spring 框架期望一個名為 "command" 的物件,如果您在 JSP 檔案中使用了 `<form:form>` 標籤。因此,當呼叫 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><form:label path = "id">id</form:label></td>
               <td><form:input path = "id" /></td>
            </tr>
            <tr>
               <td colspan = "2">
                  <input type = "submit" value = "Submit"/>
               </td>
            </tr>
         </table>  
      </form:form>
   </body>
</html>

這裡,我們使用 `<form:input />` 標籤來渲染一個 HTML 文字框。例如:

<form:input path = "name" />

它將渲染以下 HTML 內容。

<input id = "name" name = "name" type = "text" value = ""/>

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 應用程式一切正常,我們將看到以下螢幕。

Textbox Spring Student Form

提交所需資訊後,單擊提交按鈕提交表單。如果 Spring Web 應用程式一切正常,我們將看到以下螢幕。

Textbox Spring Student Form Result
廣告
© . All rights reserved.