Springboot轉(zhuǎn)發(fā)重定向?qū)崿F(xiàn)方式解析
1、轉(zhuǎn)發(fā)
方式一:使用 'forword' 關(guān)鍵字(不是指java關(guān)鍵字),注意:類(lèi)的注解不能使用@RestController 要用@Controller
@RequestMapping(value='/test/test01/{name}' , method = RequestMethod.GET)public String test(@PathVariable String name) { return 'forword:/ceng/hello.html';}
方式二:使用servlet 提供的API,注意:類(lèi)的注解可以使用@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)鍵字),注意:類(lèi)的注解不能使用@RestController,要用@Controller
@RequestMapping(value='/test/test01/{name}' , method = RequestMethod.GET)public String test(@PathVariable String name) { return 'redirect:/ceng/hello.html';}
方式二:使用servlet 提供的API,注意:類(lèi)的注解可以使用@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í),一般會(huì)在url之前加上:request.getContextPath()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python使用oslo.vmware管理ESXI虛擬機(jī)的示例參考2. Jsp servlet驗(yàn)證碼工具類(lèi)分享3. .NET6打包部署到Windows Service的全過(guò)程4. python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5滑塊條控件QSlider詳細(xì)使用方法與實(shí)例5. IntelliJ IDEA 好用插件之a(chǎn)nalyze inspect code詳解6. JetBrains IntelliJ IDEA 配置優(yōu)化技巧7. idea2020.1無(wú)法自動(dòng)加載maven依賴(lài)的jar包問(wèn)題及解決方法8. PHP程序員簡(jiǎn)單的開(kāi)展服務(wù)治理架構(gòu)操作詳解(一)9. vue項(xiàng)目中使用bpmn為節(jié)點(diǎn)添加顏色的方法10. 解決idea update project 更新選項(xiàng)消失的問(wèn)題
