Creating a Maven project

Create a Java folder in the main directory and convert it to Sources Root. Create a project directory structure as follows:

The configuration of engineering

Configure POM.xml to introduce the relevant JAR packages

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <! --> <spring.version>4.2.5.RELEASE</spring.version> <! Mybatis. Version >3.3.1</mybatis. Version > <! --> <mysql-driver.version>5.1.38</mysql-driver.version> <! --log4J log package version number --> <slf4j.version>1.7.18</slf4j.version> <log4 j. version > 1.2.17 < /log4j.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> < version > 3.8.1 < / version > < scope >test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId> <version>3.8.1</version> <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> < version > 3.1.0 < / version > < / dependency > < the dependency > < groupId > org. Springframework < / groupId > < artifactId > spring - the core < / artifactId > < version > 4.2.5. RELEASE < / version > < / dependency > < the dependency > < the groupId > org. Springframework < / groupId > < artifactId > spring - web < / artifactId > < version > 4.2.5. RELEASE < / version > </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> < version > 4.2.5. RELEASE < / version > < / dependency > < the dependency > < groupId > mysql < / groupId > < artifactId > mysql connector - Java < / artifactId > < version > 5.1.38 < / version > < / dependency > < the dependency > < the groupId > Commons DBCP - < / groupId > < artifactId > Commons DBCP - < / artifactId > < version > 1.2.2 < / version > < / dependency > <dependency> <groupId>org.mybatis</groupId> <artifactId> <version>3.3.1</version> </dependency> < the dependency > < groupId > org. Mybatis < / groupId > < artifactId > mybatis - spring < / artifactId > < version > 1. < / version > </dependency> </dependencies>Copy the code

Right-click the project and select Maven menu Reimport to import the reference package

Configure web. XML

<! DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app  xmlns="http://xmlns.jcp.org/xml/ns/javaee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
          version="3.1"> <display-name>ume web server</display-name> <! How does the Spring framework solve the problem of encoding strings from pages? The Spring framework provides us with a CharacterEncodingFilter that filters for each browser request, and then adds on top of it a feature that the parent class doesn't have: character encoding. The encoding used to set the encoding format, forceEncoding used to set whether to ignore the request. The getCharacterEncoding () method, which is set totrueForces the previous encoding format to be overwritten. --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <! When Spring is used in a project, the applicationContext.xml configuration file does not contain a BeanFactory. To directly reference Spring container-managed beans in the business layer class file, you can do the following: > <! ContextLoaderListener--> <! --ContextLoaderListener is used to automatically assemble ApplicationContext configuration information when the Web container is started. Because it implements the ServletContextListener interface, the methods it implements are executed by default when the web.xml is configured to start the container. The ContextLoader class is associated with the ContextLoaderListener, so the entire loading and configuration process is done by the ContextLoader. The ContextLoader can be generated by ContextLoaderListener and ContextLoaderServlet. If you look at the ContextLoaderServlet API, you can see that it is also associated with the ContextLoader class and that it implements the second section of the HttpServlet interface, The ContextLoader creates a class called XmlWebApplicationContext, It implements the interface is WebApplicationContext - > ConfigurableWebApplicationContext - > ApplicationContext - > BeanFactory so that all the beans in Spring are created by this class IUploaddatafileManager uploadManager = (IUploaddatafileManager) ContextLoaderListener.getCurrentWebApplicationContext().getBean("uploadManager"); --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <! Deploy applicationContext XML file --> <! -- If no parameter configuration information is written in web.xml, the default path is"/ WEB - INF/applicationContext. XML, XML files created in the WEB - INF directory must be the name of the applicationContext. The XML. To customize the file name, add contextConfigLocation to web. XML: Specify the corresponding XML file name in <param-value> </param-value>. If there are multiple XML files, write them together and separate them with commas (,). ApplicationContext -*.xml can also be used with wildcards, such as applicationContext-ibatis-base. XML, applicationContext-action.xml, Files such as ApplicationContext-ibatis -dao.xml are loaded together. The ContextLoader class is associated with the ContextLoaderListener, so the entire loading and configuration process is done by the ContextLoader. --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value> </context-param> <servlet> <servlet-name>spring_mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <! --<init-param>--> <! --<param-name>contextConfigLocation</param-name>--> <! Value > - < param - * *. The XML value > < param - here you can use a variety of writing -- -- > <! - 1, don't write, use the default value: / WEB - INF / < servlet - name > - servlet. XML - > <! - 2, value > < param - / WEB - INF/classes/dispatcher - servlet. XML value > < param - - - > <! - 3, < param - value > classpath * : the dispatcher - servlet. XML value > < param - - - > <! Separate multiple values with commas --> <! --<param-value>classpath:spring/dispatcher-servlet.xml</param-value>--> <! --</init-param>--> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring_mvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>Copy the code

Configuration DispatcherServlet

Create spring_MVC-servlet.xml in the WEB-INF directory

<? xml version="1.0" encoding="UTF-8"? > <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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <! <context:component-scan base-package= -- <context:component-scan base-package="com.ume.webserver.controller"/ > <! -- Activate @required, @AutoWired, JSR 250', @postconstruct etc. --> <context:annotation-config/> <! MVC :default-servlet-handler/> <! -- Open annotations --> <! < MVC :annotation-driven/> <! Model view name resolution, that is, add prefix and suffix to model view name: / WEB - INF/JSP/folder of the JSP file - > < bean class = "org. Springframework. WEB. Servlet. The InternalResourceViewResolver" > < property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean>  </beans>Copy the code

Create controller and corresponding JSP file

Run the project

Run, Edit Configurations,Add New Configuration select Tomcat Server Local, server default, Deployment Configuration:

After the project start type http://localhost:8080/welcome in your browser

Export the WAR package and configure it to the Tomcat server

Bring up the Maven Projects options window, select Lifecycle, and double-click the Package to generate the WAR package

Deploying WAR in Tomcat:

Add the war file to tomcat directory webapps and modify server.xml in tomcat directory conf

 <Context path="/" docBase="umewebserver.war" debug="0" privileged="true" reloadable="true"/>
Copy the code

Perform startup. Bat start tomcat, pay attention to the need to configure the JAVA_HOME environment variable After the success of the launch to http://localhost:8080/umewebserver/welcome

Subsequent sections

  • Mysql > install mysql
  • Integrate Mybatis to realize data increase, delete, change and check