通过springboot 去创建和提交一个表单(七)
创建工程
涉及了 web,加上spring-boot-starter-web和spring-boot-starter-thymeleaf的起步依赖。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<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> |
创建实体
代码清单如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<!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:
通过springboot 去创建和提交一个表单(七)的更多相关文章
- Spring Boot教程(七)通过springboot 去创建和提交一个表单
创建工程 涉及了 web,加上spring-boot-starter-web和spring-boot-starter-thymeleaf的起步依赖. <dependencies> < ...
- 开发日志系列:一个表单页面的呈现与提交(一)——JSON的操作
JSON操作 引子 最近在做一个表单页面,大概是这个样子的 这里打算用一个JSON存储所有的信息,我们可以理解为,所有东西都存在一个字符串里面.方便,快捷,易读,数据库操作也方便了.甚至,可以将很多不 ...
- Js定义一个表单并提交
Js定义一个表单 var form = $("<form>"); //定义一个form表单 form.attr('style', 'display:none'); // ...
- mysql根据逗号分割的字符串去关联查询另外一个表的数据
1.说明 在做显示数据的时候,一个字段会存那种逗号分割的字符串,那如何去根据逗号分割字符串去查询另一个表的数据呢? 首先我们查看一下需要显示的数据 select * from company wher ...
- ajax post提交form表单 报400错误 解决方法
昨天晚上做项目遇到了一个奇怪的问题,我用ajax提交一个form表单,后台Java方法用的是一个实体接,但是他根本不进方法体中,直接给我一个400的错误,一开始我以为是我路径的问题(尴尬),结果直接访 ...
- Jquery通过Ajax方式来提交Form表单
今天刚好看到Jquery的ajax提交数据到服务器的方法,原文是: 保存数据到服务器,成功时显示信息. jQuery 代码: $.ajax({ type: "POST", url: ...
- 使用ajax提交form表单,包括ajax文件上传
前言 使用ajax请求数据,很多人都会,比如说: $.post(path,{data:data},function(data){ ... },"json"); 又或者是这样的aja ...
- 使用ajax提交form表单(转)
前言 使用ajax请求数据,很多人都会,比如说: $.post(path,{data:data},function(data){ ... },"json"); 又或者是这样的aja ...
- Java 创建、填充PDF表单域
表单域,可以按用途分为多种不同的类型,常见的有文本框.多行文本框.密码框.隐藏域.复选框.单选框和下拉选择框等,目的是用于采集用户的输入或选择的数据.下面的示例中,将分享通过Java编程在PDF中添加 ...
随机推荐
- Webpack+React项目入门——入门及配置Webpack
一.入门Webpack 参考文章:<入门Webpack,看这篇就够了> 耐心看完这篇非常有帮助 二.React+Webpack环境配置 参考文章:<webpack+react项目初体 ...
- C# Combobox联动
接上一篇博文,对界面做一个小修改,做4个combobox,形成窗口之间的联动: 界面如下: 选择combobox里的条件,单击查询获取数据 首先连接数据库,获取到数据到第一个combobox里,代码在 ...
- 3.2 git命令大全
1. 常用命令 -- 查看 git remote:要查看当前配置有哪些远程仓库; git remote -v: -v 参数,你还可以看到每个别名的实际链接地址; git branch -a :查看远程 ...
- 学习笔记48—PS画虚线
PS画虚线步骤: 1)我们打开PS后,随便新建一个文件. 2)大小随意. 2)我们选择画笔工具,快捷键是B. 3)画笔面板的快捷键是F5. 4)点击画笔预设面板,再点击右边右上角的下三角,在弹出的菜单 ...
- ubuntu 配置sublime text3
1安装st3(http://blog.csdn.net/zhaoyu106/article/details/52858962) sudo apt-get update sudo apt-get ins ...
- centos 7 安装jdk1.8
首先下载jdk1.8 去官网下载jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151. ...
- 00-python-内置函数笔记
01.enumerate()函数用于将一个可遍历的数据对象(如 列表.元组或字符串)组合为一个索引序列,同时列出数据和数据包下标,一般用在for循环中 for i, element in enumer ...
- Django - Python3 常用命令
1.创建Django 项目 执行命令 django-admin.py startproject project_name 2.创建app 执行命令 注意:要先进入项目目录下,cd project_na ...
- Asp.net core 学习笔记 (Excel 读写)
EPPlus 已经支持 .net core 了 https://www.nuget.org/packages/EPPlus https://github.com/JanKallman/EPPlus 写 ...
- 带参数EXE
有时候我们需要让软件带参数运行,使用参数控制软件的部分行为, C#默认窗口应用是不带参数的,不过在Main函数的参数手动加上就可以得到参数了. 举例如下: /// <summary> // ...