【Thymeleaf/jQuery】在Thymeleaf頁面中引入jQuery


本例工程下載:https://files.cnblogs.com/files/heyang78/myBank_themeleaf_jquery3.6.rar

 

第一步:在resources目錄下新建static目錄,再在static目錄里新建js目錄,然后把jquery-3.6.0.min.js放進去。注意兩層目錄一個文件不要放錯了。

如上圖所示,注意不要放錯了,否則找不到文件。

 

第二步:在頁面上引用jQuery

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Show all students</title>
<script  type="text/javascript" src="js/jquery-3.6.0.min.js"></script>
<script type="text/javascript">
    $(function(){
        alert(1);
        
        $("#showBtn").click(function(){
            alert(2);
        });
    });
</script>
</head>
<body>
    <h1>Show all students using jquery.</h1>
    <button id="showBtn">Show</button>
</body>
</html>

注意上面js/jquery-3.6.0.min.js之前時沒有static目錄的,這一點說明static目錄和templates目錄是Thymeleaf默認的目錄,不需要明寫出來。

 

第三步:在ActionCtroller里做個跳轉,以便轉到新頁面:

@Controller
public class ActionController {
    @Autowired
    private StudentMapper studentMapper;
......    
    @RequestMapping("/jqueryShowAll")
    public String showJqueryShowAllPage() {
        return "jqueryShowAll";
    }
......
}

 

最后,在地址欄輸入http://localhost:8080/jqueryShowAll,jQuery就起作用了。

以上四步就達成了目的,其實弄不出來主要是static目錄沒弄或是路徑里多寫了,注意調整就好。

-END-


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM