SpringMVC learning record

Note: The following content is the notes, source code and personal understanding recorded after learning the SpringMVC video of Beijing Power Node, recorded for learning only

Chapter 4 SpringMVC core technology

4.1 Request Redirection and Forwarding

After processing a request, a processor can jump to another resource in two ways: request forwarding and redirection. Depending on the type of resource to jump to, there are two types: jump to page and jump to other processors. Note that for a page requesting a forward, it can be a web-INF page; The redirected page cannot be a web-INF page. Because a redirect is equivalent to the user making another request, the user cannot directly access the WEB-INF Chinese source.

The SpringMVC framework encapsulates the request forwarding and redirection operations in the original Servlet. Forwarding and redirection can now be implemented in a simple manner.

Forward: forward, to achieve the request. GetRequestDispatcher ("xx.jsp").forward() redirect: Response.sendreDirect ("xxx.jsp")
Copy the code

4.1.1 Request Forwarding

4.1.2 Requesting Redirection

Project Structure:  Here is the source code:

  • colntroller
package com.bjpowernode.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.RequestWrapper;

/ * * *@RequestMapping: * value: The common part of all requested addresses, called the module name * location: placed above the class */
@Controller
public class MyController {
    SetViewName ("forward: view file path ") * Forward features: Do not use the view parser, just as there is no view parser in the project */
    @RequestMapping(value = "/doForward.do")
    public ModelAndView doSome(a){
        // The some.do request was processed. The service call processing is complete.
        ModelAndView mv  = new ModelAndView();
        mv.addObject("msg"."Welcome to Web development with SpringMVC.");
        mv.addObject("fun"."I'm doing doSome.");
        // Display forwarding
        //mv.setViewName("forward:/WEB-INF/view/show.jsp");

        mv.setViewName("forward:/hello.jsp");
        return mv;
    }

    Syntax: setViewName("redirect: view complete path ") * redirect: Not used with a view parser, as if there is no view parser in the project * * framework for redirection operations: * 1. The framework converts the simple type of data in the Model to a string, which is used as a get request parameter in hello.jsp. * The purpose is to pass data between doredirect. do and hello.jsp requests * * 2. You can use the parameter collection object ${param} to get the request parameter value * ${param.myname} * * 3 on the target hello.jsp page. Redirection cannot access /WEB-INF resource */
    @RequestMapping(value = "/doRedirect.do")
    public ModelAndView doWithRedirect(String name,Integer age){
        // The some.do request was processed. The service call processing is complete.
        ModelAndView mv  = new ModelAndView();
        // Data is put into the request scope
        mv.addObject("myname",name);
        mv.addObject("myage",age);
        / / redirection
        //mv.setViewName("redirect:/hello.jsp");
        //http://localhost:8080/ch08_forard_redirect/hello.jsp? myname=lisi&myage=22
        // The redirect cannot access the /WEB-INF resource
        mv.setViewName("redirect:/WEB-INF/view/show.jsp");
        returnmv; }}Copy the code
  • springmvc.xml

      
<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"
       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">

    <! Declare component scanner -->
    <context:component-scan base-package="com.bjpowernode.controller" />

    <! Declare the view parser in the SpringMVC framework to help developers set the path of view files.
    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <! Prefix: view file path -->
        <property name="prefix" value="/WEB-INF/view/" />
        <! -- suffix: extension of view file -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
Copy the code
  • maven pom.xml

      

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.bjpowernode</groupId>
  <artifactId>ch08-forard-redirect</artifactId>
  <version>1.0 the SNAPSHOT</version>
  <packaging>war</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <! - the servlet dependence - >
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <! - the JSP rely on -- -- >
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2.1 - b03</version>
      <scope>provided</scope>
    </dependency>
    <! - for springmvc dependence - >
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.5. RELEASE</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <! -- Encoding and compiling and JDK version -->
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Copy the code

Forward: indicates forwarding redirect: indicates redirection. Both forward and redirect are keywords. They have a common feature and do not work with view parsers

Extension:

Forward and Redirect they both have access to view files, such as a JSP or HTML

forward:/hello.jsp forward:/main.html

Both forward and Redirect have access to other controllers

forward:/some.do , redirect:/other.do

Processor methods can return ModelAndView, String, and void with forward and redirect