Struts——概覽

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   io   檔案   資料   

Struts是什嗎?

     struts是一種架構。          

         先說之前的設計,用戶端瀏覽器通過容器調用類,類直接重新導向或轉寄請求給頁面;而struts相當於是提供了一種豐富的工具,使類與介面解耦。

          使用Struts的目的是為了協助我們減少在運用MVC設計模型來開發Web應用的時間。如果我們想混合使用Servlets和JSP的優點來建立可擴充的應用,struts是一個不錯的選擇。

Sturts原理?  

 Struts工作原理,如:


             用戶端瀏覽器發出請求,通過設定檔調用到ActionServlet類(已經寫好能滿足基本需求),通過名字我們可以確定它是一個Servlet;這個類讀取struts的設定檔Struts-config.xml;實現ActionForm賦值(賦值原理略過);根據設定檔中的action-mapping找到調用真正幹活的Action類;在struts中Action類調用商務邏輯。

 

        struts工作流程:


          在web應用啟動時就會載入,初始化ActionServlet,ActionServlet從struts-config.xml檔案中讀取配置資訊,把它們存放到各種設定物件中,當ActionServlet接收到一個客戶請求時,將執行如下流程.

        (1)檢索和使用者請求匹配的ActionMapping執行個體,如果不存在就返回請求路徑無效資訊;

(2)如果ActionForm執行個體不存在,就建立一個ActionForm對象,把客戶提交的表單資料儲存到ActionForm對象中;

(3)根據配置資訊決定是否需要表單驗證.如果需要驗證,就調用ActionForm的validate()方法;

(4)如果ActionForm的validate()方法返回null或返回一個不包含ActionMessage的ActuibErrors對象,就表示表單驗證成功;

(5)ActionServlet根據ActionMapping所包含的映射資訊決定將請求轉寄給哪個Action,如果相應的Action執行個體不存在,就先建立這個執行個體,然後調用Action的execute()方法;

(6)Action的execute()方法返回一個ActionForward對象,ActionServlet在把客戶請求轉寄給ActionForward對象指向的JSP組件;

(7)ActionForward對象指向JSP組件產生動態網頁,返回給客戶;

 

Struts怎麼用?

 

web.xml設定檔如下:

<servlet>   <servlet-name>action</servlet-name>   <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>    <init-param>     <param-name>config</param-name>     <param-value>/WEB-INF/struts-config.xml</param-value>    </init-param>    <init-param>     <param-name>debug</param-name>      <param-value>2</param-value>    </init-param>    <init-param>     <param-name>detail</param-name>      <param-value>2</param-value>    </init-param>   <load-on-startup>2</load-on-startup>  </servlet>  <servlet-mapping>   <servlet-name>action</servlet-name>    <url-pattern>*.do</url-pattern>  </servlet-mapping> 


 

Strutsconfig.xml設定檔:

<struts-config><form-beans><form-beanname="loginForm" type="struts.LoginActionForm"/></form-beans> <action-mappings><actionpath="/login"type="struts.LoginAction"name="loginForm"                scope="request"                ><forwardname="success" path="/login_success.jsp" /><forwardname="error" path="/login.jsp"/></action></action-mappings></struts-config>


 

標籤<form-beans>配置ActionForm;action-mapping下配置Action。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.