当前位置:首页 > 通信资讯 > 正文

springmvc中的controller(spring mvc controller)

目录
  • Controller如何进行重定向
    • 本人知道的有两种方式
      • 注意
    • 具体看demo理解这两种方式的实现
    • controller请求转发,重定向
      • 了解
        • 转发forward
          • 重定向redirect

          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”);

          以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

          原文链接:https://blog.csdn.net/yh_zeng2/article/details/75136772

          如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

          为您推荐:

          发表评论

          ◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。