接觸現在做的項目有一段時間了,發現現在這個項目每個文件包有很清析,和直接用eclipse直接建立的web項目不一樣,后來在網上找了一些資料,才發現原來我們這個項目是采用了maven這個插件來建立web項目的。下面就介紹一種采用maven插件建立web項目的方法。
一、web項目需要的運行環境:
1、java version “1.6.0_18″
2、Maven v3.0.4(直接在網上下載,地址:http://maven .apache.org/download.html)
3、Eclipse 3.7
4、Struts2.3.4.1
5、tomcat 6.0
二、安裝maven插件
1、直接將下截獲下來的maven插件解壓到你想要的安裝目錄,這里我選擇的是:F:\apache-maven-2.2.1
2、配置環境變量:在系統環境變量中加入M2_HOME 變量,變量值為:F:\apache-maven-2.2.1,注意用分號分隔。
3、打開cmd命令框,輸入 : mvn -v 確定是否安裝成功。

三、maven構建web工程
1打開cmd 命令框,進入工程目錄,我的目錄為:G:\MywebProject。
2.輸入命令行:mvn archetype:create -DgroupId=com.micmiu.struts2.demo -DartifactId=Hao
-DarchetypeArtifactId=maven-archetype-webapp - DinteractiveMode=false
“Hao”為項目名稱。

運行后的效果如上圖如示,出現‘BUILD SUCCESSFUL’就表示工程建立成功。這時你就會發現你的工程目錄下多一個文件,這個文件夾就是所建立的maven工程。
四、將maven 導入 eclipse中。
1、打開cmd命令框,進入工程文件夾中,輸入如下命令:
mvn eclipse:eclipse -Dwtpversion=1.0

此時,你進入Hao這個工程文件夾中就會發現,出現了.project 為后綴的文件,這個文件就是eclipse所能識別的工程文件名。
2、打開ecliplse ,選擇:File---Import---Exciting Project into Workspace.將工程導入eclipse。
此時,web項目已建立完畢。接下來,就是web.xml的配置。整個web項目目錄結構如下圖所示:

五、struts2配置
1.在WEB-INF/lib下導入struct 2相關 jar包。commons-fileupload-1.2.2.jar,commons-io-2.0.1.jar,commons-lang-2.5.jar,commons-logging-1.1.1.jar
freemarker-2.3.18.jar,javassist-3.11.0.GA.jar(可選),ognl-3.0.3.jar,struts2-core-2.3.1.jar,xwork-core-2.3.1.jar。

2、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="WebApp_ID" version="2.5">
<display-name>Struct2</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.struts.xml配置
在src/main/resource 下新建一個struts.xml文件,配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="basicstruts2" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
</struts>
至此,struts2已經配置完成。
六、部署項目
1、將web項目發布到tomcat中去,具體過程略。
2、起動tomcat ,在瀏覽器中輸入:http://localhost/Hao/,若能出現index.jsp頁面,則配置成功。
