主要目的:如何在 eclipse 利用 maven 建立 struts2 並在 eclipse internal runtime Server: Tomcat 作開發環境測試

平台:

eclipse:
Version: Kepler Service Release 2
Build id: 20140224-0627

maven:Maven Integration for Eclipse (Luna and newer) 1.5

tomcat:apache-tomcat-7.0.52_x64

java:jdk1.7.0_45_x64

jboss:JBoss EAP 6.2.0.GA

 

step01:建立一個 struts2-archetype-blank 的 maven 專案

maven_struts_01.png

 

maven_struts_02.png

可以按一下 Add Archetype,建立一個空的 Struts2 專案,相關欄位資訊可以查:http://struts.apache.org/release/2.2.x/docs/struts-2-blank-archetype.html

maven_struts_03.png  

填入此專案相關的資訊

maven_struts_04.png  

目前的 Struts2 專案: guEBS 架構如下:

maven_struts_05.png

 

Step02:轉換成為 Dynamic Web project

選擇此專案 guEBS 按下滑鼠右鍵,選擇 Properties 裡的 Project Facets ,按下 Covert to faceted form...

 

maven_struts_06.png

先將 java 的版本改為 1.7 ,再將 Dynamic Web project 的 check box 打勾,此時下方會出現 "Further configuration available...",Content directory 原本是 WebContent ,修改為 src/main/webapp

maven_struts_07.png

再到 Deployment AssemblyMaven Dependencies 加入,然後按 Apply -> ok。

maven_struts_071.png  

 

Step03:Deploy to Internal runtime Server:Tomcat 測試

執行 Maven build 時, 應該在 target 目前下會產生 guEBS-1.0.0-SNAPSHOT.war,這個檔直接可以佈署在 JBoss 下執行。

maven_struts_08.png

 

也可以佈署在 eclipse 內的 runtime server,這裡 runtime server 使用的是 apache-tomcat-7.0.52_x64

maven_struts_09.png

 

執行時,會出現下列的錯誤訊息:The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory

Messages:     
Unable to compile class for JSP: An error occurred at line: [42] in the generated java file: [D:\wei_workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\guEBS\org\apache\jsp\example\HelloWorld_jsp.java] The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory Stacktrace:

maven_struts_072.png  

The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory 的異常的原因及解決辦法。

原因:這是由於工程的WEB-INF/lib下存在 jsp-api.jar、servlet-api.jar,與Tomcat自帶的jar包衝突造成的。
解決辦法:

從 pom.xml 檔中移除 jsp-api.jar、servlet-api.jar 的 dependency 設定

<!-- 本機 Tomcat 測試時, servlet-api , jsp-api 不要載入
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.4</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.0</version>
            <scope>provided</scope>
        </dependency>
-->

 在測試一次,就沒有問題了,但若是佈署在 JBoss EAP 6.2.0.GA 則不需要在 pom.xml 檔的內文中移除 jsp-api.jar、servlet-api.jar 的 dependency 設定。

maven_struts_10.png

注意:若要重新作 maven build 產生 guEBS-1.0.0-SNAPSHOT.war 檔時,jsp-api.jar、servlet-api.jar 的 dependency 設定要加回來,不然會有錯誤訊息。

maven_struts_11.png  

 

參考:

http://struts.apache.org/release/2.2.x/docs/struts-2-maven-archetypes.html

https://wiki.base22.com/display/btg/How+to+create+a+Maven+web+app+and+deploy+to+Tomcat+-+fast

http://www.avajava.com/tutorials/lessons/how-do-i-deploy-a-maven-web-application-to-tomcat.html

http://developer.51cto.com/art/201205/338496.htm

 

arrow
arrow
    文章標籤
    eclipse maven struts2 tomcat
    全站熱搜

    MIS 發表在 痞客邦 留言(0) 人氣()