@Controller
public class TestController {
…
}
1、使用ModelAndView
@RequestMapping("redirect")
public ModelAndView redirect(){
return new ModelAndView("redirect:http://www.baidu.com");
}
2、使用SpringMVC
@RequestMapping("redirect2")
public String redirect(){
return"redirect:http://www.baidu.com";
}
3、使用HttpServletResponse
@RequestMapping("/send")
public void sendInfoToWeiXin(HttpServletResponse response){
try {
response.sendRedirect("http://www.baidu.com");
} catch (IOException e) {
e.printStackTrace();
}
}
本文介绍了三种实现页面重定向的方法:使用ModelAndView、使用Spring MVC的简单字符串返回方式及利用HttpServletResponse进行重定向。这些方法适用于不同的场景需求,帮助开发者灵活选择。
1万+

被折叠的 条评论
为什么被折叠?



