[Spring] Spring and the Web (the last chapter)

Keywords: Spring

Spring learning is coming to an end~~~~~~~~~

👉 spring-web 👉 Complete the student registration function.

Steps:

  1. New maven
  2. Modify pom.xml: dependencies other than spring and mybatis, servlet and jsp dependencies
  3. Create entity class Student, corresponding to Student2 table
  4. Create dao interface and mapper file
  5. Create mybatis master profile
  6. Create service and implementation classes
  7. Create a servlet, receive the requested parameters, and call the service object
  8. Create jsp and submit request parameters
  9. Create a jsp as a view to display the processing results of the request
  10. Create spring configuration file

1. Create a maven - web project

2. Add dependency

<!-- servlet rely on -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

    <!-- jsp rely on -->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2.1-b03</version>
      <scope>provided</scope>
    </dependency>


    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!--Listener dependency-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>

    <!--spring rely on-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>

    <!--spring Transaction dependency-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>

    <!--mybatis rely on-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.1</version>
    </dependency>

    <!--mybatis and spring integrate-->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.1</version>
    </dependency>

    <!--mysql drive-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.9</version>
    </dependency>

    <!--Ali's connection pool-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.12</version>
    </dependency>

plug-in unit:

<build>
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
  </build>

3. Directory structure: create java and resources and mark them

4. Create entity class and interface class

5. Create mapper file


6. Main configuration files: Spring, mybatis config, and properties

7. Create business layer service and implementation classes
Business level method: Verb + noun (see the meaning of name)



8. Create a servlet, receive the requested parameters, and call the service object





9. Create jsp and submit request parameters
Delete the original index.jsp and regenerate a JSP file with the idea template

Configure server:





There are records in the database


Query students:
form 👇

Create a new servlet:

	<servlet>
        <servlet-name>QueryStudentServlet</servlet-name>
        <servlet-class>com.bjpowernode.controller.QueryStudentServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>QueryStudentServlet</servlet-name>
        <url-pattern>/queryStudent</url-pattern>
    </servlet-mapping>


In the doGet method of QueryStudentServlet:


Start the server and enter the address in the browser:



After looking for it for a long time, I don't know what went wrong...... The query results are not displayed, but there is output object information on the console.

1 now the problem of using container objects

1. Create container objects many times
2. Create container objects in multiple servlet s

2 what kind of container object is needed

1. There is only one container object. You can create it once
2. Container objects should be shared throughout the project. Multiple servlet s can use the same container object

To solve the problem, use the listener ServletContextListener (the two methods are executed initially and destroyed)
In the listener, the created container object should be placed in the scope of ServletContext in the web application.

3 ContextLoaderListener listener object

ContextLoaderListener is a listener object provided by the spring framework. The listener is used to:
1. Create container object: once
2. Put the container object into the scope of ServletContext.

Use steps:
1.pom.xml adds dependency on spring Web

<!--Listener dependency-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>5.2.5.RELEASE</version>
</dependency>

2.web.xml declaration listener


Default listener: configuration file read when creating container object: / WEB-INF/applicationContext.xml
Profile path used by custom container:
Context param: it is called context parameter, and it provides parameters to listeners

Listener settings:

Start server: successful

4. Contextloaderlistener source code

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {

    //Initial method of listener
    public void contextInitialized(ServletContextEvent event) {
        this.initWebApplicationContext(event.getServletContext());
    }

}


Initialization method internal: core part

View context:

private WebApplicationContext context;
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
            try {
                if (this.context == null) {
                    //Create container object for srping
                    this.context = this.createWebApplicationContext(servletContext);
                }
                
           //Put the container object into the ServletContext scope of the
          //key=WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
          //value = container object
                servletContext.setAttribute(
                    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,                             this.context);
               
            } catch (Error | RuntimeException var8) {
     
            }
        }
    }

 

WebApplicationContext is a container object used in a web project

public interface WebApplicationContext extends ApplicationContext 


Create container objects through the above method: no matter how many requests are made, only one object is created and shared.

It's much easier to remove the logs from the mybatis file

Request query multiple times:

//Use the tools and methods provided by Spring to obtain the container object
ServletContext sc = getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);

or

//Use the tools and methods provided by Spring to obtain the container object
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

Posted by FadeToLife on Thu, 11 Nov 2021 00:39:00 -0800