使用springboot跳轉(zhuǎn)到指定頁面和(重定向,請求轉(zhuǎn)發(fā)的實(shí)例)
springboot跳轉(zhuǎn)到指定頁面
controller的寫法
必須是templates下面的頁面,不經(jīng)過配置,無法直接跳轉(zhuǎn)到public,static,等目錄下的頁面
package com.ljf.spring.boot.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @ClassName: UserController * @Description: TODO * @Author: liujianfu * @Date: 2021/04/01 10:26:05 * @Version: V1.0 **/ @Controller public class UserController { @RequestMapping("/api/show") public String showName(String userName,Model model){ System.out.println("進(jìn)入controller層了!!!"+userName); model.addAttribute("name",userName); return "index";//跳轉(zhuǎn)到指定頁面 } }
springboot重定向和請求轉(zhuǎn)發(fā)
springboot重定向
方式一:使用 "redirect" 關(guān)鍵字(不是指java關(guān)鍵字),注意:類的注解不能使用@RestController,要用@Controller;因?yàn)锧RestController內(nèi)含@ResponseBody,解析返回的是json串。不是跳轉(zhuǎn)頁面
@RequestMapping(value="/test/test01/{name}" , method = RequestMethod.GET) public String test(@PathVariable String name) { return "redirect:/ceng/hello.html"; }
方式二:使用servlet 提供的API,注意:類的注解可以使用@RestController,也可以使用@Controller
@RequestMapping(value="/test/test01/{name}" , method = RequestMethod.GET) public void test(@PathVariable String name, HttpServletResponse response) throws IOException { response.sendRedirect("/ceng/hello.html"); }
springboot的請求轉(zhuǎn)發(fā)
方式一:使用 "forword" 關(guān)鍵字(不是指java關(guān)鍵字),注意:類的注解不能使用@RestController 要用@Controller
@RequestMapping(value="/test/test01/{name}" , method = RequestMethod.GET) public String test(@PathVariable String name) { return "forword:/ceng/hello.html"; }
方式二:使用servlet 提供的API,注意:類的注解可以使用@RestController,也可以使用@Controller
@RequestMapping(value="/test/test01/{name}" , method = RequestMethod.GET) public void test(@PathVariable String name, HttpServletRequest request, HttpServletResponse response) throws Exception { request.getRequestDispatcher("/ceng/hello.html").forward(request,response); }
springboot轉(zhuǎn)發(fā)和重定向
springmvc重定向?qū)懛?/h3>
@RequestMapping(“/test1”)
public String test1(){
return “index”;
}
@RequestMapping(“/test1”) public String test1(){ return “index”; }
轉(zhuǎn)發(fā)是不需要手動加html的,此時springboot發(fā)現(xiàn)是轉(zhuǎn)發(fā),默認(rèn)配置下他會自動去templates文件夾下找到對應(yīng)的文件進(jìn)行轉(zhuǎn)發(fā),如果return后寫的是index.html會報(bào)404。
重定向
@RequestMapping(“/test2”) public String test2(){ return “redirect:index1.html”; }
首先,添加redirect:這個毋庸置疑是mvc的語法問題,其次這里需要注意的是需要手動添加.html,否則會報(bào)404
轉(zhuǎn)發(fā)的特點(diǎn)
地址欄不發(fā)生變化,顯示的是上一個頁面的地址
請求次數(shù):只有1次請求
根目錄:http://localhost:8080/項(xiàng)目地址/,包含了項(xiàng)目的訪問地址
請求域中數(shù)據(jù)不會丟失
重定向的特點(diǎn)
地址欄:顯示新的地址
請求次數(shù):2次
根目錄:http://localhost:8080/ 沒有項(xiàng)目的名字
請求域中的數(shù)據(jù)會丟失,因?yàn)槭?次請求
疑問
問:什么時候使用轉(zhuǎn)發(fā),什么時候使用重定向?
如果要保留請求域中的數(shù)據(jù),使用轉(zhuǎn)發(fā),否則使用重定向。
以后訪問數(shù)據(jù)庫,增刪改使用重定向,查詢使用轉(zhuǎn)發(fā)。
問:轉(zhuǎn)發(fā)或重定向后續(xù)的代碼是否還會運(yùn)行?
無論轉(zhuǎn)發(fā)或重定向后續(xù)的代碼都會執(zhí)行
1、轉(zhuǎn)發(fā)使用的是getRequestDispatcher()方法;重定向使用的是sendRedirect();
2、轉(zhuǎn)發(fā):瀏覽器URL的地址欄不變。重定向:瀏覽器URL的地址欄改變;
3、轉(zhuǎn)發(fā)是服務(wù)器行為,重定向是客戶端行為;
4、轉(zhuǎn)發(fā)是瀏覽器只做了一次訪問請求。重定向是瀏覽器做了至少兩次的訪問請求;
5、轉(zhuǎn)發(fā)2次跳轉(zhuǎn)之間傳輸?shù)男畔⒉粫G失,重定向2次跳轉(zhuǎn)之間傳輸?shù)男畔G失(request范圍)。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- springBoot熱部署、請求轉(zhuǎn)發(fā)與重定向步驟詳解
- springboot如何重定向外部網(wǎng)頁
- SpringBoot中處理的轉(zhuǎn)發(fā)與重定向方式
- springboot?實(shí)戰(zhàn):異常與重定向問題
- springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes
- springboot 重定向方式(redirect前綴)
- springboot項(xiàng)目攔截器重定向循環(huán)問題的解決
- 基于springboot redirect重定向路徑問題總結(jié)
- springboot 如何重定向redirect 并隱藏參數(shù)
- Springboot轉(zhuǎn)發(fā)重定向?qū)崿F(xiàn)方式解析
- SpringBoot后端服務(wù)重定向的實(shí)現(xiàn)示例
相關(guān)文章
SpringBoot生產(chǎn)環(huán)境打包如何去除無用依賴
這篇文章主要介紹了SpringBoot生產(chǎn)環(huán)境打包如何去除無用依賴問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09TraceIdPatternLogbackLayout日志攔截源碼解析
這篇文章主要為大家介紹了TraceIdPatternLogbackLayout日志攔截源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Netty搭建WebSocket服務(wù)器實(shí)戰(zhàn)教程
這篇文章主要介紹了Netty搭建WebSocket服務(wù)器實(shí)戰(zhàn),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03詳解基于Spring Boot/Spring Session/Redis的分布式Session共享解決方案
本篇文章主要介紹了詳解基于Spring Boot/Spring Session/Redis的分布式Session共享解決方案 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06