Spring MVC-表單(Form)標籤-隱藏欄位(Hidden Field)示例(轉載實踐)

weixin_34162629發表於2017-09-05

以下內容翻譯自:https://www.tutorialspoint.com/springmvc/springmvc_hidden.htm

說明:示例基於Spring MVC 4.1.6

以下示例顯示如何使用Spring Web MVC框架在窗體中使用隱藏欄位。首先,讓我們使用Eclipse IDE,並按照以下步驟使用Spring Web Framework開發基於動態窗體的Web應用程式:

步驟描述
1 建立一個名為HelloWeb的專案,在一個包com.tutorialspoint下,如Spring MVC - Hello World Example章節所述。
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”,因為如果您在JSP中使用<form:form>標籤,Spring框架會期望一個名為“command”的物件檔案。所以當呼叫student()方法時,返回student.jsp檢視。

將在HelloWeb/addStudent URL 上針對POST方法呼叫第二個服務方法addStudent()。您將根據提交的資訊準備您的模型物件。最後,將從服務方法返回“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>

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

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

提交所需資訊後,點選提交按鈕提交表單。如果您的Spring Web應用程式的一切都很好,您應該會看到以下結果:

Maven示例:

https://github.com/easonjim/5_java_example/tree/master/springmvc/tutorialspoint/test13

相關文章