Springboot轉(zhuǎn)發(fā)重定向?qū)崿F(xiàn)方式解析
1、轉(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);}
2、重定向
方式一:使用 'redirect' 關(guān)鍵字(不是指java關(guān)鍵字),注意:類的注解不能使用@RestController,要用@Controller
@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');}
使用API進(jìn)行重定向時(shí),一般會在url之前加上:request.getContextPath()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python GUI庫圖形界面開發(fā)之PyQt5動態(tài)(可拖動控件大小)布局控件QSplitter詳細(xì)使用方法與實(shí)例2. ASP動態(tài)include文件3. 概述IE和SQL2k開發(fā)一個(gè)XML聊天程序4. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)5. js開發(fā)中的頁面、屏幕、瀏覽器的位置原理(高度寬度)說明講解(附圖)6. CSS清除浮動方法匯總7. 不要在HTML中濫用div8. vue跳轉(zhuǎn)頁面常用的幾種方法匯總9. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)10. XML入門的常見問題(三)
