Maven快速搭建spring+springMVC项目

1.搭建环境

idea

jdk 1.8

maven 3.5.4

tomcat 8

2.创建项目

首先选择maven项目,输入groupId和artifactId,创建一个空的项目

Maven快速搭建spring+springMVC项目
Maven快速搭建spring+springMVC项目
Maven快速搭建spring+springMVC项目

3.配置之前

首先在pom.xml中添加packaging为war,指明项目打包之后是一个war包,然后添加依赖,现在只需要添加spring-webmvc的依赖即可

<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
</dependencies>

因为是java web项目,idea创建项目后并没有创建web.xml文件,所以现在需要自己手动添加。

鼠标右击项目名,选择Open Module Settings,选择web之后,先创建webapp的目录

Maven快速搭建spring+springMVC项目
Maven快速搭建spring+springMVC项目

创建目录之后,再新建一个web.xml,将此文件的存放地址改成刚刚新建的webapp目录下

Maven快速搭建spring+springMVC项目

创建完成之后即可在main->webapp->WEB-INF下看到web.xml文件

4.开始配置

这时候就需要开始写spring和springMVC的配置文件了,首先右键resources文件夹,可以看到XML Configuration File之中有个Spring Config的选项,这个是能够创建spring的相关配置的配置文件。

Maven快速搭建spring+springMVC项目

因为在引入依赖的时候,spring-webmvc依赖了spring-context,所以idea就会多了这个选项(IDEA天下第一),如果没有这个选项的话,就需要看是否正确的引入了这些包

Maven快速搭建spring+springMVC项目

我们在resources中创建两个文件,分别是applicationContext.xml和spring-mvc.xml,一般来说 applicationContext .xml文件通常用来配置和spring相关的东西,比如扫service、dao等,spring-mvc.xml的话,就用来扫controller。

ps:虽然这些都能用一个文件全部扫到,但分开是为了方便管理,以至于都可以把 applicationContext .xml 再拆分出一个专门扫service和扫dao的两个配置。

在main->java中分别创建controller类和service类

package com.connor.controller;

import com.connor.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController{

    @Autowired
    private HelloService helloService;

    @GetMapping("/hello")
    public String hello() {
        return helloService.sayHello();
    }
}
package com.connor.service;

import org.springframework.stereotype.Service;

@Service
public class HelloService {

public String sayHello(){
return "hello springmvc";
}

}

然后在applicationContext.xml中,添加配置

<context:component-scan base-package="com.connor" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

在spring-mvc.xml中添加如下配置

<context:component-scan base-package="com.connor" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

这两个配置文件的区别的关键之处在于use-default-filters属性。

use-default-filters属性默认值为true,即使用默认的Filter对包进行扫描,而此默认的Filter所扫描的是带有@Controller、@Service、@Repository注解的bean,又因为spring-mvc只用来扫Controller,所以在spring-mvc.xml中,将 use-default-filters属性设为false,但在其中声明需要用Filter过滤出的类型是注解类型(type="annotation")为Controller的bean,这样就只会扫描出Controller了。同理,在applicationContext.xml中,除去注解为Controller,就只会去扫Service和Repository了。

接下来,在web.xml中将这两个配置文件引入

<!-- spring配置 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- springMVC配置 -->
<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:spring-mvc.xml</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

5.测试效果

将此项目部署到tomcat中,启动项目,输入地址localhost:8080/hello,即可得到helloService返回的值

Maven快速搭建spring+springMVC项目

在此之后,将继续更新引入视图控制器和Mybatis框架,使其成为一个完整的SSM框架