SpringCloud商城day08 商品详情页-2021-10-14

一. Thymeleaf入门

1. thymeleaf: 页面静态化技术 -> 提前生成静态资源 -> 用户访问时加载到浏览器-> 提高访问效率减少占用资源

Thymeleaf is a modern server-side Java template engine for both web and standalone environments. 

2. SpringMVC整合thymeleaf模块 -> 六种模板

3. 创建springboot工程 -> 依赖starter-web/thymeleaf -> 启动类ThymeleafApplication.java

(1) yml配置: spring.thymeleaf.cache: false //页面加载时关闭缓存
(2) 新建页面 resourece/templates/*.html 

 4. thymeleaf语法:

(1) <html xmlns:th="http://www.thymeleaf.org">
    //表单提交
    <p th:text="${hello}"></p>
    <form th:action="@{/demo/test}">
        <input th:type="text" th:name="id">  //输入框属性名
        <button>提交</button>
    </form>
(2) 遍历对象: 
    <tr th:each="user, userStat:${userList}" >
        <td>
            <span th:text="${userStat.index}"></span>  // 0 1 2
        </td>
        <td th:text="${user.id}"></td>  // 1 2 3
    </tr>
(3) Map输出:
    <div th:each="map, mapStat:${dataMap}">
        <div th:text="${map}"></div>
        <span th:text="${mapStat.current.key}"></span>   //取键
        <span th:text="${mapStat.current.value}"></span> //取值
    </div>
(4) 数组输出:
    <div th:each="array, arrayStat:${names}">
        <span th:text="{arrayStat.count}"></span> //count属性 从1开始
    </div>
(5) 日期输出:
    <div th:text="${#dates.format(now, 'yyyy-MM-dd HH:mm:ss')}">
      
    </div>
(6) 条件判断:
    <div>
        <span th:if="${(age>=18)}">符合条件则输出</span>
    </div>
(7) 创建页面模块footer.html
 -> 引入命名空间 <html xmlns:th="http://www.thymeleaf.org">
 -> fragment模块:  <div id="C" th:fragment="copy"></div>
 -> demo.html引入模块: <div id="F" th:include="footer::copy"></div>

(8) th:utext="直接解析自带html标签"
      

二. 搜索页面渲染

1. 需求: 显示 用户勾选的搜索条件+商品属性规格+商品搜索结果集合

2. 实现: 客户端搜索 -> 搜索微服务查询索引 -> thymeleaf渲染静态化页面 -> 返回页面;

3. 步骤:

1. search微服务
(1) 添加thymeleaf依赖: spring-boot-starter-thymeleaf
(2) yml配置文件: thymeleaf.cache: false
(3) 添加页面静态化资源: 
    1) resources/static/js/img/css
    2) resources/templates/html
(4) 更新SearchController.java
    1)  类注解:@Controller   //不用@RestController, 新增方法返回值不是JSON
    2) 添加方法
    @GetMapping()
    public String list(){
        //特殊符号处理
        this. handleSearchMap(searchMap);
        //获取查询结果
        Map resultMap = searchService.search(searchMap);
        //携带数据
        model.addAttribute("searchMap", searchMap);
        model.addAttributes("resultMap", resultMap);
        //跳转搜索页面
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值