SpringMVC转发和重定向


SpringMVC转发和重定向

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>

对应的controller类 ControllerMV

@Controller
public class ControllerMV {

    //请求地址: localhost:8080/test1
    @RequestMapping("/test1")
    public ModelAndView test(ModelAndView mv){
        mv.addObject("msg","hello, mv");
        mv.setViewName("test");
        return mv;
    }
}

无视图解析器

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
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
    <context:component-scan base-package="com.allen.controller"/>

<!--    &lt;!&ndash; 让Spring MVC不处理静态资源 &ndash;&gt;-->
<!--    <mvc:default-servlet-handler />-->

<!--    <mvc:annotation-driven />-->

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

</beans>

新建测试类 ControllerTest4

实现转发(两种方式)、重定向

@Controller
public class ControllerTest4 {

    //请求地址为  localhost:8080/m1
    @RequestMapping("/m1")
    public String test1(Model model){
        model.addAttribute("msg","ControllerTest4");
        //转发一
//        return "/WEB-INF/jsp/test.jsp";
        //转发二
//        return "forward:/WEB-INF/jsp/test.jsp";
        //重定向
        return "redirect:/index.jsp";
    }
}

有视图解析器

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
       https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 自动扫描包,让指定包下的注解生效,由IOC容器统一管理 -->
    <context:component-scan base-package="com.allen.controller"/>

<!--    &lt;!&ndash; 让Spring MVC不处理静态资源 &ndash;&gt;-->
<!--    <mvc:default-servlet-handler />-->

<!--    <mvc:annotation-driven />-->

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

</beans>

在测试类 ControllerTest4 下新建测试方法

实现转发、重定向

//请求地址为  localhost:8080/m2
@RequestMapping("/m2")
public String test2(Model model){
    model.addAttribute("msg","ControllerTest4");
    //转发一
//        return "test";
    //重定向
    return "redirect:/index.jsp";
}

补充:原生Servlet API转发和重定向

通过设置ServletAPI , 不需要视图解析器 .

1、通过HttpServletResponse进行输出

2、通过HttpServletRequest实现转发

3、通过HttpServletResponse实现重定向

package com.allen.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Controller
public class ControllerServlet extends HttpServlet {

    //输出
    //请求地址为  localhost:8080/m3
    @RequestMapping("/m3")
    protected void test1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().println("hello, spring by servlet API");
    }

    //转发
    //请求地址为  localhost:8080/m4
    @RequestMapping("/m4")
    protected void test2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("msg","hello, forward");
        req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,resp);
    }

    //重定向
    //请求地址为  localhost:8080/m5
    @RequestMapping("/m5")
    protected void test3(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.sendRedirect("index.jsp");
    }

}

文章作者: Hailong Gao
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Hailong Gao !
评论
  目录