【WEEK2】 【DAY4】Data Processing and Redirection - Methods of Result Redirection 【English Version】

2024.3.7 Thursday

5. Data Processing and Redirection

5.1. Methods of Result Redirection

5.1.1. ModelAndView

Set up a ModelAndView object and jump to the specified page according to the name of the view and the view resolver.
Page: {View Resolver Prefix} + viewName + {View Resolver Suffix}

<!-- View Resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
     id="internalResourceViewResolver">
   <!-- Prefix -->
   <property name="prefix" value="/WEB-INF/jsp/" />
   <!-- Suffix -->
   <property name="suffix" value=".jsp" />
</bean>

The corresponding controller class

public class ControllerTest1 implements Controller {

   public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
       //return a ModelAndView object
       ModelAndView mv = new ModelAndView();
       mv.addObject("msg","ControllerTest1");
       mv.setViewName("test");
       return mv;
  }
}

5.1.2. ServletAPI

1. Set up ServletAPI without needing a view resolver
  • Output via HttpServletResponse
  • Redirect via HttpServletResponse
  • Forward via HttpServletResponse
2. Create ResultGo.java

Insert image description here

package com.kuang.controller;

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

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

@Controller
public class ResultGo {
//    http://localhost:8080/springmvc_04_controller_war_exploded/result/t1
    @RequestMapping("/result/t1")
    public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
        rsp.getWriter().println("Hello,Spring BY servlet API");
    }

//    Entering the following two URLs results in a 404 error
    @RequestMapping("/result/t2")
    public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
        rsp.sendRedirect("/index.jsp");
    }

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

5.1.3. SpringMVC

5.1.3.1. Implement forwarding and redirection through SpringMVC without using a view resolver
5.1.3.2. Comment out the view resolver

Insert image description here

5.1.3.3. Create ResultSpringMVC.java

Insert image description here

package com.kuang.controller;

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

@Controller
public class ResultSpringMVC {
    @RequestMapping("/rsm/t1")
    public String test1(){
        //Forwarding
        return "/index.jsp";
    }

    @RequestMapping("/rsm/t2")
    public String test2(){
        //Secondary forwarding
        return "forward:/index.jsp";
    }

    @RequestMapping("/rsm/t3")
    public String test3(){
        //Redirect, WEB-INF files cannot be accessed through redirect
        return "redirect:/index.jsp";
    }
}
1. Forwarding

http://localhost:8080/springmvc_04_controller_war_exploded//rsm/t1
Insert image description here
http://localhost:8080/springmvc_04_controller_war_exploded//rsm/t2
Insert image description here

2. Redirect

重定向的概述和使用(基于web方面),很简单

5.1.3.4. Use a view resolver and implement forwarding and redirection through SpringMVC
5.1.3.5. Uncomment the view resolver
5.1.3.6. Create ResultSpringMVC1.java

Insert image description here

package com.kuang.controller;

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

@Controller
public class ResultSpringMVC1 {
    @RequestMapping("/rsm1/t1")
    public String test1(){
        //Forwarding
        return "test";
    }

    @RequestMapping("/rsm1/t2")
    public String test2(){
        //Redirect
        return "redirect:/index.jsp";
        //return "redirect:hello.do"; //hello.do is another request
    }
}
5.1.3.7. Results
1. Results

http://localhost:8080/springmvc_04_controller_war_exploded//rsm1/t1
Insert image description here

2. Redirect

http://localhost:8080/springmvc_04_controller_war_exploded//rsm1/t2
Insert image description here
=> Redirected to redirect:/index.jsp

http://localhost:8080/springmvc_04_controller_war_exploded/index.jsp
Insert image description here

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值