springmvc基础知识(15):重定向和转发以及mvc:view-controller标签的使用

首先来看常规用法

@RequestMapping("/index")
public String index(){
    System.out.println("index Controller...");
    return "hello";
}

发送请求 http://localhost:8080/springmvc-rest/index
后台接收请求直接跳转到hello.jsp页面。


重定向(redirect)

重定向也是web开发中常用的,在springmvc中,重定向相当的简单

@RequestMapping("/index")
public String index(){
    return "redirect:hello";
}

@RequestMapping("/hello")
public String hello(){
    return "hello";
}
  • 发送请求http://localhost:8080/springmvc-rest/index
    当执行到return "redirect:hello";时,发现是个重定向服务端会要求客户端重新发送http://localhost:8080/springmvc-rest/hello请求
  • 由于重定向Model不共享,所以之前提交的数据在重定向后请求对应的处理方法hello()中中无法获取。
  • 重定向后浏览器的输入框中URL也发生变化。
  • 对于redirect而言,Request的attribute不会被传递,放到session中,session在跳到页面后也会马上移除对象。

如果你希望Request的attribute被传递,可以使用RedirectAttributes类

@RequestMapping("/index")
public String index(HttpServletRequest request,RedirectAttributes redirectAttributes){
    String userName = request.getParameter("name");
    String password = request.getParameter("pwd");
    //重定向传递参数的两种方法
    //1.这种方式会将参数拼接到重定向的 url中  
    redirectAttributes.addAttribute("name", userName);
    //2.这种方式会将参数放到session中,如果刷新页面就会丢失 
    redirectAttributes.addFlashAttribute("pwd", password);
    return "redirect:hello";
}

@RequestMapping(value="/hello", method=RequestMethod.GET)
public String hello(HttpServletRequest request){
    String userName = request.getParameter("name");
    return "hello";
}
  • 由于重定向是客户端重新通过url发送的请求,所以重定向后的请求是 GET 方式的。
  • 使用RedirectAttributes类的addAttribute方法传递参数会跟随在URL后面,如
    上面这个重定向后重新发送的请求url是:
    http://localhost:8080/springmvc-rest/hello?name=xxx
  • 使用RedirectAttributes类的addFlashAttribute方法传递参数不会跟随在URL后面,会把该参数值暂时保存于session,待重定向URL获取该参数后从session中移除,这里的redirect必须是方法映射路径,jsp无效。redirect后的hello.jsp页面中pwd只会出现一次,刷新后pwd再也不会出现了。

转发(forward)

@RequestMapping(value="/index",method=RequestMethod.GET)
public String index(){
   return "forward:hello";
}
@RequestMapping("/hello",method=RequestMethod.GET)
public String hello1(){
   return "hello1";
}
@RequestMapping("/hello",method=RequestMethod.POST)
public String hello2(){
   return "hello2";
}
  • 发送请求http://localhost:8080/springmvc-rest/index 当执行到
    return "forward:hello";时发现是个转发,服务器就将请求转发给hello1()处理,为什么不是hello2()方法呢?那是因为还需要匹配请求的method。
  • 转发不会再经过客户端,直接由服务端调控。
  • 转发后浏览器的输入框中URL不会发生变化。
  • 跳转时Model共享(表单会被重复提交)。

mvc:view-controller标签

如果发送的请求不想通过controller,只想直接地跳转到目标页面,这时候就可以使用mvc:view-controller标签
在配置文件中配置:

<mvc:view-controller path="/hello" view-name="hello"></mvc:view-controller>
  • path=”/hello” 就是你访问的路径(相当于RequestMapping(“/hello”))
  • view-name=”hello”是你所要的视图(如hello.jsp,相当于return “hello”) 配置了这个后对于/hello请求,就会直接交给dispatcherServlet,然后使用ViewResolver进行解析。

相当于以下代码

@RequestMapping(value="/hello")
public String hello(){
    System.out.println("hello");
    return "hello";
}

也可以配置成重定向或者转发:

<mvc:view-controller path="/index" view-name="redirect:hello"></mvc:view-controller>

相当于

@RequestMapping("/index")
public String index(){
    return "redirect:hello";
}

使用了这个标签后必须配置 <mvc:annotation-driven /> 否则会造成所有的@Controller注解无法解析,导致404错误。

猜你喜欢

转载自blog.csdn.net/abc997995674/article/details/80432632