在JavaScript中使用Thymeleaf框架的语法获取后台数据

一、在springboot项目中,我们常结合thymeleaf模板来开发项目,在HTML标签中输出的语法不做过多介绍,网上一堆一堆的,那么怎么在js中获取到后台通过Model传递过来的数据呢,语法很简单,如以下代码:

/*questionInfo是通过后台传递过来的*/
var answerList = [[${questionInfo.answers}]]

这样我们就能在js中获取到后台传递过来的数据加以使用了,另外,记一下我常用的一些其他常用语法

二、格式化日期

<!-- 其中item.createTime是后台传递过来的数据 -->
<div th:text="${#dates.format(item.createTime, 'yyyy-MM-dd HH:mm:ss')}"></div>

三、获取多级对象值的时候报错,通过加判断后输出来解决,代码如下,注意代码中的“?”

<p th:text="${item?.user?.nickName}"></p>

四、在thymeleaf中分离页面公共部分,如导航栏分离,公共css、js的抽取等

先抽取<head></head>标签的公共部分,代码如下,需要主页,使用thymeleaf语法的页面都需要在<html></html>标签的属性中引入网址“http://www.thymeleaf.org”,如以下代码第二行

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="head(title,links,styles)">

    <meta charset="utf-8"/>
    <title th:replace="${title}">这里是标题</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="format-detection" content="telephone=no"/>
    <meta name="format-detection" content="address=no"/>

    <link rel="stylesheet" type="text/css" th:href="@{/static/home/plugs/layui/css/layui.css}"/>
    <link rel="stylesheet" type="text/css" th:href="@{/static/home/plugs/bootstrap/css/bootstrap.css}" />
    <script type="text/javascript" th:src="@{/static/home/js/jquery-3.3.1.min.js}"></script>
    <script type="text/javascript" th:src="@{/static/home/plugs/bootstrap/js/bootstrap.min.js}"></script>

    <!--/* 页面独有的js文件引入 */-->
    <th:block th:replace="${links}" />
    <!--/* 页面独有的css文件引入 */-->
    <th:block th:replace="${styles}" />
</head>
</html>

分离导航栏代码,就把导航栏代码复制出来,单独写一个页面,在外层添加一个html标签,如下

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <!--顶部导航栏-->
    <ul th:fragment="top-menu" id="app" class="layui-nav" lay-filter="" style="text-align: center;margin-bottom: 0;">
        <li class="layui-nav-item"><a th:href="@{/home/index/index}">首页</a></li>
        <li class="layui-nav-item">
            <a href="javascript:;">知名师资</a>
            <dl class="layui-nav-child">
                <dd><a href="">知名教师</a></dd>
                <dd><a href="">人气学霸</a></dd>
                <dd><a href="">教师认证</a></dd>
            </dl>
        </li>
        <li class="layui-nav-item"><a th:href="@{/home/index/question}">有问有答</a></li>
        <li class="layui-nav-item">
            <a href="javascript:;">资料专区</a>
            <dl class="layui-nav-child">
                <dd><a href="">题目资料</a></dd>
                <dd><a href="">复习资料</a></dd>
                <dd><a href="">杂项资料</a></dd>
            </dl>
        </li>
        <li class="layui-nav-item"><a href="">积分兑换</a></li>
        <li class="layui-nav-item"><a href="">意见反馈</a></li>
        <li class="layui-nav-item"><a th:href="@{/home/index/about}">联系我们</a></li>

        <li class="layui-nav-item" th:if="${session.user} == null"><a th:href="@{/home/index/login}">登录</a></li>
        <li class="layui-nav-item" th:if="${session.user} == null"><a th:href="@{/home/index/register}">注册</a></li>

        <li class="layui-nav-item" th:if="${session.user} != null">
            <a href="javascript:;"><img th:src="@{${session.user.userPhoto}==null?'/static/home/images/user-head/1.jpg':${session.user.userPhoto}}" class="layui-nav-img" th:text="${session.user.userName}"></a>
            <dl class="layui-nav-child">
                <dd><a href="javascript:;">修改信息</a></dd>
                <dd><a href="javascript:void(0);" id="JsSignOut">退了</a></dd>
            </dl>
        </li>
    </ul>
</html>

分离footer公共底部代码如下

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!-- 底部导航栏 start-->
<footer th:fragment="footer" class="container-fluid" style="margin-top:15px;">
    <div class="row text-center" style="background-color:#424242;color:#E0E0E0;padding:30px;">
        <span class="layui-breadcrumb" lay-separator="|">
          <a th:href="@{/home/index/index}">首页</a>
          <a href="">知名教师</a>
          <a th:href="@{/home/index/question}">有问有答</a>
          <a href="">资料专区</a>
          <a href="">积分兑换</a>
          <a href="">意见反馈</a>
          <a th:href="@{/home/index/about}">联系我们</a>
        </span>
    </div>
    <script th:src="@{/static/home/plugs/layui/layui.js}"></script>
    <script th:src="@{/static/home/js/public.js}"></script>
</footer>
<!-- 底部导航栏end -->

最后,在页面中引入抽取的公共代码

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<!-- 引入head部分代码 ,如果有自定义样式或者css文件或者js文件,在后面的参数中传入标签名称即可,如下示例,没有则不填写-->
<head th:replace="home/public/head::head(~{::title},~{::style},~{::link})">
    <title>解答详情-答题吧</title>
    <link rel="stylesheet" type="text/css" th:href="@{/static/home/plugs/viewer/viewer.css}"/>
    <style>
        .row {
            padding-top: 5px;
            padding-bottom: 10px;
        }

        img {
            width: 30%;
        }
        .img-thumbnail{
            height: 150px;
            width: auto;
        }

        p {
            line-height: 30px;
        }
    </style>
</head>
<body>

<!--引入顶部导航栏-->
<div th:replace="home/public/top-menu::top-menu"></div>

<div id="question_container">
    <!-- 这里是本页面的代码 -->
</div>

<!-- 引入抽取出来的footer部分-->
<footer th:replace="home/public/footer::footer"></footer>

<!-- 这里引入页面独有的js文件 -->
<script th:src="@{/static/home/plugs/vue/vue.js}"></script>
<script th:src="@{/static/home/plugs/viewer/viewer.js}"></script>
<script th:inline="javascript">
    /*这里些页面的js代码*/
</script>
</body>
</html>

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
JavaScript 文件使用 Thymeleaf 语法需要借助于服务器端的渲染,通过在服务器端解析模板并将变量的值渲染到模板,然后将渲染好的 HTML 页面返回给客户端。 具体实现方法有以下几种: 1. 将 JavaScript 文件的内容嵌入到 HTML 页面使用 Thymeleaf 语法引用模板的变量。例如: ``` <script th:inline="javascript"> var myVar = /*[[${myVar}]]*/ ''; // ... </script> ``` 此方法需要将 JavaScript 内容写在 HTML 页面,不太灵活。 2. 将 JavaScript 文件的内容存储在模板使用 Thymeleaf 语法引用模板的变量,然后在服务器端将模板渲染为 HTML 页面。例如: ``` // JS 模板文件 var myVar = /*[[${myVar}]]*/ ''; // 渲染模板并返回 HTML 页面 @GetMapping("/my-page") public String myPage(Model model) { model.addAttribute("myVar", "hello world"); return "my-page"; } ``` 此方法需要在服务器端进行模板渲染,适用于需要动态生成 HTML 页面的情况。 3. 将 JavaScript 文件的内容存储在独立的文件,在服务器端使用 Thymeleaf 语法生成动态的 JavaScript 文件。例如: ``` // JavaScript 文件模板 var myVar = /*[[${myVar}]]*/ ''; // 渲染 JavaScript 文件并返回 @GetMapping("/my-script.js") public ResponseEntity<String> myScript(Model model) { String content = // 读取 JavaScript 文件模板并使用 Thymeleaf 渲染 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JAVASCRIPT); return new ResponseEntity<>(content, headers, HttpStatus.OK); } ``` 此方法需要在服务器端生成动态的 JavaScript 文件,适用于需要动态生成 JavaScript 文件的情况。 无论采用哪种方法,在使用 Thymeleaf 语法时都需要确保在服务器端进行模板渲染,而不能直接在客户端访问 JavaScript 文件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值