不灭的焱

革命尚未成功,同志仍须努力

作者:php-note.com  发布于:2021-03-27 15:26  分类:Java框架/系统  编辑

一、DEA创建项目

1、新建一个 Maven Project,并选择 webapp 原型:

 

2、填好 GroupId 和 ArtifactID:

 

3、核对一下Maven地址:

 

4、剩下的保持默认,新建好的项目结构如下:

二、添加pom依赖

通过Maven配置文件(pom.xml),添加SpringMVC所需的jar包,

1、在properties标签中,统一配置Spring版本号

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>

    <!-- Spring 版本号 -->
    <spring.version>4.3.30.RELEASE</spring.version>
</properties>

 

2、在 dependencies 中,添加依赖的jar包:

<!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!-- Spring依赖 start -->
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!-- 1.Spring核心依赖:IOC/DI-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.version}</version>
</dependency>

<!-- 2.AOP依赖 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>${spring.version}</version>
</dependency>

<!-- 3.Spring Web依赖 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
</dependency>

<!-- 4.Spring DAO依赖 -->
<!-- spring-jdbc包括了一些如jdbcTemplate的工具类 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>${spring.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>${spring.version}</version>
</dependency>

<!-- 5.Spring Test依赖:方便做单元测试和集成测试 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
</dependency>
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->
<!-- Spring依赖 end -->
<!--++++++++++++++++++++++++++++++++++++++++++++++++++++-->

三、添加框架支撑

配置完 pom.xml后,还需要在IDEA中添加一下框架支撑

右键项目文件夹,选择 Add Framwwork Support

注意:如果弹框中没有显示 Spring 相关模块,按这篇文章解决问题:

【解决】IDEA 创建 Spring MVC 项目时,在 add framework support 中找不到 spring 选项

 

分别选中 SpringSpring MVC,并且选择Spring时,记得勾选Create empty spring-config.xml

 

点击 OK,我们会发现WEB-INF文件夹下多出了两个文件:appalicationContext.xmldispatcher-servlet.xml ,暂且放置,后文进行配置。

四、完善目录结构

1、首先我们在src/main文件夹下创建java文件夹、resources文件夹,完整的目录结的如图所示:

 

2、设置 /src/main/java 等目录为 IDEA 能识别的 Maven目录

五、基于XML 的配置

1、配置 web.xml

如下图所示,IDEA会自动往 web.xml 里面,添加了 Spring MVC 的相关配置:

特别注意】上面的dispather,其实是省略掉了默认的<init-param>节点配置,即:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

 

2、配置 dispatcher-servlet.xml,这个文件主要负责mvc的配置。

<?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:p="http://www.springframework.org/schema/p"
       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd 
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
">

    <!-- 开启Spring注解的“自动装配”和“自动检测”的功能 -->
    <context:component-scan base-package="com.jianbao"/>

    <!-- 配置注解驱动,可以把request参数绑定到controller参数上 -->
    <mvc:annotation-driven/>

    <!--静态资源映射-->
    <!--【特别注意】我们可以把静态资源放在webapp的statics目录下,不过需要做一下资源映射-->
    <mvc:resources mapping="/css/**" location="/WEB-INF/statics/css/"/>
    <mvc:resources mapping="/js/**" location="/WEB-INF/statics/js/"/>
    <mvc:resources mapping="/image/**" location="/WEB-INF/statics/image/"/>
    
</beans>

六、测试

1、在controller文件夹下创建一个IndexController,代码如下:

package com.jianbao.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/home")
public class IndexController {
    @GetMapping("/index")
    public void index() {
        System.out.println("Hello, Spring MVC ...");
    }
}

2、接下来,配置Tomcat运行就行。输入:http://localhost:8080/home/index

IDEA如何配置Tomcat,可参考文章:JFinal入门教程:IDEA(2018.2) + Maven(3.5) + Tomcat(9.0) + JFinal(3.5)

IDEA的控制台面板输出:

Hello, Spring MVC ...

 

 

参考:

https://blog.csdn.net/shengshengshiwo/article/details/82979324

https://blog.csdn.net/CQUWEL/article/details/59495083

https://blog.csdn.net/u014377655/article/details/81332959

http://www.cnblogs.com/parryyang/p/5783399.html

https://blog.csdn.net/xiao__gui/article/details/46803193

https://blog.csdn.net/sbjiesbjie/article/details/53264340