Struts2使用IntellijIDEA和maven管理:搭建開發環境

在對spring比較熟悉之後,決心學習一下另一個很流行的框架Struts2,奈何資料比較老舊,也沒有采用maven和intellji配合的教科書,網上的幾個教程說的不是很清楚導致遇到了一些困難,在經過了一天的嘗試後,總結了搭建最基本的開發環境的流程(寫該教程的時候是重新搭建了一遍且運行成功):

訪問我的個人網站獲取更多文章

開發環境搭建

  • Intellji IDEA14
  • maven 3
  • struts 2.5.5

注意:建議將maven的鏡像修改一下,詳情參考我另一篇文章修改intellji的maven鏡像,使用阿里雲的鏡像,一分鐘就可以下載好,否則過程可能要很久

首先,新建一個maven管理的項目
步驟1
填寫項目信息
步驟2
選擇項目名字和路徑
步驟3
這是初始生成的項目結構
步驟4

修改pom文件添加如下依賴,其中在maven中

<build>
<final-name>name</final-name>
</build>

是指在發佈的時候打成名字爲name的war包
步驟5
之後設置項目的結構,添加web支持
步驟6
步驟7
步驟8
步驟9
步驟10
下一步很重要!!如果最後配置完後項目運行不成功,這邊再右鍵操作一下即可
步驟11
這是配置完之後的項目結構
步驟12
之後修改web.xml文件如下(具體內容見文末),對於Struts不同的版本,filter-class的內容是有區別的,讀者可以自行百度或者看官方文檔:
步驟13
在resources目錄下添加struts.xml文件,並且添加如下內容(見文末以及配置的詳細解析)
步驟14
在web目錄下添加index.jsp文件,最終完成的目錄結構就是這樣子
步驟15
配置Jeety容器(tomcat的過程類似)
步驟16
步驟17
步驟18
這裏名字可以不一致的,進行自定義,不定義會採用默認的(比較難記),這個配置決定了將來訪問的url
步驟19
點擊ok就好了,項目已經可以運行了!!(如果有問題如上面講的再去項目結構那個地方右鍵導入一下就好了)
步驟20
接下來,訪問如下網址http://localhost:8080/helloworld/hello.action 就可以看到這個頁面了~

參考文件

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>MyProject</groupId>
    <artifactId>test1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <!--選用最新的版本2.5.5不同版本之後的web.xml文件會有區別 -->
            <version>2.5.5</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>
    <build>
        <!--控制最後生成的war包的名字 -->
        <finalName>helloworld</finalName>
    </build>
</project>

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">
    <filter>
        <filter-name>struts2</filter-name>
        <!--如果是早起的Struts2可能是下面的樣子
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
        -->
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

struts.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

    <!-- 支持動態調用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <!-- 設置開發模式 -->
    <constant name="struts.devMode" value="true"/>

    <package name="default" namespace="/" extends="struts-default">

        <action name="hello">
            <result name="success">/index.jsp</result>
        </action>

    </package>

</struts>

最後,補充一點struts說明(剛學有些地方不一定對,歡迎指正,之後學習深入了我也會訂正的)
而最後生成的項目的訪問路徑是這樣定義的:
http://localhost:8080/webcontext/namespace/actionname.action
webcontext-之前在jetty中的配置
namespace:在struts.xml中定義的package的namespace屬性
actionname:在struts.xml中定義的action的屬性
在namespace正確的情況下,即使namespace和actionname中間多了其他的字符(如http://localhost:8080/helloworld/hh/hello.action) ,也不影響最終頁面的訪問,因爲最新的struts2支持自動的搜索包
所以在我之前的實驗中,因爲沒有設置namespace,所以webcontext後面添加任何字段都不影響最後的結果
因此在一個namespace中的action是不能衝突的,因爲namespace包含了package,所以在package中的action當然也是唯一的

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章