SpringMVC学习笔记四:结果跳转

SpringMVC学习笔记四:结果跳转,第1张

5、结果跳转

在SpringMVC下,有多种结果跳转方式,下面分别进行介绍。

5.1、返回字符串跳转

这一跳转方式我们经常会用到,使用这种跳转方式需要配置视图解析器,视图解析器的作用是对返回的字符串进行拼接,生成一个URL地址,跳转到指定的页面。


<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("/t2")
    public String Controller1(Model model) {
        model.addAttribute("msg", "helloController");
        return "test";
    }

视图解析器会将返回来的"test"拼接为/WEB-INF/jsp/test.jsp,并跳转到指定的页面。Model可以携带传递给视图上的数据。

5.2、ModelAndView

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

页面 : {视图解析器前缀} + viewName +{视图解析器后缀}


<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      id="internalResourceViewResolver">
    
    <property name="prefix" value="/WEB-INF/jsp/" />
    
    <property name="suffix" value=".jsp" />
bean>
public class MyController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg", "controller");
        modelAndView.setViewName("test");
        return modelAndView;
    }
}
5.3、使用原生的Servlet API

通过原生的Servlet API实现结果跳转,转发和重定向,不需要配置视图解析器。

@Controller
public class ResultGo {

    @RequestMapping("result/t1")
    public void test1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("哪里都是你");
    }

    @RequestMapping("result/t2")
    public void test2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //一定要加上 request.getContextPath()
        response.sendRedirect(request.getContextPath() + "/index.jsp");
    }

    @RequestMapping("result/t3")
    public void test3(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setAttribute("msg","/result/t3");
        // /表示当前项目
        request.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(request, response);
    }
}

配置Tomcat,运行测试

重定向

请求转发

注意:重定向不能重定向至WEB-INF目录下

5.4、SpringMVC

通过SpringMVC来实现转发和重定向 - 无需视图解析器;

@Controller
public class ResultSpringMVC {
    @RequestMapping("/rsm/t1")
    public String test1(){
        //转发
        return "/index.jsp";
    }
    @RequestMapping("/rsm/t2")
    public String test2(){
        //转发二
        return "forward:/index.jsp";
    }
    @RequestMapping("/rsm/t3")
    public String test3(){
        //重定向
        return "redirect:/index.jsp";
    }

}

测试前,需要将视图解析器注释掉,如果不注释掉,会出现以下错误。

可以看到视图解析器会拼接返回的字符串,形成一个URL地址。但测试2和3不受影响。

正确的运行结果:



通过SpringMVC来实现转发和重定向 - 配置视图解析器;

下面将刚才注释了的视图解析器的注释取消

@Controller
public class ResultSpringMVC2 {
    @RequestMapping("/rsm2/t1")
    public String test1(Model model) {
        model.addAttribute("msg","Hello, SpringMVC");
        return "test";
    }
    @RequestMapping("/rsm2/t2")
    public String test2(Model model) {
        model.addAttribute("msg","Hello, SpringMVC");
        return "redirect:/index.jsp";
    }
}

配置Tomcat并运行测试

5.5、小结

SpringMVC返回结果的方式有如下几种:

  • 返回字符串,需要配置视图解析器。
  • ModelAndView,设置返回的视图名,同样需要配置视图解析器。
  • 使用原生的Servlet API完成结果跳转,不需配置视图解析器。
  • 使用SpringMVC中的forwardredirect实现转发和重定向。

这一块很容易混淆,建议读者们自己动手,多尝试几次,总结规律,如果有什么错误的话,可以联系我,以便及时更正。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/719530.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存