eclipse配置maven搭建spring环境

1、 环境配置

a)         Java 1.7+

b)         Eclipse luna

c)         Maven3.3.9

d)         Spring 4.3.1

2、 创建maven工程

a)         打开eclipse,file->new->project->Maven->Maven Project

 eclipse配置maven搭建spring环境_第1张图片

b)         下一步

 eclipse配置maven搭建spring环境_第2张图片

c)         选择创建的工程为webapp,下一步

 eclipse配置maven搭建spring环境_第3张图片

d)         填写项目的group id和artifact id。一般情况下,group id写域名的倒序,artifact id写项目名称即可。最后点完成。

 eclipse配置maven搭建spring环境_第4张图片

e)         最初建好后,项目目录结构如下

 eclipse配置maven搭建spring环境_第5张图片

f)          一般的项目目录中,在java Resources目录下,还有src/main/java,src/main/test/java,src/main/test/resources这三个source folder,需要手动创建。在下面的步骤中会讲到如何补齐这三个目录。

 

3、 修改项目基本设置

a)         右键此项目名称->Properties->Java Build path,点击source标签。

 eclipse配置maven搭建spring环境_第6张图片

b)         提示 hello/src/main/java (missing)和hello/src/test/java (missing)。一般的项目目录中,在java Resources目录下,还会有src/main/test/resources这个source folder。将missing的先删除,再重新创建,缺少的直接创建。点右键操作按键进行删除和添加。在目录下把index.html删除,然后重新建一个index.html就不会报错了。

 eclipse配置maven搭建spring环境_第7张图片

c)         修改完整,效果如下图

 eclipse配置maven搭建spring环境_第8张图片

d)         接下来再修改libraries的配置,jre使用1.7版本。选中JRE System Library->edit ,更换版本。

 eclipse配置maven搭建spring环境_第9张图片

e)         再修改一下order and export里的配置,主要是调整这四个目录的显示顺序,调为自己喜欢的顺序即可

 eclipse配置maven搭建spring环境_第10张图片

f)          接下来再修改project facets,先将java修改为1.7。

 eclipse配置maven搭建spring环境_第11张图片

Dynamic Web Module无法在这里直接修改为3.0,需要打开工程目录下有一个.settings文件夹,打开org.eclipse.wst.common.project.facet.core.xml,做如下修改:

 

重启eclipe就可以看到更改生效了。

4、 Eclipse中maven的配置

a)           window->properties->maven,勾选 download repository index updates on startup

 eclipse配置maven搭建spring环境_第12张图片

  

5、 简单Spring mvc的配置

a)         打开项目中的pom.xml文件,并点击Dependencies标签,点击add添加新的依赖

b)         如果知道依赖的group id和artifact id,可以直接填写,如果不清楚,可以输入关键字进行查询,或是到http://search.maven.org网站查询

 eclipse配置maven搭建spring环境_第13张图片

c)         需要添加的依赖有:spring-webmvc,版本为4.1.4. RELEASE。完整的POM.XML文件内容如下:

 

复制代码
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4.0.0
  zky
  hello
  war
  0.0.1-SNAPSHOT
  hello Maven Webapp
  http://maven.apache.org
 
   
      junit
      junit
      3.8.1
      test
   

   
        org.springframework
        spring-context
        4.3.1.RELEASE
   

    
       
            org.springframework  
            spring-beans  
            4.3.1.RELEASE  
            jar  
       
 
  
         
            org.springframework  
            spring-webmvc  
            4.3.1.RELEASE  
            jar  
       
 
  
         
            org.springframework  
            spring-orm  
            4.3.1.RELEASE  
            jar  
       
 
    
    
 
 javax.servlet
 servlet-api
 2.5
 provided
 

 

 
    hello
 

  
复制代码

 

d)         打开src/main/webapp/WEB-INF/web.xml文件,最终修改如如下内容:

 

复制代码
复制代码




  Archetype Created Web Application
  
    
         
            contextConfigLocation
            
            classpath*:spring-*.xml,
                         classpath*:applicationContext.xml
         
         
         
             org.springframework.web.context.ContextLoaderListener
         
         
         
              Dispatcher
              org.springframework.web.servlet.DispatcherServlet
              
              
                  contextConfigLocation
                  classpath*:spring-servlet.xml
              
              1
         
         
         
            Dispatcher
            /
         
         
            index.jsp
         

复制代码
复制代码

 *这里边有个点需要注意一下,maven新建的工程师有src/main/java和src/main/resources,必须得把spring*.xml和applicationContext.xml放在这两个任一目录下才不会报错,别的很多文章是写的放在src目录下,在maven环境中不好使,只能放在src/main/java或src/main/resources下才行。

e)         在Java Resources/scr/main/resources目录下,创建configs文件夹,以便存放在web.xml中声明的配置路径

 

f)          在Java Resources/scr/main/resources/configs目录下,创建spring-servlet.xml,内容如下:

 

复制代码
复制代码



 


        


         


         


         


        


         


         


         


 


         


                   


                   


         


复制代码
复制代码

 

 

g)         创建controller包,在spring-servlet.xml文件中,"com.springstudy.controller" />已经指定了路径

 eclipse配置maven搭建spring环境_第14张图片

h)         在src/main/webapp/WEB-INF目录下,创建views文件,在spring-servlet.xml文件中,"prefix" value="/WEB-INF/views/" />已指定了视图文件路径

 eclipse配置maven搭建spring环境_第15张图片

i)           创建第一个controller文件HelloController.java,完整的文件内容如下:

 

复制代码
复制代码
package com.springstudy.controller;

 

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

 

@Controller

public class HelloController {

 

         @RequestMapping("/hello")

         public ModelAndView hello(){

                   ModelAndView mv =new ModelAndView();

                   mv.addObject("spring", "spring mvc");

                   mv.setViewName("hello");

                   return mv;

         }

}
复制代码
复制代码

 

j)           添加src/main/webapp/WEB-INF/views/hello.jsp文件,内容如下:

 

复制代码
复制代码




         

                   

                   sprint hello

         

         hello ${spring}!

         

复制代码
复制代码

 

6、 将项目发布到tomcat

a)         在eclipse中添加tomcat 7

b)         在tomcat添加完成后,双击,设置overview选项卡中Server Locations的设置。

                         i.              将 Use Tomcat installation(takes control of Tomcat installation)选中

                       ii.              将Deploy path的内容改为:webapps

                      iii.              保存

c)         右键tomcat,Add and Remove… ,添加study

 eclipse配置maven搭建spring环境_第16张图片

d)         启动tomcat

 

e)         浏览器打开http://localhost:8080/study/hello,访问成功!如下图

 eclipse配置maven搭建spring环境_第17张图片

你可能感兴趣的:(eclipse配置maven搭建spring环境)