Springmvc from entry to source code analysis topic 1_preliminary preparation

Return to the topic list

Springmvc from entry to source code analysis topic 1_preliminary preparation

Preface

The reason for writing this topic is to consolidate the basic knowledge of springmvc for yourself, and also provide a simple and easy-to-understand tutorial for those who are just getting started with springmvc but are not clear about the principles of springmvc.

Note: If you think the writing is not good, please criticize it directly

Related software version

Here to introduce the version of my local related software

  • jdk: jdk1.8.0_221
  • eclipse: Photon Release (4.8.0)
  • springmvc: 3.2.12.RELEASE
    • Let me talk about why you choose the version of springmvc  3.2.12.RELEASE for the following reasons
      • Although spring has been released to the  5.*version now, the core code of springmvc has not changed much, only many other functions 3.2.12.RELEASEhave been added to it, and most of the functional versions of springmvc we use every day have already included
      • In this topic, we will go into the source code analysis of springmvc. In order to make the analysis easier and easier to understand, and to avoid non-core code from interfering with everyone reading the source code, I choose the  3.2.12.RELEASEversion of springmvc
  • maven: 3.5.3
  • tomcat: tomcat-8.5.43

Download link of related software

Basic project environment construction

Next, we will build a springmvc web project from scratch to implement a simple helloword. I have uploaded the relevant source code to my github

The screenshot of the completed project is as follows

Key profile

  • The pom.xml file Maven depends on the following

    <dependencies>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>3.2.12.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>3.2.12.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>3.2.12.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
      </dependency>
      <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>jsp-api</artifactId>
          <version>2.1</version>
      </dependency>
      <dependency>
          <groupId>javax.portlet</groupId>
          <artifactId>portlet-api</artifactId>
          <version>2.0</version>
      </dependency>
      <dependency>
          <groupId>javax.el</groupId>
          <artifactId>el-api</artifactId>
          <version>1.0</version>
      </dependency>
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.0.1</version>
      </dependency>
      <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.17</version>
      </dependency>
    </dependencies>

     

  • The core configuration content of the web.xml configuration file is as follows

    <!-- 使用ContextLoaderListener配置时,需要告诉它Spring配置文件的位置 -->
      <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
      <!-- 
           配置上下文载入器,上下文载入器载入除DispatherServlet载入的配置文件之外的其他上下文配置文件
        最常用的上下文载入器是一个Servlet监听器,器名称为ContextLoaderListener
       -->
      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <!-- 
          SpringMVC的前端控制器,当DispatcherServlet载入后,它将从一个XML文件中
          载入Spring的应用上下文,该XML文件的名字取决于这里DispathcerServlet将试图从一个
         叫做Springmvc-servlet.xml的文件中载入应用上下文,其默认位于WEB-INF目录下
       -->
        <servlet>
          <servlet-name>springmvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <load-on-startup>1</load-on-startup>
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:spring-servlet.xml</param-value>
          </init-param>
      </servlet>
      <servlet-mapping>
          <servlet-name>springmvc</servlet-name>
          <url-pattern>*.do</url-pattern>
      </servlet-mapping>

     

Note: The configuration items in the configuration file have been annotated in detail for each configuration item, so I will not emphasize it here.

We were in web.xml ContextLoaderListenerand DispatcherServletspecify load profile applicationContext.xmland spring-servlet.xml, in this helloword in, we applicationContext.xmldid not configure anything, because the three-tier system based on the principle that we should leave it to service and dao layer bean Configuration, let's take a look at the spring-servlet.xmlconfiguration file

  • spring-servlet.xmlContents of the configuration file

          <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/WEB-INF/jsp/"></property>
          <property name="suffix" value=".jsp"></property>
       </bean>
      <bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
          <property name="mappings">
              <props>
                  <prop key="/hello.do">helloController</prop>
              </props>
          </property>
      </bean>
      <!-- 这里的id="helloController" 对应的是<bean id="simpleUrlMapping">中的<prop>里面的value -->
      <bean id="helloController" class="org.linge.springmvc.study.controller.HelloController"></bean>

     

  • For reasons of space, HelloControllerand the hello.jspcode will not put me out of here, go to the source text at the end github address to download

  • Finally, we use eclipse to climb up our project using tomcat, and then visit it in the browser

    http://localhost:8181/springmvc-study/hello.do
    // 注: tomcat的端口请换成自己本地tomcat的实际端口

    Address, you can see the following page, it means that our environment is successfully built

Well, here we have completed our first springmvc-based helloword environment. In the following articles, we will analyze the configuration of springmvc in web.xml and how it is started.

Source code address

 

Source code address

The source code address of springmvc from entry to source code sharing

Return to the topic list

 

Guess you like

Origin blog.csdn.net/u013328649/article/details/111626882