SpringBoot实践之---处理前端表单提交

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

创建工程

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

<dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>


            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-thymeleafartifactId>
            dependency>

    dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

创建实体

代码清单如下:


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;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

创建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";
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

页面展示层

src/main/resources/templates/greeting.html


<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submissiontitle>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
head>
<body>
    <h1>Formh1>
    <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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

src/main/resources/templates/result.html


<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Handling Form Submissiontitle>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
head>
<body>
    <h1>Resulth1>
    <p th:text="'id: ' + ${greeting.id}" />
    <p th:text="'content: ' + ${greeting.content}" />
    <a href="/greeting">Submit another messagea>
body>
html>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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

点击submit:

参考资料

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

源码下载

https://github.com/forezp/SpringBootLearning

优秀文章推荐:

  • 更多springboot 教程:springBoot非官方教程 | 文章汇总
  • 更多springcoud 教程:史上最简单的 SpringCloud 教程 | 文章汇总

你可能感兴趣的:(Springboot)