Spring+MyBatis开发 Day09

thymeleaf实现form表单提交

话不多说,上代码

<form action="/adminStudent/toStudentDataList?currentPage=1" method="get">
     <div class="col-md-3 data">
         <input type="text" class="form-control input-sm" placeholder="姓名" name="studentName">
             <select data-placeholder="" name="classId" class="form-control input-sm">
                   <option th:value="0" selected>班级</option>
                   <option th:each="classes:${classList}" th:value="${classes.id}"  th:text="${classes.name}"></option>
             </select>
      </div>
      <button type="submit" class="btn bg-maroon">搜索</button>
 </form>                     

注意:
每个input标签都要有name属性,form要有action和method。
当然,这里也可以使用button代替input作为提交的按钮:
button的type属性有两个值:button和submit。当不写type属性时,其type的默认值是submit,点击的话也会直接提交数据

我们也可以用下面的方式,用一个实体类来接受所有参数:

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

页面展示层

    <!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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值