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

转载请标明出处:
原文首发于:https://www.fangzhipeng.com/springboot/2017/05/20/sb20-form.html
本文出自方志朋的博客

个人博客纯净版:https://www.fangzhipeng.com/springboot/2017/05/20/sb20-form.html
这篇文件主要介绍通过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:

SpringBoot非官方教程 | 第二十篇: 处理表单提交_第1张图片

点击submit:

SpringBoot非官方教程 | 第二十篇: 处理表单提交_第2张图片

参考资料

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

源码下载

https://github.com/forezp/SpringBootLearning

更多阅读

史上最简单的 SpringCloud 教程汇总

SpringBoot教程汇总

Java面试题系列汇总


扫码关注公众号有惊喜

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

你可能感兴趣的:(springboot,SpringBoot,非官方教程)