Spring MVC中使用Controller如何进行重定向
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;
/**
* @编写人: yh.zeng
* @编写时间:2017-7-13 上午9:10:29
* @文件描述: Spring MVC重定向demo
*/
@Controller
@Scope("singleton") //只实例化一个bean对象(即每次请求都使用同一个bean对象),默认是singleton
@RequestMapping("/redirect")
public class RedirectURLController {
private Logger logger = Logger.getLogger(RedirectURLController.class);
/**
* 方式一:方法返回的URI(相对路径)中加上"redirect:"前缀,声明要重定向到该地址
* "redirect:"后面跟着的是"/"和不跟着"/"是不一样的:
* 1) "redirect:"后面跟着"/": 说明该URI是相对于项目的Context ROOT的相对路径
* 2) "redirect:"后面没有跟着"/": 说明该URI是相对于当前路径
* @return
*/
@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";
}
/**
* 方式二:使用HttpServletResponse对象进行重定向,HttpServletResponse对象通过方法入参传入
* @param request
* @param response
* @return
* @throws IOException
*/
@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;
/**
* @编写人: yh.zeng
* @编写时间:2017-7-9 下午2:56:21
* @文件描述: todo
*/
public class StringUtils {
/**
* 获取异常信息
*
* @param e
* @return
*/
public static String getExceptionMessage(Exception e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
return stringWriter.toString();
}
/**
* 返回web项目的context path,格式 为:协议://服务器IP或服务器主机名:端口号/项目的Context ROOT
* @param request
* @return
*/
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”);
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot3使用Jasypt加密数据库用户名、密码等敏感信息
使用Jasypt(Java Simplified Encryption)进行数据加密和解密主要涉及几个步骤,包括引入依赖、配置加密密码、加密敏感信息、将加密信息存储到配置文件中,以下是详细的使用说明,需要的朋友可以参考下2024-07-07
如何在Spring Boot微服务使用ValueOperations操作Redis集群String字符串
这篇文章主要介绍了在Spring Boot微服务使用ValueOperations操作Redis集群String字符串类型数据,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-06-06
spring @Validated 注解开发中使用group分组校验的实现
这篇文章主要介绍了spring @Validated 注解开发中使用group分组校验的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-05-05
SpringKafka消息发布之KafkaTemplate与事务支持功能
通过本文介绍的基本用法、序列化选项、事务支持、错误处理和性能优化技术,开发者可以构建高效可靠的Kafka消息发布系统,事务支持特性尤为重要,它确保了在分布式环境中的数据一致性,感兴趣的朋友一起看看吧2025-04-04
web.xml SpringBoot打包可执行Jar运行SpringMVC加载流程
这篇文章主要为大家介绍了web.xml SpringBoot打包可执行Jar运行SpringMVC加载流程示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-04-04
Mybatis mysql模糊查询方式(CONCAT多个字段)及bug
这篇文章主要介绍了Mybatis mysql模糊查询方式(CONCAT多个字段)及bug,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-01-01


最新评论