springboot如何重定向外部網(wǎng)頁
springboot重定向外部網(wǎng)頁
package com.liangxs.web; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller//注意這里不能用@RestController,RestController由@Controller+ResponseBody組成,返回的是數(shù)據(jù)中支持跳轉(zhuǎn)視圖 @RequestMapping("/upload") public class TestController { @RequestMapping("/redirect") public String redirect(HttpServletResponse response) { return "redirect:http://www.baidu.com";//spring redirect方式 } @RequestMapping("/redirect1") public void redirect1(HttpServletResponse response) { try { response.sendRedirect("http://www.baidu.com");//HttpServletResponse方式 } catch (IOException e) { e.printStackTrace(); } } }
springboot頁面重定向問題
@GetMapping("/delemp/{id}") public String deleteEmp(@PathVariable("id")Integer id){ ? ? employeeDao.delete(id); ? ? return "redirect:/emps"; }
如上述代碼所示,接受前端請求后通返回"redirect:/emps"即可實(shí)現(xiàn)重定向到localhost:8080/emps請求中,此時(shí)不能寫成"redirect:emps"即最前端的斜杠不能省略,否則運(yùn)行時(shí)報(bào)錯(cuò)
Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type ‘java.lang.String’ to required type ‘java.lang.Integer’;
nested exception is java.lang.NumberFormatException: For input string: “emps”]。
在沒有@PathVariable的請求中可以寫成"redirect:emps"重定向返回(目前不知道報(bào)錯(cuò)和可以省略斜杠的原因)
如下代碼所示,但建議都寫成"redirect:/emps"。
@PostMapping("/updateEmp") public String updateEmp(Employee employee){ ? ? employeeDao.save(employee); ? ? return "redirect:emps"; }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- springBoot熱部署、請求轉(zhuǎn)發(fā)與重定向步驟詳解
- SpringBoot中處理的轉(zhuǎn)發(fā)與重定向方式
- springboot?實(shí)戰(zhàn):異常與重定向問題
- 使用springboot跳轉(zhuǎn)到指定頁面和(重定向,請求轉(zhuǎn)發(fā)的實(shí)例)
- 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)文章
Json字符串轉(zhuǎn)Java對象和List代碼實(shí)例
這篇文章主要介紹了Json字符串轉(zhuǎn)Java對象和List代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Java實(shí)現(xiàn)ATM機(jī)操作系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)ATM機(jī)操作系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05在springboot中如何使用filter設(shè)置要排除的URL
這篇文章主要介紹了在springboot中如何使用filter設(shè)置要排除的URL,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12SpringCloud:feign對象傳參和普通傳參及遇到的坑解決
這篇文章主要介紹了SpringCloud:feign對象傳參和普通傳參及遇到的坑解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03java原裝代碼完成pdf在線預(yù)覽和pdf打印及下載
本文主要介紹了java原裝代碼完成pdf在線預(yù)覽和pdf打印及下載的方法,具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02springboot中使用redis并且執(zhí)行調(diào)試lua腳本
今天有個(gè)項(xiàng)目需要使用redis,并且有使用腳本的需求,本文主要介紹了springboot中使用redis并且執(zhí)行調(diào)試lua腳本,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Java中MyBatis Plus知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理一篇關(guān)于Java中MyBatis Plus知識(shí)點(diǎn)總結(jié),需要的朋友們參考下。2019-10-10