我是靠谱客的博主 深情灯泡,这篇文章主要介绍Spring MVC中使用Controller如何进行重定向,现在分享给大家,希望可以做个参考。

Controller如何进行重定向

Spring MVC中进行重定向

本人知道的有两种方式

方法返回的URI(相对路径)中加上"redirect:"前缀,声明要重定向到该地址

使用HttpServletResponse对象进行重定向

注意

"redirect:"后面跟着的是"/"和不跟着"/"是不一样的:

1) "redirect:"后面跟着"/": 说明该URI是相对于项目的Context ROOT的相对路径

2) "redirect:"后面没有跟着"/": 说明该URI是相对于当前路径

具体看demo理解这两种方式的实现

RedirectURLController.java:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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地址不变.可能会找不到静态文件:

复制代码
1
2
3
4
5
@GetMapping(value = "/index") @ApiOperation("首页") public void index(HttpServletRequest request, HttpServletResponse response) throws Exception { request.getRequestDispatcher("/index.html").forward(request, response); }

重定向redirect

controller中返回值为void

复制代码
1
2
3
4
5
@GetMapping(value = "/index") @ApiOperation("首页") public void index(HttpServletRequest request, HttpServletResponse response) throws IOException { response.sendRedirect("/index.html"); }

第三种方式:controller中返回值为ModelAndView

复制代码
1
return new ModelAndView(“redirect:/toList”);

以上为个人经验,希望能给大家一个参考,也希望大家多多支持靠谱客。

最后

以上就是深情灯泡最近收集整理的关于Spring MVC中使用Controller如何进行重定向的全部内容,更多相关Spring内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(155)

评论列表共有 0 条评论

立即
投稿
返回
顶部