springboot实现转发和重定向
1、转发
⽅式⼀:使⽤ "forword" 关键字(不是指java关键字),注意:类的注解不能使⽤@RestController 要⽤@Controller
1 2 3 4@RequestMapping(value="/test/test01/{name}", method = RequestMethod.GET)
public String test(@PathVariable String name) {
return"forword:/ceng/hello.html";
}
⽅式⼆:使⽤servlet 提供的API,注意:类的注解可以使⽤@RestController,也可以使⽤@Controller
1 2 3 4@RequestMapping(value="/test/test01/{name}", method = RequestMethod.GET)
public void test(@PathVariable String name, HttpServletRequest request, HttpServletResponse response) throws Exception {    RequestDispatcher("/ceng/hello.html").forward(request,respo
nse);
}
2、重定向
⽅式⼀:使⽤ "redirect" 关键字(不是指java关键字),注意:类的注解不能使⽤@RestController,要⽤@Controller
1 2 3 4@RequestMapping(value="/test/test01/{name}", method = RequestMethod.GET)
public String test(@PathVariable String name) {
springboot原理和机制return"redirect:/ceng/hello.html";
}
⽅式⼆:使⽤servlet 提供的API,注意:类的注解可以使⽤@RestController,也可以使⽤@Controller
1 2 3 4@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之前加上:ContextPath()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。