2016-12-17 84 views
1

我正在使用Spring MVC,Thymeleaf并希望在我的html文档中导入一个JavaScript文件。虽然所有链接都设置正确,并且文件出现在浏览器的调试视图中,但它们不会加载。 存在这样应该叫一个datepicker的HTML以下JS,但它并没有显示出来:JavaScript文件不加载

<script th:inline="javascript"> 
/*<![CDATA[*/ 
    $(function() { 
    $("#datepicker").Datepicker(
    ); 
    }); 
/*]]>*/ 
</script> 

的文件都包含这样:

<script type="text/javascript" th:src="@{/resources/js/file.js}"></script> 

CSS文件的工作只是罚款只是类型改变了。

任何想法如何解决这个问题?

Shows error on Chrome debugging

现在我删除从导入所有没有必要的js文件,它显示了以下错误:

more precise error

这表明jQuery的1.12.4使用。

回答

0

当您使用Spring和Thymeleaf推荐以下项目结构:

src 
    |-main 
     |-resources 
      |-static 
      | |-css 
      |  |-bootstrap.min.css 
      |  |-application.css 
      | |-images 
      | |-js 
      |  |-jquery-3.1.1.js 
      |-templates 

然后包括在您的模板不同的资源将是这样的:

<head> 
    <link href="../static/css/bootstrap.min.css" rel="stylesheet" media="screen" 
      th:href="@{/css/bootstrap.css}"/> 
    <link href="../static/css/application.css" rel="stylesheet" media="screen" 
      th:href="@{/css/application.css}"/> 

    <script src="../static/js/jquery-3.1.1.js" 
      th:src="@{/js/jquery-3.1.1.js}"></script> 
</head> 
0

答案:

1)<th:block></th:block>不作任何放置脚本标签diffrence

2)增加的HTML标签

xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" 
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" 

没有什么区别

3)在身体底部的内容与将其放置在头部时的内容相同

4)我在这个项目中使用Bootstrap并在Bootstrap的div中使用了datepicker。把它放在身体其他地方,而不是在它工作的div内。

结论: 如果您正在使用Spring MVC和Thymeleaf并想包括JavaScript文件,你需要:

1)<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" >

2)<link th:href="@{/resources/.../file.css}" rel="stylesheet" type="text/css"/>

3)<script type="text/javascript" th:src="@{/resources/.../file.js}"></script>

4 )确保您的JS或CSS文件不被以下文件“覆盖”

感谢您的时间和帮助@Parth!