1、請(qǐng)求轉(zhuǎn)發(fā)的路徑問(wèn)題
請(qǐng)求轉(zhuǎn)發(fā)是服務(wù)器內(nèi)部資源的一種跳轉(zhuǎn)方式,即當(dāng)瀏覽器發(fā)送請(qǐng)求訪問(wèn)服務(wù)器中
的某一個(gè)資源(A)時(shí),該資源將請(qǐng)求轉(zhuǎn)交給另外一個(gè)資源(B)進(jìn)行處理并且由資
源B做出響應(yīng)的過(guò)程,就叫做請(qǐng)求轉(zhuǎn)發(fā)。
請(qǐng)求轉(zhuǎn)發(fā)的特點(diǎn):
請(qǐng)求轉(zhuǎn)發(fā)實(shí)現(xiàn):
request.getRequestDispatcher(url地址/轉(zhuǎn)發(fā)到資源的地址).forward(req, res);
//從當(dāng)前Servlet轉(zhuǎn)發(fā)到 index.jsp(http://localhost/day10/index.jsp)
//request.getRequestDispatcher("/index.jsp").forward(request, response);
request.getRequestDispatcher("index.jsp").forward(request, response);
spring mvc的轉(zhuǎn)發(fā)
/* 測(cè)試請(qǐng)求轉(zhuǎn)發(fā)(forward) */
@RequestMapping("testForward")
public String testForward(){ System.out.println("測(cè)試請(qǐng)求轉(zhuǎn)發(fā)(forward)...");
return "forward:hello";
//return "forward:/hello";
}
可以看出轉(zhuǎn)發(fā)路徑前面加不加“/”都可以,這是因?yàn)槭欠?wù)器內(nèi)部資源的跳轉(zhuǎn),路徑是從后面拼接而來(lái)的。
2、重定向的路徑問(wèn)題
當(dāng)瀏覽器向服務(wù)器發(fā)請(qǐng)求訪問(wèn)某一個(gè)資源A,資源A在響應(yīng)時(shí)通知瀏覽器需要再
進(jìn)一步請(qǐng)求才能獲取到對(duì)應(yīng)的資源,瀏覽器再次發(fā)請(qǐng)求訪問(wèn)服務(wù)器中的資源B,最終
由資源B響應(yīng)瀏覽器要獲取的資源,這個(gè)過(guò)程叫做重定向。
重定向的特點(diǎn):
實(shí)現(xiàn)代碼:
response.sendRedirect(所重定向到資源的URL地址);
//測(cè)試1: 從當(dāng)前Servlet(day10/TestRedirect)重定向到day10/index.jsp
// http://localhost/day10/TestRedirect
// http://localhost/day10/index.jsp
response.sendRedirect( "http://localhost/day10/index.jsp" );
response.sendRedirect( "/day10/index.jsp" );
response.sendRedirect( "/index.jsp" ); //錯(cuò)誤路徑 localhost/index.jsp
response.sendRedirect( "index.jsp" ); //正確路徑
//測(cè)試2: 從當(dāng)前Servlet重定向到day09/index.jsp
response.sendRedirect( "http://localhost/day09/index.jsp" );
//測(cè)試3: 從當(dāng)前Servlet重定向到百度首頁(yè)
response.sendRedirect( "http://www.baidu.com" );
springboot實(shí)現(xiàn)重定向
/* 測(cè)試請(qǐng)求重定向(redirect) */
@RequestMapping("testRedirect")
public String testRedirect(){
System.out.println("測(cè)試請(qǐng)求重定向(redirect)...");
return "redirect:hello";
// return "redirect:/hello"; 可能報(bào)錯(cuò)
}
可以看出重定向路徑前面加“/”是可能會(huì)報(bào)錯(cuò)的,這是因?yàn)槭莾纱握?qǐng)求,可能是不同服務(wù)器之間的資源跳轉(zhuǎn),如果前面加上“/”,會(huì)從絕對(duì)目錄下拼接,就會(huì)出現(xiàn)下面的錯(cuò)誤
url:http://localhost:8080/DataShow/logout
@RequestMapping("/logout")
public String testRedirect(){
System.out.println("測(cè)試請(qǐng)求重定向(redirect)...");
return "redirect:/login";
redirect:/login,如果加了“/”
可以看出重定向的路徑是直接從絕對(duì)路徑(/localhost:8080/)后面拼接的,而不是從項(xiàng)目的根目錄(/localhost:8080/DataShow)后面拼接的。如果springboot配置文件是這樣,不會(huì)報(bào)錯(cuò),或者去掉login前面”/"或者改為“./”。
server:
port: 8080
servlet:
context-path: /
3、springboot的html引入js、css
先看看配置文件
# 端口配置
server:
port: 8080
servlet:
context-path: /DataShow
url:http://localhost:8080/DataShow/
引入js、css路徑
<link rel="stylesheet" type="text/css"
href="/static/bootstrap-3.3.4/css/bootstrap.css">
<script src="/static/hhassets/js/jquery.min.js"></script>
可以看出是以“/"開(kāi)始的,加載后,是直接從(localhost:8080/)后面拼接的,會(huì)報(bào)錯(cuò),我們需要從/**localhost:8080/DataShow/**后面拼接,給前面“/”加個(gè)點(diǎn)“./”或者去掉前面的“/”,就不會(huì)報(bào)錯(cuò)或者配置文件url改為“/”。
<link rel="stylesheet" type="text/css"
href="./static/bootstrap-3.3.4/css/bootstrap.css">
<script src="./static/hhassets/js/jquery.min.js"></script>
4、a標(biāo)簽路徑
先看配置文件
# 端口配置
server:
port: 8080
servlet:
context-path: /DataShow
url:http://localhost:8080/DataShow/
<a href="/index" class="a-access">
<button class="button type1">進(jìn)入后臺(tái)</button>
</a>
可以看出路徑前面有“/”,點(diǎn)擊進(jìn)入后臺(tái),url變?yōu)椋?strong>http://localhost:8080/index,可以看出是從localhost:8080后面拼接的,而我們需要給前面“/”加個(gè)點(diǎn)“./”或者去掉前面的“/”,就不會(huì)報(bào)錯(cuò)或者配置文件url改為“/”。
server:
port: 8080
servlet:
context-path: /
5、總結(jié)
./ 表示當(dāng)前目錄,可以省略
…/ 表示父級(jí)目錄。
…/…/ 表示上級(jí)目錄的父級(jí)目錄。
/ 表示 根目錄。
當(dāng)我們進(jìn)行重定向,引入js、css或者a標(biāo)簽等的路徑前面加上斜杠“/”的時(shí)候,是表示從根目錄(ip:端口)后面進(jìn)行拼接的,所以我們需要將項(xiàng)目訪問(wèn)路徑配置為/localhost:8080這種類(lèi)型?;蛘呶覀冃薷哪夸浡窂剑?/ …/ …/…/)來(lái)達(dá)到訪問(wèn)路徑:redirect:hello,(/localhost:8080/DataShow/static/hhassets/js/jquery.min.js)
如果controllerurl是:/localhost:8080/DataShow/index/
那么引入的js、css 就是前面兩個(gè)點(diǎn):…/static/…文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-616345.html
url組成:請(qǐng)求協(xié)議://ip(域名):端口/項(xiàng)目名(web應(yīng)用)/…
所以說(shuō),我們?nèi)绻丝诤竺娓隧?xiàng)目名,那些從根目錄(也就是“/”開(kāi)頭)下訪問(wèn)的url就是會(huì)報(bào)錯(cuò)的,因?yàn)橐浴?”開(kāi)頭拼接在端口后面。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-616345.html
到了這里,關(guān)于spring boot項(xiàng)目資源跳轉(zhuǎn),及引入js、css和a標(biāo)簽,ajax等的路徑問(wèn)題的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!