Spring如何处理表单提交

今天我们来讲一个最简单的表单提交处理的例子,通过提交一个表单给朋友打一声招呼!

看这边文章之前,你至少应该了解基于Spring的Web开发的基础知识,当然,你还是应该准备好开发环境:

  • IDE+Java环境(JDK 1.7或以上版本)
  • Maven 3.0+(Eclipse和Idea IntelliJ内置,如果使用IDE并且不使用命令行工具可以不安装)

准备POM文件

POM.xml

<?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.tianmaying</groupId>
 <artifactId>springboot-form-submission-demo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>springboot-form-submission-demo</name>
 <description>Springboot form submission demo</description>

 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>1.2.5.RELEASE</version>
 <relativePath/>
 </parent>

 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <java.version>1.8</java.version>
 </properties>

 <dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-thymeleaf</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 </dependencies>

 <build>
 <plugins>
 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin>
 </plugins>
 </build>

</project>

创建Controller

我们已经知道可以通过Controller来进行URL路由,Spring WebMvc框架会将Servlet容器里收到的HTTP请求根据路径分发给对应的@Controller类进行处理、而 @RequestMapping注解表明该方法处理那些URL对应的HTTP请求。

我们的SayHelloController的代码如下:

package com.tianmaying.springboot.formsubmission;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class SayHelloController {

 @RequestMapping(value="http://www.cppcns.com/sayhello", method=RequestMethod.GET)
 public String sayHelloForm(Model model) {
 model.addAttribute("helloMessage", new HelloMessage());
 return "sayhello";
 }

 @RequestMapping(value="http://www.cppcns.com/sayhello", method=RequestMethod.POST)
 public String sayHello(@ModelAttribute HelloMessage helloMessage, Model model) {
 model.addAttribute("helloMessage", helloMessage);
 return "message";
 }

}
  • 针对/sayhelloGET请求,我们返回提交表单的页面,即sayHello.html
  • 针对/sayhelloPOST请求,我们进行表单的处理,然后将打招呼的信息渲染到message.html页面返回。

表单处理也无外乎这两件事情:显示表单,处理表单提交。

显示表单

/sayhelloGET请求里,在渲染页面之前,我们通过model.addAttribute("helloMessage", new HelloMessage());告诉页面绑定到一个空的HelloMessage对象,这样sayHello.html页面初始时就会显示一个空白的表单。

HelloMessage

package com.tianmaying.springboot.formsubmission;

public class HelloMessage {

 private String name;
 private String message;

 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public String getMessage() {
 return message;
 }
 public void setMessage(String message) {
 this.message = message;
 }

}

仅仅扔一个空白对象给表单还不够,你还得告诉表单的各个输入如何绑定到对象的各个属性上。这个时候我们要用上Themeleaf了。

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
 <title>我们: Spring表单提交处理</title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
 <h1>表单处理演示</h1>
 <form action="#" th:action="@{/sayhello}" th:object="${helloMessage}" method="post">
 <p>friend: <input type="text" th:field="*{name}" /></p>
 <p>message: <input type="text" th:field="*{message}" /></p>
 <p><input type="submit" value="http://www.cppcns.com/ruanjian/java/Submit" /> <input type="reset" value="http://www.cppcns.com/ruanjian/java/Reset" /></p>
 </form>
</body>
</html>

Spring如何处理表单提交

扫一扫手机访问