一、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就不会涉及请求方式问题。

posted on 2019-05-26 22:13  lazyli  阅读(1004)  评论(0编辑  收藏  举报