转载请标明出处:

原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot-form/

本文出自方志朋的博客

这篇文件主要介绍通过springboot 去创建和提交一个表单。

创建工程

涉及了 web,加上spring-boot-starter-web和spring-boot-starter-thymeleaf的起步依赖。

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

创建实体

代码清单如下:


public class Greeting { private long id;
private String content; public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} }

创建Controller

@Controller
public class GreetingController { @GetMapping("/greeting")
public String greetingForm(Model model) {
model.addAttribute("greeting", new Greeting());
return "greeting";
} @PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute Greeting greeting) {
return "result";
} }

页面展示层

src/main/resources/templates/greeting.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Form</h1>
<form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
<p>Id: <input type="text" th:field="*{id}" /></p>
<p>Message: <input type="text" th:field="*{content}" /></p>
<p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>
</body>
</html>

src/main/resources/templates/result.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Result</h1>
<p th:text="'id: ' + ${greeting.id}" />
<p th:text="'content: ' + ${greeting.content}" />
<a href="/greeting">Submit another message</a>
</body>
</html>

启动工程,访问ttp://localhost:8080/greeting:

点击submit:

参考资料

https://spring.io/guides/gs/handling-form-submission/

源码下载

https://github.com/forezp/SpringBootLearning




扫码关注公众号有惊喜

(转载本站文章请注明作者和出处 方志朋的博客

SpringBoot非官方教程 | 第二十篇: 处理表单提交的更多相关文章

  1. (转) SpringBoot非官方教程 | 第二十四篇: springboot整合docker

    这篇文篇介绍,怎么为 springboot程序构建一个Docker镜像.docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.Docker 可以让开发者打包他们的 ...

  2. SpringBoot非官方教程 | 第二十六篇: sprinboot整合elk,搭建实时日志平台

    转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/sprinboot25-elk/ 本文出自方志朋的博客 这篇文章主要介绍 ...

  3. SpringBoot非官方教程 | 第二十四篇: springboot整合docker

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot24-docker/ 本文出自方志朋的博客 这篇文 ...

  4. SpringBoot非官方教程 | 第二十二篇: 创建含有多module的springboot工程

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springbot22-modules/ 本文出自方志朋的博客 这篇文 ...

  5. SpringBoot非官方教程 | 第二十三篇: 异步方法

    转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/springboot-ansy/ 本文出自方志朋的博客 这篇文章主要介绍 ...

  6. SpringBoot非官方教程 | 第二十五篇:2小时学会springboot

    转载请标明出处: http://blog.csdn.net/forezp/article/details/61472783 本文出自方志朋的博客 一.什么是spring boot Takes an o ...

  7. SpringBoot非官方教程 | 第十篇: 用spring Restdocs创建API文档

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot10-springrestdocs/ 本文出自方志 ...

  8. SpringBoot非官方教程 | 第二十一篇: springboot集成JMS

    转载请标明出处: http://blog.csdn.net/forezp/article/details/71024024 本文出自方志朋的博客 springboot对JMS提供了很好的支持,对其做了 ...

  9. (转) SpringBoot非官方教程 | 第二篇:Spring Boot配置文件详解

    springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优先于配置的惯例,旨在让您尽快启动和运行.在一般情况下,我们不需要做太多的配置就能够让spring boot正 ...

随机推荐

  1. 白话SpringCloud | 第七章:分布式配置中心的使用

    前言 介绍完服务的容错保护处理,接下来我们来了解下关于分布式配置中心的相关知识和使用.众所周知,随着项目的越来越多,日益庞大,每个子项目都会伴随着不同的配置项,于此也就多了很多的配置文件.倘若某些配置 ...

  2. Cannot initialize Cluster. Please check your configuration for mapreduce.framework.name

    添加一下依赖 <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop- ...

  3. 合约实战,代币合约,DAPP开发

    1. ERC20标准 https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md pragma solidity ^; //定义接口 con ...

  4. js之cookie操作

    1.使用document.cookie保存信息 document.cookie = "Test=cooo";alert(document.cookie); //hours为空字符串 ...

  5. Etcd入门教程

    etcd是一个类似于zk的工具,用于保存值,节点-值这种映射关系的.节点组织结构类似unix文件系统结构,从/最开始.比如一个/test/name节点,值为guanxianseng.可以通过etcdc ...

  6. spring cloud provider报“Error parsing HTTP request header”,feign端报“Read timed out“

    这两天在调试spring cloud feign+hystrix报了如下错误: spring cloud provider报“Error parsing HTTP request header”,fe ...

  7. Cocos2d-js 开发记录:图片数据资源等的异步加载

    这里说的是在需要的使用加载图片,比如游戏中的某个关卡的图片,不用在游戏一开始就加载(万一用户玩不到那关,岂不是很冤,流量费了那么多),否则载入速度也慢.这种方式加载资源要用到cc.loader官方文档 ...

  8. oracle学习篇十一:视图

    视图是存储的查询定义. 1. 创建视图的语法如下: Create [OR REPLACE] [FORCE | NOFORCE] VIEW view_name[(alias,alias,...)] AS ...

  9. JavaScript中文拼音排序函数

    要对很多设备根据名称排序,找了找没有找到特别适合的,然后就自己写了一个根据中文拼音首字母排序的方法. github: https://github.com/haboll/sort.git

  10. matlab练习程序(生成黑白网格)

    提供了两种生成方法,一个是自己编程实现,比较灵活:另一个是调用系统的checkerboard函数,似乎只能生成8*8网格. 至于用途,也许可以用来下国际象棋. 自己函数生成: 系统函数生成: 代码如下 ...