thymeleaf遇到的问题01

一、springboot版本所带来的问题:

1、springboot为1.5.21时,引用thymeleaf作为前端页面模板,在Pom文件中需要加上依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.22</version>
</dependency>

因为thymeleaf模板是严格按html5规范执行,标签一定要有结束标签;加了nekohtml这个就不会那么严检查。在springboot2.0以上版本的thymeleaf没有这么严检查;

还有需要在配置文件中添加

spring.thymeleaf.cache=false;缓存问题,可以不需要添加
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode =LEGACYHTML5

2、在thymeleaf中写js需要注意的地方:

1)、在js中加载后台数组数据,会出现异常;需要在<script>标签内添加th:inline="none";

<script type="text/javascript" th:inline="none"></script>

2)、在js中获取上下文路径时,需要在<script>标签内添加th:inline="javascript";

下面是获取上下文的

<script type="text/javascript" th:inline="javascript">
/*<![CDATA[*/
var basePath = /*[[${#httpServletRequest.getScheme() + "://"
+ #httpServletRequest.getServerName() + ":"
+ #httpServletRequest.getServerPort()
+ #httpServletRequest.getContextPath()}]]*/;
</script>

注:如果页面都在一个<script>写,这时,又需要获取后台数组数据,又要获取上下路径,同时用到th:inline="none"和th:inline="javascript";没找到好的办法,分两个<script>写。

3、在标签体中引用上下文路径,不需要那么麻烦,一般都有th:src="@{/}";th:href="@{/}";在js中,一些ajax请求时,需要用到上下文路径,也就是上面的basePath;url:basePath+"/";就可以访问。

4、在请求静态页面,如下面一个请求:

//基本功能菜单加载
            $.ajax({
                url : basePath + '/json/menu.json',
                type : 'GET',
                dataType : 'text',
                success : function(data){
                    var zNodes = eval ("(" + data + ")");
                    $.fn.zTree.init($("#treeMenu"), setting, zNodes);
                },
                error : function(msg){
                    alert('菜单加载异常!');
                }
            });
            

若type:是post,不能加载成功,改为get请求,就能正常运行。menu.json是在静态资源static文件中。请求controller就不会涉及请求方式问题。

转载于:https://www.cnblogs.com/lazyli/p/10927970.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值