zoukankan      html  css  js  c++  java
  • 通过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:

  • 相关阅读:
    关于JDK中的设计模式
    关于Java中的构造方法和set方法()给属性赋值
    关于Object[]数组强转成Integer[]类型的数组.
    [ImportNew]Java中的Timer类和TimerTask类
    关于Linux最基本总结
    关于数组
    关于Linux系统和Windows系统中文件夹的命名规范
    关于数组集合之间的转换
    关于Java获取系统信息
    关于Windows常用命令
  • 原文地址:https://www.cnblogs.com/MaxElephant/p/10231976.html
Copyright © 2011-2022 走看看