0
我在Spring MVC學習數據綁定和我接近使用標籤形式來實現它。正如所描述的,爲了在Spring框架中實現數據綁定似乎是必要的。不需要Spring數據綁定標記形式嗎?
但我做了一個試驗與Spring啓動,我沒有使用表單標籤,甚至輸入JSP頁面,但只是一個外部HTML頁面。
所以,問題是,表單標籤是有用的,但不是必需的,或將其與春天開機只沒有必要?
下面的代碼。謝謝!
輸入HTML表格
<html>
<body>
<h3> Registration Form <h3>
<br/>
<form action="http://localhost:8080/register" method="post" >
<pre>
Name <input type="text" name="name" />
Email address <input type="text" name="emailAddress" />
Password <input type="password" name="password" />
<input type="submit" value="Submit" />
</pre>
</form>
</body>
</html>
彈簧控制器:
package hello;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RegistrationController {
@RequestMapping("/register")
public String greeting(User user, Model model) {
model.addAttribute("user", user);
return "result";
}
}
的pom.xml與Thymeleaf
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.bytecode</groupId>
<artifactId>SpringBootBindingForm</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
輸出頁(以驗證正確的數據綁定)
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Name, ' + ${user.name} + '!'" />
<p th:text="'Password, ' + ${user.password} + '!'" />
<p th:text="'Email, ' + ${user.emailAddress} + '!'" />
</body>
</html>
And User.java and Application.java我不認爲有必要在此處顯示。