And*_*rey 8 javascript spring thymeleaf
例:
这有效
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta charset="UTF-8"/>
<title></title>
</head>
<body>
<button th:onclick="'javascript:sayHello(\'hello\')'">Say Hello</button>
</body>
<script>
function sayHello(text) {
alert(text);
}
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
但是,如果我将js移动到同一文件夹中的文件hello.js,则脚本无效.
我尝试嵌入这样的:
<script type="text/javascript" th:src="@{hello.js}"></script>
Run Code Online (Sandbox Code Playgroud)
像这样:
<script type="text/javascript" src="hello.js"></script>
Run Code Online (Sandbox Code Playgroud)
我做错了什么?
假设您使用的是Spring Boot的默认配置,src/main/resources/templates/
那么您应该将thymeleaf文件放入其中,以便将javascript文件放入:
/src/main/resources/static
Run Code Online (Sandbox Code Playgroud)
说明:您的构建工具(Maven或Gradle)将复制/src/main/resources/
应用程序类路径中的所有内容,并且如Spring Boot的文档/static
中所述,类路径中调用的目录中的所有内容都将作为静态内容提供.
这个目录也可以,但不鼓励:
/src/main/webapp/
Run Code Online (Sandbox Code Playgroud)
如果您的应用程序将打包为jar,请不要使用src/main/webapp目录.虽然这个目录是一个通用标准,但它只适用于war包装,如果你生成一个jar,它将被大多数构建工具默默忽略.
归档时间: |
|
查看次数: |
9073 次 |
最近记录: |