轉載請註明出處:http://www.cnblogs.com/mybest/p/4265872.html html
一、 環境配置java
a) Java 1.7web
b) Eclipse lunaspring
c) Maven3.2.5apache
d) Spring 4.1.4瀏覽器
二、 建立maven工程spring-mvc
a) 打開eclipse,file->new->project->Maven->Maven Projecttomcat
b) 下一步mvc
c) 選擇建立的工程爲webapp,下一步app
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,須要手動建立。在下面的步驟中會講到如何補齊這三個目錄。
三、 修改項目基本設置
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的先刪除,再從新建立,缺乏的直接建立。點右鍵操做按鍵進行刪除和添加。
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就能夠看到更改生效了。
四、 Eclipse中maven的配置
a) window->properties->maven,勾選 download repository index updates on startup
五、 簡單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文件內容以下:
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.springstudy</groupId> <artifactId>study</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>study Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.1.4.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies> <build> <finalName>study</finalName> </build> </project>
d) 打開src/main/webapp/WEB-INF/web.xml文件,最終修改如以下內容:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="study" version="2.5"> <display-name>Archetype Created Web Application</display-name> <description>sprintMVC環境搭建</description> <!-- 加載Spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/configs/spring-*.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:configs/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>
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-4.1.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.springstudy.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>
六、 將項目發佈到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
e) 瀏覽器打開http://localhost:8080/study/hello,訪問成功!以下圖