IDEA使用maven搭建Spring Web项目

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

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

        1.新建项目

        File→New→Project

            IDEA使用maven搭建Spring Web项目_第1张图片

        选择maven-archetype-webapp

        IDEA使用maven搭建Spring Web项目_第2张图片

        输入GroupId和ArtifactId并点击Next

        IDEA使用maven搭建Spring Web项目_第3张图片

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

        IDEA使用maven搭建Spring Web项目_第4张图片

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

        IDEA使用maven搭建Spring Web项目_第5张图片

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

        IDEA使用maven搭建Spring Web项目_第6张图片

        2.添加spring

        在pom.xml中添加依赖




  4.0.0

  newWeb
  newWeb
  1.0-SNAPSHOT
  war

  newWeb Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.11
      test
    
    
    
      org.springframework
      spring-context
      5.0.7.RELEASE
    
    
    
      org.springframework
      spring-jdbc
      5.0.7.RELEASE
    
    
    
      org.springframework
      spring-web
      5.0.7.RELEASE
    
    
    
      org.springframework
      spring-webmvc
      5.0.7.RELEASE
    
    
    
      org.postgresql
      postgresql
      42.2.2
    
    
    
      javax.servlet
      javax.servlet-api
      4.0.1
      provided
    
    
    
      org.apache.commons
      commons-dbcp2
      2.3.0
    
    
    
      org.aspectj
      aspectjweaver
      1.9.1
    
  

  
    newWeb
    
      
        
          maven-clean-plugin
          3.0.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.7.0
        
        
          maven-surefire-plugin
          2.20.1
        
        
          maven-war-plugin
          3.2.0
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
      
    
  

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

        IDEA使用maven搭建Spring Web项目_第7张图片

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

        IDEA使用maven搭建Spring Web项目_第8张图片

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

  
    contextConfigLocation
    classpath:newWeb-context.xml
  
  
    org.springframework.web.context.ContextLoaderListener
  

  
    newWeb
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      
    
    2
  
  
    newWeb
    /
  

        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即解决问题

        

        

 

你可能感兴趣的:(开发杂项)