SpringMVC——SpringMVC的几种结果跳转方式

SpringMVC——SpringMVC的几种结果跳转方式

一、环境搭建

新建Maven的web项目
1.在pom.xml中导入jar包依赖

 <!-- Spring MVC 及 Spring系列包 -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.3.24.RELEASE</version>
    </dependency>
    <!--Servlet核心-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
    </dependency>
    <!-- JSTL -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

2.在web.xml中配置DispatcherServlet中心分发器,关联springmvc-servlet.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">

  <!--1.注册DispatcherServlet-->
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--关联一个springmvc的配置文件:【servlet-name】-servlet.xml-->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc-servlet.xml</param-value>
    </init-param>
    <!--启动级别-1-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  
  <!--/ 匹配所有的请求;(不包括.jsp)-->
  <!--/* 匹配所有的请求;(包括.jsp)-->
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!--这里使用 / ,不要用 /*-->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

3.创建springmvc-servlet.xml文件,并配置自动扫描包和视图解析器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">


    <!-- 自动扫描指定的包,下面所有注解类交给IOC容器管理 -->
    <context:component-scan base-package="com.muhan.controller"/>

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

4.在WEB-INF下新建jsp目录,并新建hello.jsp的视图

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    ${msg}
</body>
</html>
二、几种SpringMVC的结果跳转方式
第一种:使用模型视图对象ModelAndView(使用视图解析器)
@Controller
public class HelloController{
    @RequestMapping("/test")
    public ModelAndView hello(){
        //创建ModelAndView对象
        ModelAndView mv = new ModelAndView();
        //将要展示的视图添加到ModelAndView对象中
        mv.addObject("msg","方式一:使用ModelAndView对象");
        //将视图路径添加到ModelAndView中
        mv.setViewName("hello");
        //返回ModelAndView对象
        return mv;
    }
}

测试:
在这里插入图片描述

第二种:无返回值,使用Servlet API(不使用视图解析器)

需要知道的是:使用ServletAPI这种方式是不经过视图解析器的,所以我们可以不用配置视图解析器

@Controller
public class HelloController{
    @RequestMapping("/test")
    public void hello(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //request直接设置属性携带参数
        request.setAttribute("msg","方式二:使用Servlet API");
        //转发到hello.jsp也能达到同样的效果
        request.getRequestDispatcher("WEB-INF/jsp/hello.jsp").forward(request,response);
    }
}

在这里插入图片描述

第三种:带返回值,使用Servlet API(不使用视图解析器)
  • ①转发——使用返回值为:forward:路径

    @Controller
    public class HelloController{
        @RequestMapping("/test")
        public String hello(HttpServletRequest request, HttpServletResponse response) {
            //request直接设置属性携带参数
            request.setAttribute("msg","方式三:使用Servlet API —— 02");
            //返回值里直接使用forward:表示转发
            return "forward:WEB-INF/jsp/hello.jsp";
        }
    }
    

    在这里插入图片描述

  • ②重定向——使用返回值为:redirect:/请求

    @Controller
    public class HelloController{
        @RequestMapping("/test")
        public String hello(HttpServletRequest request, HttpServletResponse response) {
            //request直接设置属性携带参数
            request.setAttribute("msg","我是test,test02重定向过来了!");
            //返回值里直接使用forward:表示转发
            return "forward:WEB-INF/jsp/hello.jsp";
        }
        @RequestMapping("/test02")
        public String hello02(HttpServletRequest request, HttpServletResponse response) {
            //request直接设置属性携带参数
            request.setAttribute("msg","我是test02");
            //redirect:表示重定向到哪个请求,比如我们这里就是重定向到上面的/test请求
            return "redirect:/test";
        }
    }
    

    我们输入:localhost:8080/springmvc/test02发现重定向到了/test请求,网页路径也发生了变化
    在这里插入图片描述

第四种:使用SpringMVC的视图解析器(最常用)

在这里插入图片描述

@Controller
public class HelloController{
    @RequestMapping("/test")
    public String hello(Model model){
        model.addAttribute("msg","方式四:使用SpringMVC的视图解析器(最常用)");
        //经过springmvc-servlet.xml中配置的视图解析器会给hello拼接前缀和后缀为:WEB-INF/jsp/hello.jsp
        return "hello";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值