SpringBoot非官方教程 | 第二十篇: 处理表单提交

xiaoxiao2021-02-27  368

转载请标明出处: http://blog.csdn.net/forezp/article/details/71023868 本文出自方志朋的博客

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

创建工程

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

org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-starter-thymeleaf

创建实体

代码清单如下:

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

Getting Started: Handling Form Submission Form Id: Message:

src/main/resources/templates/result.html

Getting Started: Handling Form Submission Result Submit another message

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

点击submit:

参考资料

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

源码下载

https://github.com/forezp/SpringBootLearning

优秀文章推荐:

更多springboot 教程:springBoot非官方教程 | 文章汇总更多springcoud 教程:史上最简单的 SpringCloud 教程 | 文章汇总
转载请注明原文地址: https://www.6miu.com/read-1516.html

最新回复(0)