1. 程式人生 > >eclipse配置maven搭建spring環境

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

 

b)         下一步

 

c)         選擇建立的工程為webapp,下一步

 

d)         填寫專案的group id和artifact id。一般情況下,group id寫域名的倒序,artifact id寫專案名稱即可。最後點完成。

 

e)         最初建好後,專案目錄結構如下

 

f)          一般的專案目錄中,在java Resources目錄下,還有src/main/java,src/main/test/java,src/main/test/resources這三個source folder,需要手動建立。在下面的步驟中會講到如何補齊這三個目錄。

3、 修改專案基本設定

a)         右鍵此專案名稱->Properties->Java Build path,點選source標籤。

 

b)         提示 hello/src/main/java (missing)和hello/src/test/java (missing)。一般的專案目錄中,在java Resources目錄下,還會有src/main/test/resources這個source folder。將missing的先刪除,再重新建立,缺少的直接建立。點右鍵操作按鍵進行刪除和新增。在目錄下把index.html刪除,然後重新建一個index.html就不會報錯了。

 

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

 

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

 

e)         再修改一下order and export裡的配置,主要是調整這四個目錄的顯示順序,調為自己喜歡的順序即可

 

f)          接下來再修改project facets,先將java修改為1.7。

 

Dynamic Web Module無法在這裡直接修改為3.0,需要開啟工程目錄下有一個.settings資料夾,開啟org.eclipse.wst.common.project.facet.core.xml,做如下修改:

<installed facet="jst.web" version="3.0"/>

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

4、 Eclipse中maven的配置

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

 

5、 簡單Spring mvc的配置

a)         開啟專案中的pom.xml檔案,並點選Dependencies標籤,點選add新增新的依賴

b)         如果知道依賴的group id和artifact id,可以直接填寫,如果不清楚,可以輸入關鍵字進行查詢,或是到http://search.maven.org網站查詢

 

c)         需要新增的依賴有:spring-webmvc,版本為4.1.4. RELEASE。完整的POM.XML檔案內容如下:

d)         開啟src/main/webapp/WEB-INF/web.xml檔案,最終修改如如下內容:

複製程式碼 複製程式碼
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >


<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
    <!-- 載入Spring配置檔案 -->
         <context-param>
            <param-name>contextConfigLocation</param-name>
            <!-- <param-value>classpath:/configs/spring-*.xml</param-value> --><!--  -->
            <param-value>classpath*:spring-*.xml,
                         classpath*:applicationContext.xml</param-value>
         </context-param>
         <!-- Spring監聽 -->
         <listener>
             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
         </listener>
         <!-- Spring MVC配置 -->
         <servlet>
              <servlet-name>Dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <!-- 自定義spring mvc的配置檔名稱和路徑 -->
              <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>classpath*:spring-servlet.xml</param-value>
              </init-param>
              <load-on-startup>1</load-on-startup>
         </servlet>
         <!-- spring mvc 請求字尾 -->
         <servlet-mapping>
            <servlet-name>Dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
         </servlet-mapping>
         <welcome-file-list>
            <welcome-file>index.jsp</welcome-file>
         </welcome-file-list>
</web-app>
複製程式碼 複製程式碼

 *這裡邊有個點需要注意一下,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,內容如下:

複製程式碼 複製程式碼
<?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:jee="http://www.springframework.org/schema/jee"


         xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"


         xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"


         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-4.0.xsd


                                                   http://www.springframework.org/schema/jee


                                                        http://www.springframework.org/schema/jee/spring-jee-4.1.xsd


                                                        http://www.springframework.org/schema/mvc


                                                   http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd


                                                   http://www.springframework.org/schema/util 


                                                   http://www.springframework.org/schema/util/spring-util-4.1.xsd">


 


        


         <context:annotation-config/>


         <context:component-scan base-package="com.zny.controller" />


         <mvc:annotation-driven />


        


         <mvc:resources mapping="/styles/**" location="/styles/" />


         <mvc:resources mapping="/scripts/**" location="/scripts/" />


         <mvc:resources mapping="/images/**" location="/images/" />


 


         <bean


                   class="org.springframework.web.servlet.view.InternalResourceViewResolver">


                   <property name="prefix" value="/WEB-INF/views/" />


                   <property name="suffix" value=".jsp" />


         </bean>


</beans>
複製程式碼 複製程式碼

g)         建立controller包,在spring-servlet.xml檔案中,<context:component-scan base-package="com.springstudy.controller" />已經指定了路徑

 

h)         在src/main/webapp/WEB-INF目錄下,建立views檔案,在spring-servlet.xml檔案中,<property name="prefix" value="/WEB-INF/views/" />已指定了檢視檔案路徑

 

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檔案,內容如下:

複製程式碼 複製程式碼
<!DOCTYPE html>

<html>

         <head>

                   <meta charset="utf-8">

                   <title>sprint hello</title>

         </head>

         <body>hello ${spring}!

         </body>

</html>
複製程式碼 複製程式碼

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

 

d)         啟動tomcat