springboot無法跳轉(zhuǎn)頁面的問題解決方案
首先我登錄頁面直接通過瀏覽器請(qǐng)求直接訪問的,項(xiàng)目結(jié)構(gòu)如圖所示

登錄頁面
<form action="index" id="frm">
<input type="text" name="dname">
<input type="text" name="loc">
<input type="button" value="提交" id="but" ></form>
<script src="js/jquery-1.12.2.js"></script>
<script>
$(function () {
$("#but").click(function(){
var data = $("#frm").serialize();
$.get("index",data);
})
})
</script>
點(diǎn)擊提交后,是一個(gè)ajax發(fā)送表單里面的數(shù)據(jù),請(qǐng)求地址為index,會(huì)去數(shù)據(jù)庫里面查詢是否有這個(gè)人(后端采用mybatis去數(shù)據(jù)庫查詢),根據(jù)返回的結(jié)果,跳到相應(yīng)的頁面去,我在controller里面寫的index請(qǐng)求的java代碼為:
// 登錄
@GetMapping("index")
public String addDept(Dept dept) {
log.info("dept===" + dept);
List<Dept> depts = deptService.selectDept(dept);
if (depts != null) {
return "index";
} else {
return "error";
}
}
意外的事情出現(xiàn)了,有查詢結(jié)果出來,而且也進(jìn)入了if判斷,但就是沒有跳轉(zhuǎn)頁面,這個(gè)問題困惑了許久,一直沒想到問題出現(xiàn)在哪里,百度了很多,其中百度給的結(jié)果有以下幾點(diǎn):
注解使用@Controller 而不是@RestController,因?yàn)槭褂聾RestController會(huì)返回“index”字符串
首先在pom文件中引入模板引擎jar包,即:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
在application.properties中配置模板引擎
spring.thymeleaf.prefix=classpath:/templates/
不加@responseBody注解,因?yàn)榧恿酥髸?huì)返回一個(gè)字符串的形式;
以上的這些坑,我都試了,最后還是沒有失敗,但是我直接在瀏覽器上輸入index請(qǐng)求,會(huì)跳轉(zhuǎn)到index.html的頁面上面去,我就很納悶了,還是不知道我的問題出現(xiàn)在哪里
我的index.html的頁面如下,用ajax請(qǐng)求,調(diào)用去數(shù)據(jù)庫查詢所有人的請(qǐng)求,代碼如下:
index頁面
<script src="../js/jquery-1.12.2.js"></script>
<script>
selectDept()
function selectDept() {
$.get("getDept",callSelectDept,"JSON")
function callSelectDept(data) {
var str=""
for (var i =0;i<data.length;i++){
str+=data[i].deptno+"---"+data[i].dname+"---"+data[i].loc+
"<a href=deleteDept?deptno='"+data[i].deptno+"'>刪除</a>"+
"<a href=updateDept?deptno='"+data[i].deptno+"'>修改</a>"
+"<br/>"
}
$("#queryDept").append(str)
}
}
當(dāng)通過瀏覽器訪問index.html后,會(huì)顯示出來數(shù)據(jù),這里是沒有問題的
后來過了一段時(shí)間吧,才想起來是不是ajax請(qǐng)求調(diào)用方法后,在java后端發(fā)送跳轉(zhuǎn)頁面請(qǐng)求后,不能跳轉(zhuǎn)頁面,因?yàn)閍jax默認(rèn)是異步請(qǐng)求嘛.代碼如下
$.ajax({
asyn:false,
url:"index",
type:"get",
data:data
})
后來將ajax請(qǐng)求改為同步之后,還是失敗,最后,將提交表單的方式改為summit,成功!!!
<form action="index" id="frm"> <input type="text" name="dname"> <input type="text" name="loc"> <input type="submit" value="提交" ></form>
總結(jié):ajax請(qǐng)求最好只用于發(fā)送數(shù)據(jù),和從后端拿數(shù)據(jù),不要做跳轉(zhuǎn)頁面的...如果一定要做頁面的跳轉(zhuǎn),可以約定后端放回的數(shù)據(jù)為1或0,當(dāng)返回的數(shù)據(jù)為1時(shí),用Windows.location.href="index.html" rel="external nofollow" rel="external nofollow" 來跳轉(zhuǎn)
具體代碼如下:
function callback(dat){
if (dat=1){
window.location.href="index.html" rel="external nofollow" rel="external nofollow"
}else {
alert("1")
}
否則就用submit提交,記住了,ajax用于發(fā)送請(qǐng)求到那個(gè)方法后,后端是跳轉(zhuǎn)不了頁面的,也不會(huì)報(bào)錯(cuò),因?yàn)閍jax用于默認(rèn)是異步請(qǐng)求,如果要跳就在前端跳轉(zhuǎn)頁面也是可以的
這個(gè)坑記錄下來,為后來的你們給與一些建議!!!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot?錯(cuò)誤頁面跳轉(zhuǎn)方式
- Springboot實(shí)現(xiàn)頁面間跳轉(zhuǎn)功能
- Springboot實(shí)現(xiàn)從controller中跳轉(zhuǎn)到指定前端頁面
- 使用springboot跳轉(zhuǎn)到指定頁面和(重定向,請(qǐng)求轉(zhuǎn)發(fā)的實(shí)例)
- springboot用controller跳轉(zhuǎn)html頁面的實(shí)現(xiàn)
- 詳解如何配置springboot跳轉(zhuǎn)html頁面
- SpringBoot 中html的頁面間跳轉(zhuǎn)問題小結(jié)
相關(guān)文章
Java用三元運(yùn)算符判斷奇數(shù)和偶數(shù)的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了Java用三元運(yùn)算符判斷奇數(shù)和偶數(shù)的簡(jiǎn)單實(shí)現(xiàn),需要的朋友可以參考下2014-02-02
Java Servlet線程中AsyncContext異步處理Http請(qǐng)求
這篇文章主要介紹了Java Servlet線程中AsyncContext異步處理Http請(qǐng)求及在業(yè)務(wù)中應(yīng)用,AsyncContext是Servlet 3.0使Servlet 線程不再需要一直阻塞,直到業(yè)務(wù)處理完畢才能再輸出響應(yīng),最后才結(jié)束該Servlet線程2023-03-03

