Spring MVC中怎么使用Controller进行重定向
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章主要介绍Spring MVC中怎么使用Controller进行重定向,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!有两种方式方法返回的URI(相对路径)中加上"redi
千家信息网最后更新 2025年11月07日Spring MVC中怎么使用Controller进行重定向
这篇文章主要介绍Spring MVC中怎么使用Controller进行重定向,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
有两种方式
方法返回的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");以上是"Spring MVC中怎么使用Controller进行重定向"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
跟着
路径
服务器
项目
服务
对象
方式
主机
口号
地址
文件
方法
浏览器
首页
浏览
内容
前缀
时间
篇文章
中加
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全保护员
招标网络技术方案怎么写
与服务器修改管理员信息
达梦数据库怎么改用户名
万友引利(广州)网络技术
服务器cpu价格
数据库怎么看是否可为空
网络技术对督促程序的作用
汪德嘉网络安全大会
网络安全测试题免费下载
本地服务器安全
虹口区网络技术开发哪家便宜
深圳因味网络技术有限责任公司
杭州九旺网络技术招聘
合肥电商平台软件开发公司
广州网络安全工程师薪资
深圳市天瑞网络技术
网络安全专业能干到多少岁
嘻嘻网络技术服务
海创网络技术支持
怎么把数据库数据拼上负号
国内软件开发转对日软件开发
sql数据库字段设计
广州麻将软件开发
软件开发有哪些证书
扫黄打非网络安全课主题班会
甘肃存储服务器机箱哪里有云空间
网络安全专责
mysql怎么连接数据库
女生可以学软件开发嘛