创建一个maven项目(项目代码最后见):
image.png
项目组织、名称、版本等等:
image.png
然后Next、Next(注意检查项目路径),删除自动生成日志配置文件和applicationContext.xml项目如下:
image.png
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<name>springproject</name>
<groupId>com.lingkang.top</groupId>
<artifactId>springproject</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!--可以在spring官网看最新版本 https://spring.io/projects/spring-framework#learn-->
<!-- 用最新的spring5 2019年10月5日 21:21:39最新版 -->
<spring.version>5.2.0.RELEASE</spring.version>
</properties>
<dependencies>
<!--spring 核心包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!--spring web包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- springMVC-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<!--jstl for jsp page-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--
配置一个运行的tomcat插件,注意spring4才支持,spring5就不支持tomcat7插件
下面的插件并不适用
可参考Apache官网:http://tomcat.apache.org/maven-plugin-2.2/
-->
<!--<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<server>tomcat7</server>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>-->
</plugins>
</build>
</project>
1、配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!--1、首先呢,要启动spring-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--2、启动的时候,添加spring的配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<!--3、启动spring MVC-->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<!--服务器启动就加载Servlet-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2、applicationContext.xml和springMVC.xml
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- spring的配置文件,这里主要配置和业务逻辑相关的内容 -->
<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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx">
</beans>
springMVC.xml:
<?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:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven/>
<!-- 自动扫描控制器controller -->
<context:component-scan base-package="com.lingkang.top.springproject.controller" />
<!-- 定义跳转的文件的前后缀 ,配置视图 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
3、运行项目:
image.png
添加tomcat运行服务器
image.png
添加war:
image.png
运行它:
image.png
效果如下:
image.png
整合spring完毕!这是一个最基础的Maven Spring模板,后续在此模板的基础上加入数据库、事务等操作即可
项目代码:https://gitee.com/lingkang_top/springproject
项目整体如下:
intellij IDEA 2019.2.png