Thymeleaf 用法踩过的坑

一、在页面上插入背景图片路径的写法

<div class="ad" th:style="'background:url(' + @{/static/img/banner_sj.png} + ') no-repeat center; height:160px;'"></div>

二、Input框赋值写法

<input type="radio" name="checkbox" th:attr="data-labelauty=''+${obj.name}+''"  />

三、图片嵌套layui模板时 路径传参写法

<img th:src="@{'/proxyFile/file/download?path={{item.picUrl}}'}" class="hvr-bounce-in" />

四、href多个参数传参 写法(&需要替换成&amp;)

<td><a href="{{ctx}}f/guide/xyda?id={{item.id}}&amp;f={{item.id}}">跳转地址</a></td>

五、传值(多个参数)

第一种:地址之后拼接 

<td><a href="{{ctx}}f/guide/xyda?id={{item.id}}&amp;f={{item.id}}">跳转地址</a></td>

第二种:地址传参

前台写法:
<a th:href="@{'/f/guide/xyda/'+${xyy.id}}">跳转地址</a>

后台接收:
@RequestMapping("/xyda/{id}")
public String view(@PathVariable String id,Model model){
    return "url";
}

第三种:官网推荐写法

<img th:src="@{/proxyFile/file/download(path=${xyy.path},name=${xyy.name})}" />

六、css、js、jpg的引用

JS引用:
<script type="text/javascript" th:src="@{/static/js/jquery-1.11.0.min.js}"></script>
CSS引用:
<link rel="stylesheet" th:href="@{/static/css/jquery.sPage.css}"/>
图片引用:
<img th:src="@{/static/img/icon_lj.png}" />

 七、公共页面的引用与创建

公共页面位置

引用代码:

<th:block th:include="include :: top"/>

公共页面结构:

<!-- 通用CSS -->
<head th:fragment="header">
    <link rel="stylesheet" href="../static/css/hover-min.css" th:href="@{/static/css/hover-min.css}"/>
</head>
<div th:fragment="top">
    <div class="head">
        <ul>
            <div class="logo">
                头部公共内容
            </div>
        </ul>
    </div>
    <script>
        $(function () {
           $(".head li").each(function(){
             $(this).removeClass("cask");
           })
        })
    </script>
</div>
<div th:fragment="footer">
    <div class="foot">
        底部公共内容
    </div>
</div>

八、数据格式化问题,如果有小数显示小数,没有小数或者结尾为0(如:15.00)显示整数(显示为:15)

        第一种实现方式:

实现思路:thymeleaf框架调用Java实例方法

后台编写工具类:

/**
 * Copyright (DigitalChina) 2016-2020, DigitalChina.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.springboot.mybatis.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 处理积分
 */
public class FormatterNumber {

	public String valueFormat(double value) {
		DecimalFormat df = new DecimalFormat("#####.##");
		String xs = df.format(new BigDecimal(value));
		return xs;
	}
}

前台调用如下:

<span th:text="${new me.springboot.mybatis.utils.FormatterNumber().valueFormat(data.modelScore)}"></span>

        第二种实现方式:

实现思路:采用thymeleaf框架的${T(fullclasspath).function()}接口解决

       后台编写静态类

/**
 * Copyright (DigitalChina) 2016-2020, DigitalChina.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.springboot.mybatis.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 处理积分
 */
public class FormatterNumber {

	public static String valueFormat(double value) {
		DecimalFormat df = new DecimalFormat("#####.##");
		String xs = df.format(new BigDecimal(value));
		return xs;
	}
}

前台调用如下:

 <input type="text" th:value="${T(me.springboot.mybatis.utils.FormatUtil).valueFormat(data.modelScore)}" />

 

 

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值