Interpretation of the operating mechanism of SpringMVC's Hello World

                                                                                

"Opening Verses"
the most profound subtlety
Hundreds of thousands of catastrophe
I have seen and heard today
Willing to understand the true meaning of Spring
                                                                                                                            ---Super Java rookie

This poem expresses poet Han Yu's boundless love for SpringMVC. The poet said: If you hear the Tao in the morning, you can die in the evening. Hence, this blog post.

  1. Build a SpringMVC program.
  2. Interpret SpringMVC's configuration files, especially DispatcherServlet and ContextLoaderListener.
  3. Compare DispatcherServlet and ContextLoaderListener based on XML configuration and Java configuration.

In the past, the widow spent two days in order to build a SpringMVC Hello World. Not how hard it was, but the IDEA configuration was wrong, but didn't know it at the time. The widow is here to show mercy and present a complete process of SpringMVC for the Java rookie. If you still can't build it, then it can be seen that you are the sacrifice of Spring like the widow.

  • Build a SpringMVC program.

Step 1: Prepare tools IDEA, Maven.

Step 2: Build the environment using maven, first build the skeleton of the web project, and omit the details of the middle brain.

The pom.xml file is complete as follows:

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
    <springmvc-version>5.0.0.RELEASE</springmvc-version>
  </properties>

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

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${springmvc-version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${springmvc-version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${springmvc-version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${springmvc-version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${springmvc-version}</version>
    </dependency>
  </dependencies>


Looking at the directory structure carefully, it is not a SpringMVC project at this time, and SpringMVC framework support needs to be added.


After adding framework support, the project structure obviously has two more xml files.

This is the previous web.xml without adding SpringMVC framework support


This is the web.xml after adding SpringMVC framework support


Configure tomcat, start the server

send hello.mvc request


  • Interpret SpringMVC's configuration files, especially DispatcherServlet and ContextLoaderListener.

What did ContextLoaderListener come into this world for? What is the purpose of DispatcherServlet? Everything is reasonable.

Notice what ContextLoaderListener is? Essentially a Listener, what is DispatcherServlet essentially? is a servlet.

Having said that, if you are smart enough, the first difference between ContextLoaderListener and DispatcherServlet is already half-covered.

However, if you are not as mobile as I imagined, then please look down, I am modifying the relative position of ContextLoaderListener and DispatcherServlet in web.xml.

before fixing:

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  
  
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.form</url-pattern>
  </servlet-mapping>
</web-app>

After modification:


See if you change the order of servlets and listeners, there will be an error. The error message is as follows:


What's the meaning? This means that the order of tags in the web-app must be placed in the specified order. Since programs are loaded sequentially, what does this mean? This means that the listener must be loaded before the servlet can be loaded. So in loading order, ContextLoaderListener precedes DispatcherServlet. What does it mean that one thing must come after another? In fact, the scope of the ContextLoaderListener context is Spring global, but the DispatcherServlet context is only the web layer. This is the relationship between the whole and the part. The DispatcherServlet is responsible for loading the beans of the web layer, and the ContextLoaderListener is responsible for loading the beans of the business layer and the persistence layer. How can you see it?

<servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>*.form</url-pattern>
</servlet-mapping>

DispatcherServlet加载的应用上下文是dispatcher-servlet.xml

<context:component-scan base-package="com.logic.controller"></context:component-scan>

<context:annotation-config/>

<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

<context:component-scan base-package="com.logic.controller"></context:component-scan>扫描包中带有@Component注解的组件bean,比如@Controller, @Service, @Repository。

<context:annotation-config/>将@Component组件注入容器。

<mvc:annotation-driven/>处理@Controller映射匹配。

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>

 </bean>

InternalResourceViewResolver视图资源解析。

这些都是web端MVC的范畴,那ContextLoaderListener呢?如果你接触过SSH框架,那你一定见到过 在applicationContext.xml中配置数据库。不错,ContextLoaderListener负责加载后端数据层组件,比如dataSource啊,bean和table之间的对应关系啊。

--------------------------------------------------------睡觉 未完待续------------------------------------------------------------

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325768006&siteId=291194637