使用springboot跳轉到指定頁面和(重定向,請求轉發(fā)的實例)
springboot跳轉到指定頁面
controller的寫法
必須是templates下面的頁面,不經(jīng)過配置,無法直接跳轉到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("進入controller層了!!!"+userName); model.addAttribute("name",userName); return "index";//跳轉到指定頁面 } }
springboot重定向和請求轉發(fā)
springboot重定向
方式一:使用 "redirect" 關鍵字(不是指java關鍵字),注意:類的注解不能使用@RestController,要用@Controller;因為@RestController內(nèi)含@ResponseBody,解析返回的是json串。不是跳轉頁面
@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的請求轉發(fā)
方式一:使用 "forword" 關鍵字(不是指java關鍵字),注意:類的注解不能使用@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轉發(fā)和重定向
springmvc重定向寫法
@RequestMapping(“/test1”) public String test1(){ return “index”; }
轉發(fā)是不需要手動加html的,此時springboot發(fā)現(xiàn)是轉發(fā),默認配置下他會自動去templates文件夾下找到對應的文件進行轉發(fā),如果return后寫的是index.html會報404。
重定向
@RequestMapping(“/test2”) public String test2(){ return “redirect:index1.html”; }
首先,添加redirect:這個毋庸置疑是mvc的語法問題,其次這里需要注意的是需要手動添加.html,否則會報404
轉發(fā)的特點
地址欄不發(fā)生變化,顯示的是上一個頁面的地址
請求次數(shù):只有1次請求
根目錄:http://localhost:8080/項目地址/,包含了項目的訪問地址
請求域中數(shù)據(jù)不會丟失
重定向的特點
地址欄:顯示新的地址
請求次數(shù):2次
根目錄:http://localhost:8080/ 沒有項目的名字
請求域中的數(shù)據(jù)會丟失,因為是2次請求
疑問
問:什么時候使用轉發(fā),什么時候使用重定向?
如果要保留請求域中的數(shù)據(jù),使用轉發(fā),否則使用重定向。
以后訪問數(shù)據(jù)庫,增刪改使用重定向,查詢使用轉發(fā)。
問:轉發(fā)或重定向后續(xù)的代碼是否還會運行?
無論轉發(fā)或重定向后續(xù)的代碼都會執(zhí)行
1、轉發(fā)使用的是getRequestDispatcher()方法;重定向使用的是sendRedirect();
2、轉發(fā):瀏覽器URL的地址欄不變。重定向:瀏覽器URL的地址欄改變;
3、轉發(fā)是服務器行為,重定向是客戶端行為;
4、轉發(fā)是瀏覽器只做了一次訪問請求。重定向是瀏覽器做了至少兩次的訪問請求;
5、轉發(fā)2次跳轉之間傳輸?shù)男畔⒉粫G失,重定向2次跳轉之間傳輸?shù)男畔G失(request范圍)。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- springBoot熱部署、請求轉發(fā)與重定向步驟詳解
- springboot如何重定向外部網(wǎng)頁
- SpringBoot中處理的轉發(fā)與重定向方式
- springboot?實戰(zhàn):異常與重定向問題
- springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes
- springboot 重定向方式(redirect前綴)
- springboot項目攔截器重定向循環(huán)問題的解決
- 基于springboot redirect重定向路徑問題總結
- springboot 如何重定向redirect 并隱藏參數(shù)
- Springboot轉發(fā)重定向實現(xiàn)方式解析
- SpringBoot后端服務重定向的實現(xiàn)示例
相關文章
SpringBoot生產(chǎn)環(huán)境打包如何去除無用依賴
這篇文章主要介紹了SpringBoot生產(chǎn)環(huán)境打包如何去除無用依賴問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09TraceIdPatternLogbackLayout日志攔截源碼解析
這篇文章主要為大家介紹了TraceIdPatternLogbackLayout日志攔截源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11詳解基于Spring Boot/Spring Session/Redis的分布式Session共享解決方案
本篇文章主要介紹了詳解基于Spring Boot/Spring Session/Redis的分布式Session共享解決方案 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06