SpringMVC learning (III) ------ four kinds of ways to jump pages

Forwarding and Redirection

  1. Forwarding server behavior, redirect the client behavior.
  2. Forwarding HTTP request once, the address bar does not change, requested data is not lost domain
    redirects HTTP requests at least twice, the address bar changes to display the new address, requesting domain information is lost.

For example:
Forward: Xiao Ming to the class teacher called to ask what time school, but the teacher did not know, the phone will be forwarded to the next headmaster allowed to answer. (Xiaoming side shows teacher's phone, but he and the exchange of data is headmaster, before the information did not hang up)
redirection: Xiao Ming to the class teacher called to ask what time school, but the teacher did not know, to tell him headmaster know, Xiao Ming to hang up the phone to dial the headmaster. (Hang up before the request, a new request again)

The first page request is forwarded ------

  1. Create a new project, add configuration jar package, write the configuration file, springmvc registered in the web.xml file, create a package structure
    在这里插入代码片Here Insert Picture Description
  2. New to write a SpringmvcAction.java, a main.jsp
  3. In the preparation of the first page jump index.jsp way

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <!--第一种跳转方式:请求转发页面-->
  <a href="${pageContext.request.contextPath}/admin/one.action">请求转发</a><br>
  </body>
</html>

  1. Write controller in SpringmvcAction.java
package com.oracle.controller;

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


@Controller//表明此类是一个控制器
@RequestMapping("/admin")
public class SpringmvcAction {
    @RequestMapping("/one")
    public String one(){
        //请求转发页面(默认转发方式)
        System.out.println("这是请求转发页面");
        return "main";
    }
}

  1. operation result
    Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Description
  2. For simplicity tomcat in the project name removed
    Here Insert Picture Description

The second request is forwarded action -----

  1. Write new way to jump in index.jsp, write new controller in SpringmvcAction.java
<!--第二种跳转方式:请求转发action页面-->
<a href="${pageContext.request.contextPath}/admin/two.action">请求转发action</a><br>
@RequestMapping("/two")
    public String two(){
        //请求转发action
        System.out.println("这是请求转发action");
        return "forward:/fore/other.action";
    }
  1. Creating a write controller SpringMvcOther.java
package com.oracle.controller;

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

@Controller
@RequestMapping("fore")
public class SpringMvcOther {
    @RequestMapping("other")
    public String other(){
        System.out.println("经过了other.action");
        return "main";
    }

}

  1. operation result
    Here Insert Picture Description
    Here Insert Picture Description
    Here Insert Picture Description

The third ------ redirects

  1. Write new way to jump in index.jsp, write new controller in SpringmvcAction.java
 <!--第三种跳转方式:重定向页面-->
<a href="${pageContext.request.contextPath}/admin/three.action">重定向页面</a><br>
 @RequestMapping("/three")
    public String three(){
        //重定向页面
        System.out.println("这是重定向面");
        return "redirect:/main.jsp";
    }
  1. operation result
    Here Insert Picture Description
    Here Insert Picture DescriptionHere Insert Picture Description

The fourth ------ redirect action

1. Write a new jump in index.jsp way, the preparation of the new controller and SpringMvcAction.java in SpringmvcAction.java

<!--第四种跳转方式:重定向action-->
<a href="${pageContext.request.contextPath}/admin/four.action">重定向action(+转发)</a><br>
<a href="${pageContext.request.contextPath}/admin/five.action">重定向action显示main.jsp</a><br>

SpringmvcAction.java

 @RequestMapping("/four")
    public String four(){
        //重定向页面
        System.out.println("这是重定向action");
        return "redirect:/fore/other.action";
    }
    @RequestMapping("/five")
    public String five(){
        //重定向页面
        System.out.println("这是重定向action");
        return "redirect:/fore/others.action";
    }

SpringMvcAction.java

   @RequestMapping("others")
    public String others(){
        System.out.println("经过了others.action");
        return "redirect:/main.jsp";
    }
  1. Operating results
    Here Insert Picture Descriptionredirect action + forwarding
    Here Insert Picture Descriptionredirect action
    Here Insert Picture Description
Published 19 original articles · won praise 6 · views 1040

Guess you like

Origin blog.csdn.net/weixin_43288999/article/details/104668874