Springboot转发重定向实现方式解析
1、转发
方式一:使用 "forword" 关键字(不是指java关键字),注意:类的注解不能使用@RestController 要用@Controller
@RequestMapping(value="/test/test01/{name}" , method = RequestMethod.GET) public String test(@PathVariable String name) { return "forword:/ceng/hello.html"; }
方式二:使用servlet 提供的API,注意:类的注解可以使用@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" 关键字(不是指java关键字),注意:类的注解不能使用@RestController,要用@Controller
@RequestMapping(value="/test/test01/{name}" , method = RequestMethod.GET) public String test(@PathVariable String name) { return "redirect:/ceng/hello.html"; }
方式二:使用servlet 提供的API,注意:类的注解可以使用@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进行重定向时,一般会在url之前加上:request.getContextPath()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
springBoot解决static和@Component遇到的bug
这篇文章主要介绍了springBoot解决static和@Component遇到的bug,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-02-02Java生成随机数之Random与ThreadLocalRandom性能比较详解
大家项目中如果有生成随机数的需求,我想大多都会选择使用Random来实现,它内部使用了CAS来实现。 实际上,JDK1.7之后,提供了另外一个生成随机数的类ThreadLocalRandom,那么他们二者之间的性能是怎么样的呢?本文就来详细说说2022-12-12校验非空的注解@NotNull如何取得自定义的message
这篇文章主要介绍了校验非空的注解@NotNull如何取得自定义的message,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-09-09玩转spring boot 结合AngularJs和JDBC(4)
玩转spring boot,这篇文章主要介绍了结合AngularJs和JDBC,玩转spring boot,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-01-01
最新评论