目錄結構:(直接将從layui官網下載下傳的檔案夾放入static中)

使用layui提供的表格demo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>table子產品快速使用</title>
<link rel="stylesheet" href="/layui/css/layui.css" media="all">
</head>
<body>
<table id="demo" lay-filter="test"></table>
<script src="/layui/layui.js"></script>
<script>
layui.use('table', function(){
var table = layui.table;
//第一個執行個體
table.render({
elem: '#demo'
,height: 312
,url: '/demo/table/user/' //資料接口
,page: true //開啟分頁
,cols: [[ //表頭
{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
,{field: 'username', title: '使用者名', width:80}
,{field: 'sex', title: '性别', width:80, sort: true}
,{field: 'city', title: '城市', width:80}
,{field: 'sign', title: '簽名', width: 177}
,{field: 'experience', title: '積分', width: 80, sort: true}
,{field: 'score', title: '評分', width: 80, sort: true}
,{field: 'classify', title: '職業', width: 80}
,{field: 'wealth', title: '财富', width: 135, sort: true}
]]
});
});
</script>
</body>
</html>
但是idea編譯器會對js的路徑警告
是以當時我将路徑改為了"/static/layui/layui.js",Spring Boot項目啟動後,發生錯誤:
解決方案:
該錯誤大部分的原因都是找不到相關的靜态資源導緻的。
不用将路徑改為"/static/layui/layui.js",保持demo中的代碼,src="/layui/layui.js"即可。
原因我猜測是我在application.yml中配置了
#配置可以直接通路的靜态檔案目錄
resource:
static-locations:classpath:/static/,calsspath:/templates/,classpath:/static/img/,classpath:/static/css/,classpath:/static/js/
如果在js的src路徑中加入/static/,會使無法在yml配置目錄中找到相應的檔案。
如果以上無法解決:
- 如果自己配置了SpringSecurity,檢視自己有沒有正确的解決無法通路靜态檔案的問題。
- https://blog.csdn.net/YiQieFuCong/article/details/85009401 ,該部落格有其他的解決方案,但是重寫addResourceHandlers方法慎用,可能導緻項目連其他的css檔案都無法通路。
- 重新開機idea是個神奇的解決方法,但是如果第一次重新開機沒解決就别再試了。