springboot如何重定向外部网页
springboot重定向外部网页
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组成,返回的是数据中支持跳转视图
@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"即可实现重定向到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";
}以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
关于eclipse安装spring插件报错An error occurred while collecting item
这篇文章主要介绍了关于eclipse安装spring插件报错An error occurred while collecting items to be installed...解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-08-08
mybatis-plus调用update方法时,自动填充字段不生效问题及解决
这篇文章主要介绍了mybatis-plus调用update方法时,自动填充字段不生效问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-06-06
SpringBoot集成Spring security JWT实现接口权限认证
这篇文章主要介绍了SpringBoot集成Spring security JWT实现接口权限认证,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-04-04
Mybatis分页查询的实现(Rowbounds和PageHelper)
本文主要介绍了Mybatis分页查询的实现(Rowbounds和PageHelper),文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2022-01-01


最新评论