重定向和转发

本文详细介绍了在SpringMVC框架下如何使用ModelAndView、ServletAPI以及不同方式实现重定向和转发。内容包括无视图解析器的情况及带有视图解析器的情况,通过具体代码示例展示各种方法的效果。
摘要由CSDN通过智能技术生成

一、ModelAndView

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

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

二、ServletAPI

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

  1. 通过HttpServletResponse进行输出
package com.massimo.controller;

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

import javax.imageio.IIOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class ModelTest1 {

    @RequestMapping("m1/t1")
    public String test1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        System.out.println(session.getId());
        return "hello";
    }
}

效果:
在这里插入图片描述

  1. 通过HttpServletResponse实现重定向
package com.massimo.controller;

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

import javax.imageio.IIOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class ModelTest1 {

    @RequestMapping("/m1/t2")
    public void test2(HttpServletRequest request , HttpServletResponse response) throws Exception{
        response.sendRedirect("/hello");
    }
}

效果:
在这里插入图片描述

  1. 通过HttpServletResponse实现转发
package com.massimo.controller;

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

import javax.imageio.IIOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class ModelTest1 {
    @RequestMapping("m1/t3")
    public void test3(HttpServletRequest request , HttpServletResponse response) throws Exception{
        //转发
        request.setAttribute("msg","/m1/t3");
        request.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(request,response);
    }
}

在这里插入图片描述
项目结构:
在这里插入图片描述

三、通过SpringMVC来实现转发和重定向——无需视图解析器

沿用上面的项目,并将视图解析器注释掉!

3.1、方式一

@RequestMapping("m1/t1")
    public String test1(Model model) {
        //转发
        model.addAttribute("msg","ModelTest1");
        return "/WEB-INF/jsp/hello.jsp";
    }

效果:
在这里插入图片描述

3.2、方式二

@RequestMapping("m1/t1")
    public String test1(Model model) {
        //转发
        model.addAttribute("msg","ModelTest1+forward");
        return "forward:/WEB-INF/jsp/hello.jsp";
    }

方式二:
在这里插入图片描述

3.3、方式三

@RequestMapping("m1/t1")
    public String test1(Model model) {
        //重定向
        model.addAttribute("msg","ModelTest1+redirect");
        return "redirect:/index.jsp";
    }

效果:
在这里插入图片描述

四、通过S pringMVC来实现转发和重定向——有视图解析器

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值