我有一个简单的网页,想要添加JS。
呈现页面的控制器是 -
@Controller
@RequestMapping("/testui")
public class GreetingController {
@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "pages/greeting";
}
}
我的资源文件夹的结构是 -
src/main/resources
|
-- static
--templates
|
--css
--js
--pages
Thymeleaf页面是 -
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
Welcome to the future
</body>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
</html>
PS - 我也试过 -
<script type="text/javascript" th:src="@{js/jquery-1.10.2.min.js/}"></script>
但同样的问题
答案 0 :(得分:3)
问题是您的css
和js
文件夹所在的位置,这些文件夹应位于static
文件夹中。
src/main/resources
|
-- static
|
--css
--js
--templates
|
--pages
在您的*.html
页面中,您可以使用:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<!-- include css -->
<link rel="stylesheet" type="text/css" th:href="@{/css/style.css}"/>
</head>
<body>
Welcome to the future
</body>
<!-- include javascript -->
<script type="text/javascript" th:src="@{/js/jquery-1.10.2.min.js}"></script>
</html>
答案 1 :(得分:0)
使用以下
<script type="text/javascript" src="../js/jquery-1.10.2.min.js"></script>