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ù)中支持跳轉視圖 @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"即可實現(xiàn)重定向到localhost:8080/emps請求中,此時不能寫成"redirect:emps"即最前端的斜杠不能省略,否則運行時報錯
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"重定向返回(目前不知道報錯和可以省略斜杠的原因)
如下代碼所示,但建議都寫成"redirect:/emps"。
@PostMapping("/updateEmp") public String updateEmp(Employee employee){ ? ? employeeDao.save(employee); ? ? return "redirect:emps"; }
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- springBoot熱部署、請求轉發(fā)與重定向步驟詳解
- SpringBoot中處理的轉發(fā)與重定向方式
- springboot?實戰(zhàn):異常與重定向問題
- 使用springboot跳轉到指定頁面和(重定向,請求轉發(fā)的實例)
- springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes
- springboot 重定向方式(redirect前綴)
- springboot項目攔截器重定向循環(huán)問題的解決
- 基于springboot redirect重定向路徑問題總結
- springboot 如何重定向redirect 并隱藏參數(shù)
- Springboot轉發(fā)重定向實現(xiàn)方式解析
- SpringBoot后端服務重定向的實現(xiàn)示例
相關文章
在springboot中如何使用filter設置要排除的URL
這篇文章主要介紹了在springboot中如何使用filter設置要排除的URL,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12SpringCloud:feign對象傳參和普通傳參及遇到的坑解決
這篇文章主要介紹了SpringCloud:feign對象傳參和普通傳參及遇到的坑解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03springboot中使用redis并且執(zhí)行調試lua腳本
今天有個項目需要使用redis,并且有使用腳本的需求,本文主要介紹了springboot中使用redis并且執(zhí)行調試lua腳本,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04