1. 程式人生 > >(一)Struts框架

(一)Struts框架

Struts2 是基於MVC的WEB框架

經過六年多的發展,Struts1已經成為了一個高度成熟的框架,不管是穩定性還是可靠性都得到了廣泛的證明。市場佔有率超過20%,擁有豐富的開發人群,幾乎已經成為了事實上的工業標準。但是隨著時間的流逝,技術的進步,Struts1的侷限性也越來越多地暴露出來,並且制約了Struts1的繼續發展。
對於Struts1框架而言,由於與JSP/Servlet耦合非常緊密,因而導致了一些嚴重的問題。首先,Struts1支援的表現層技術單一。由於Struts1出現的年代比較早,那個時候沒有FreeMarker、Velocity等技術,因此它不可能與這些檢視層的模版技術進行整合。其次,Struts1與Servlet API的嚴重耦合,使應用難於測試。最後,Struts1程式碼嚴重依賴於Struts1 API,屬於侵入性框架。
從目前的技術層面上看,出現了許多與Struts1競爭的檢視層框架,比如JSF、Tapestry和spring MVC等。這些框架由於出現的年代比較近,應用了最新的設計理念,同時也從Struts1中吸取了經驗,克服了很多不足。這些框架的出現也促進了Struts的發展。
Struts已經分化成了兩個框架:第一個是在傳統的Struts1的基礎上,融合了另外的一個優秀的Web框架WebWork的Struts2。Struts 2雖然是在Struts1的基礎上發展起來的,但是實質上是以WebWork為核心的。Struts2為傳統的Struts1注入了WebWork的先進的設計理念,統一了Struts1和WebWork兩個框架。

官網下的jar包

開啟看見這是apache和opensymphony的WebWork結合的一個產品

找到struts-2.3.16.3\apps路徑下的struts2-blank.war這個包參考寫程式碼

1參考上面的war包建立Web動態工程匯入相關jar包


參考xml。

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <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>
    </web-app>

然後在src下放一個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>
        <package name="default" namespace="/" extends="struts-default">
            <action name="index">
                <result type="redirectAction">
                    <param name="actionName">HelloWorld</param>
                    <param name="namespace">/example</param>
                </result>
            </action>
        </package>
    </struts>

2開始寫HelloWorld