SpringMVC :转发、重定向、RedirectAttributes

SpringMVC :转发、重定向、RedirectAttributes

SpringMVC跳转默认是转发!

1. View Resolver

设置ModelAndView对象,根据视图的名称和视图解析器跳转到指定的页面!

页面路径:视图解析器前缀 + 视图名 + 视图解析器后缀。

<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      id="internalResourceViewResolver">
    <!-- 前缀 -->
    <property name="prefix" value="/WEB-INF/jsp/" />
    <!-- 后缀 -->
    <property name="suffix" value=".jsp" />
</bean>

Controller类:

@RequestMapping("/h2")
public ModelAndView Hellomvc2(HttpServletRequest httpServletRequest, 
                              HttpServletResponse httpServletResponse) throws Exception{
     //返回一个模型视图对象
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","乘风御剑");
        mv.setViewName("hello");//设置返回的视图名
        return mv;
}

InternalResourceViewResolver 是最常用的视图解析器,但不是唯一的一个,Spring MVC 还有其他的视图解析器,例如:XmlViewResolver。

2、SpringMVC

  • 没有视图解析器的情况下:
@Controller
public class HelloMVC {
    @RequestMapping("/t1")
    public String test1(){
        //转发
        return "/index.jsp";
    }

    @RequestMapping("/t2")
    public String test2(){
        //转发二
        return "forward:/index.jsp";
    }

    @RequestMapping("/t3")
    public String test3(){
        //重定向
        return "redirect:/index.jsp";
    }
}
  • 有视图解析器的情况下:

    重定向 , 不需要视图解析器 , 本质就是重新请求一个新地方嘛 , 所以注意路径问题.

    可以重定向到另外一个请求实现 .

@Controller
public class ResultSpringMVC2 {
    @RequestMapping("/rsm2/t1")
    public String test1(){
        //转发
        return "test";
    }

    @RequestMapping("/rsm2/t2")
    public String test2(){
        //重定向
        return "redirect:/index.jsp";
        //return "redirect:hello.do"; //hello.do为另一个请求/
    }

}
  • 重定向传参(RedirectAttributes):

    为了解决重定向后数据丢失的问题,SpringMVC3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的

    要使用RedirectAttributes,必须在 Spring MVC 配置文件中添加 <mvc:annotation-driven /> 。然后必须在方法上多加一个参数,参数类型为 RedirectAttributes 。RedirectAttributes 是 Model 接口的实现类。

    注意:

    1、使用 RedirectAttributes 的 addAttribute 方法传递参数会跟随在 URL 后面。

    2、使用 addFlashAttribute 不会跟随在 URL 后面,会把该参数值暂时保存于 session,待重定向 url 获取该参数后从 session 中移除,这里的 redirect 必须是方法映射路径,jsp 无效 。

    @RequestMapping("/forward1.do")
    public String demo1(RedirectAttributes model) {
      model.addFlashAttribute("msg", "hello world");
      return "redirect:/hello.do";
    }
    
    @RequestMapping("/hello.do")
    public String demo2() {
      return "demo";
    }

3、使用Servlet进行转发和重定向(不需要视图解析器)

@Controller
public class ResultGo {

    @RequestMapping("/t1")
    public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {     //向外输出
        rsp.getWriter().println("Hello,Spring BY servlet API");
    }

    @RequestMapping("/t2")
    public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {     //重定向
        rsp.sendRedirect("/index.jsp");
    }

    @RequestMapping("t/t3")
    public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {
        //转发
        req.setAttribute("msg","/result/t3");
        req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);
    }

}

4、跳转异常处理

**在 Webapp 中,一旦发生了异常可以层层上抛,直到抛给 Spring MVC 的 DispacherServlet 。而 DispacherServlet 可以根据开发者所配置的配置一个 *全局异常处理器*,来决定跳转哪个页面,展示何种信息。**

默认情况下,Spring MVC 所使用的异常处理器是:SimpleMappingExceptionResolver,它所处理的效果正是我们当前所看到的内容。

自定义的异常处理器需要实现 Spring MVC 提供的 HandlerExceptionResolver 接口,在其中 resolveException() 方法中可以根据自定义的逻辑返回 ModelAndView 对象,以决定跳转页面和展示信息。

使用自定义的异常处理器,需要在 Spring MVC 配置文件中做出配置:

<bean id="customHandleException" class="你的异常处理类"/>

猜你喜欢

转载自www.cnblogs.com/whitespaces/p/12454777.html
今日推荐