thymeleaf中前后端数据交互小结

1. 引入静态资源:th:href或th:scr+@{/从static目录开始}

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <link th:href="@{/iamges/favicon.ico}" type="image/x-icon"/>
    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet"/>
    <meta charset="UTF-8">
    <title>书籍管理</title>
</head>

2.前端将数据绑定到后端对象:*{对象属性},前端引用后端数据${对象属性}

<div class="form-group">
    <label for="book_name" class="col-sm-2 control-label">书名:</label>
    <div class="col-xs-4">
        <input type="text" class="form-control" id="book_name" 
               name="name" th:value="${book.name}" th:field="*{book.name}"/>
    </div>
</div>

3.后端将数据传入前端 ModelMap(由框架提供),前端使用${对象属性}

@RequestMapping(value = "/create",method = RequestMethod.GET)
public String createBookForm(ModelMap map){
    map.addAttribute("book",book);
    map.addAttribute("action","create");
    return BOOK_FORM;
}

4.表单提交的注意点。

  • action:表单中的内容提交给哪个页面进行处理,可能的取值:URL
  • input元素:输入框,由type决定类型。
  • 触发提交的动作:
    • HTML DOM submit() 方法。
    • type=submit
    • button
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值