IDEA使用maven搭建Spring Web项目

        之前学习spring的时候搭建过一次,结果毕设弄了两个多月,突发奇想,想做一个网页时发现自己又忘记了,再次碰到一堆问题并因为小问题浪费了很多时间,总算明白了还是记录一下比较好,以后也应该避免拖拉。

        首先确定流程:使用maven新建web项目,再添加spring

        1.新建项目

        File→New→Project

            

        选择maven-archetype-webapp

        

        输入GroupId和ArtifactId并点击Next

        

        因为并没有改过配置文件,直接Next

        

        选择项目路径,然后完成创建。

        

        默认项目文件结构如下,根据自己的需要调整为

        

        2.添加spring

        在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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>newWeb</groupId>
  <artifactId>newWeb</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>newWeb Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.0.7.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.7.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.0.7.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.7.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
    <dependency>
      <groupId>org.postgresql</groupId>
      <artifactId>postgresql</artifactId>
      <version>42.2.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-dbcp2</artifactId>
      <version>2.3.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.1</version>
    </dependency>
  </dependencies>

  <build>
    <finalName>newWeb</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.7.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.20.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

        在Project Structure中,将java、resources分别设置为Sources(蓝色)和Resources,这里比较奇怪,我添加了依赖之后被自动设置了,可能是IDEA的功劳。输出路径项目已经默认配置为了target

        

        添加spring配置文件,在resources文件夹中新建newWeb-context.xml

        

        这里给自己记录一下,设置为resources文件夹后,其下面的文件会被输出到输出目录下,不然target下的classes路径下没有newWeb-context.xml,而在web.xml中需要修改默认的applicationContext.xml路径,具体做法为,在web.xml中添加

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:newWeb-context.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>newWeb</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value></param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>newWeb</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

        context-param定义了一个上下文,“classpath:”指明在输出的classes路径下存在着newWeb-context.xml,并且需要在servlet的init-param中引用这个上下文,至此一个空的项目就搭建完了。

        同时记录一下遇到的问题,启动时报错:

                        class path resource [.xml] cannot be opened because it does not exist

        正是由于默认的applicationContext.xml在WEB-INF下,而IDEA将资源文件生成到classes目录下,如果不配置上下文就会导致找不到/WEB_INF/*.xml,因此使用上下文指定applicationContext.xml的位置。另外自己由于对配置文件的不了解,在添加了context-param后没有在servlet中使用init-param添加上下文,导致了另一个问题的出现(按照网上的说法应该是不用额外写init-param,因为context-param改变了全局的位置,而init-param只改变了该servlet,但是确实去掉就会报错。暂时没明白是为什么),这时显示的是:

                        could not open ServletContext resource [/WEB-INF/*.xml]

        添加了init-param即解决问题

        

        

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值