Controller如何进行重定向
Spring MVC中进行重定向
本人知道的有两种方式
方法返回的URI(相对路径)中加上"redirect:"前缀,声明要重定向到该地址
使用HttpServletResponse对象进行重定向
注意
"redirect:"后面跟着的是"/"和不跟着"/"是不一样的:
1) "redirect:"后面跟着"/": 说明该URI是相对于项目的Context ROOT的相对路径
2) "redirect:"后面没有跟着"/": 说明该URI是相对于当前路径
具体看demo理解这两种方式的实现
RedirectURLController.java:
package edu.mvcdemo.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import edu.mvcdemo.utils.StringUtils;
@Controller
@Scope("singleton") //只实例化一个bean对象(即每次请求都使用同一个bean对象),默认是singleton
@RequestMapping("/redirect")
public class RedirectURLController {
private Logger logger = Logger.getLogger(RedirectURLController.class);
@RequestMapping(value="/demo1", method=RequestMethod.GET)
private String testRedirect1(){
//注意:"redirect:/hello/world" 和 "redirect:hello/world"这两种写法是不一样的!!
// 本案例中:
// "redirect:/hello/world" 重定向到的URL路径为:协议://服务器IP或服务器主机名:端口号/项目的Context ROOT/hello/world
// "redirect:hello/world" 重定向到的URL路径为:协议://服务器IP或服务器主机名:端口号/项目的Context ROOT/redirect/hello/world
return "redirect:/hello/world";
}
@RequestMapping(value="/demo2", method=RequestMethod.GET)
private void testRedirect2(HttpServletRequest request ,HttpServletResponse response){
String pathPrefix = StringUtils.getWebContextPath(request);
String redirectURL = pathPrefix + "/hello/world";
logger.info(redirectURL);
try {
response.sendRedirect(redirectURL);
} catch (IOException e) {
logger.error(StringUtils.getExceptionMessage(e));
}
}
}
StringUtils.java:
package edu.mvcdemo.utils;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
public class StringUtils {
public static String getExceptionMessage(Exception e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
return stringWriter.toString();
}
public static String getWebContextPath(HttpServletRequest request){
StringBuilder webContextPathBuilder = new StringBuilder();
webContextPathBuilder.append(request.getScheme())
.append("://")
.append(request.getServerName())
.append(":")
.append(request.getServerPort())
.append(request.getContextPath());
return webContextPathBuilder.toString();
}
}
效果:
页面输入 http://localhost:8080/MavenSpringMvcDemo/redirect/demo1 或 http://localhost:8080/MavenSpringMvcDemo/redirect/demo2 都会重定向到http://localhost:8080/MavenSpringMvcDemo/hello/world
controller请求转发,重定向
了解
转发(forward):浏览器地址不会改变,始终是同一个请求。
重定向(sendRedirect):浏览器地址会改变,是两个请求。
转发forward
有异常抛出就好了:
跳首页:浏览器的url地址不变.可能会找不到静态文件:
@GetMapping(value = "/index")
@ApiOperation("首页")
public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.getRequestDispatcher("/index.html").forward(request, response);
}
重定向redirect
controller中返回值为void
@GetMapping(value = "/index")
@ApiOperation("首页")
public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.sendRedirect("/index.html");
}
第三种方式:controller中返回值为ModelAndView
return new ModelAndView(“redirect:/toList”);
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。