Spring MVC - 表單處理示例



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

步驟 描述
1 在 com.tutorialspoint 包下建立一個名為 HelloWeb 的專案,如 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”。這是因為 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>

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 檔案選項並將SpringWeb.war檔案儲存到 Tomcat 的 webapps 資料夾中。

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

Spring Student Form

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

Spring Student Form Result
廣告

© . All rights reserved.