今天的積累都是爲了更好的明天,加油!我是java程序員,大家可以關注我一起學習哈!
打成war發佈tomcat我發現資源路徑有問題,有問題咱得解決問題啊!
訪問的是返現頁面引用的js全部陣亡,均報404,那一定是路徑有問題啊!
1.我引入的模板
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.修改application.properties文件 ; 添加: (sliderverificationcode,是對應war打包名)
server.port=8081
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
#開發時關閉緩存,不然沒法看到實時頁面
spring.thymeleaf.cache=false
server.servlet.context-path=/sliderverificationcode
3.html頁面,一定要注意引用js的路徑問題,我採用的是@{}
<!--用Thymeleaf 模板,引用文件的方法-->
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>滑動驗證碼</title>
<link rel="stylesheet" th:href="@{/css/slide.css}">
<script th:src="@{/js/jquery-1.11.1.min.js}"></script>
<script th:src="@{/js/jquery.lgyslide.js}"></script>
</head>
4.ajax請求後端接口,這裏注意url前面不要加/ ,因爲前置斜槓會覆蓋掉我們contextPath該路徑
$.ajax({
type: 'POST',
url: 'getPic',
dataType: 'json',
success: function (data) {
實際上他訪問的是http://localhost:8082/sliderverificationcode/getPic這個地址
注意
#如果Tomcat下部署多個springboot項目,要在 application.properties 添加 , 不然會報錯
spring.jmx.enabled=false
就此問題就解決了!
個人能力有限,如有問題 歡迎留言!