struts1的一些總結


1、配置struts

       * 拷貝struts/lib文件夾下的所有的jar包到WEB-INF/lib

       * 修改web.xml文件,配置ActionServlet(把下面代碼添加到web.xml)

             <!-- Standard Action Servlet Configuration -->

                <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>  //跟文件名struts-config.xml有關,可有可無

                  </init-param>

                  -->

                     <!-- 如果>=0,表示Tomcat在啓動時就初始化ActionServlet -->

<load-on-startup>2</load-on-startup>

               </servlet>

             

             

                <!-- Standard Action Servlet Mapping -->

                <servlet-mapping>

                  <servlet-name>action</servlet-name>

                  <url-pattern>*.do</url-pattern>

                </servlet-mapping>

       * 創建struts-config.xml文件(放到WEB-INF文件夾下)

      

基於strutsweb應用的開發步驟

1,對應用環境進行配置

2,創建web應用的結構,需要將struts應用的jar文件進行部署。

3,在web服務器部署struts

4,配置struts-config.xml文件,配置Actoin

5,寫自定義Action,實體,以及業務類

 不要在Action中進行業務邏輯的處理,業務邏輯應交給專門的Model層去做

在業務邏輯層拋出異常,在Action中捕獲和處理

 

六、strutshtml標籤

 

strutshtml標籤的使用類似於html標籤,但是少有區別,指定類型的方式變成使用不同的標籤,這樣會綁定struts,所以旨在需要時使用。

<html:form method="post" action="/basic-validate/login">

<!--

   strutshtml標籤中的action可以只寫轉到的actionpathstruts會在解析是自動添加需 要的部分 

-->

       <html:text property="userName" />

       <html:password property="password" redisplay="false"/>

        <!--redisplay="false"不進行回寫,只有html:password標籤可用-->

        <html:radio property="hibbos">

       <html:submit value="login" />

</html:form>

 

自動產生HTML文件的內容

使用這樣的標籤,必須使用ActionForm 

 

 

七、消息標籤

 

<bean:message key="errors.username.required">這個標籤可以從指定的資源文件中根據指定的key值來取得可以對應的值,但是需要在

struts-config.xml中進行配置。

配置資源,這些資源可以在ActionMessage中使用,也就是在構造ActionMessage是指定資源文件中的key這樣,在發現校驗錯誤時,就可以

先在資源文件中指定的key的值了。可以使用struts<html:errors>

<html:message><bean:message>標籤都可以顯示指定錯誤的消息。

<struts-config>

    .....

    <message-resources parameter="alan.struts.message.MessageResource" />

    <!--使用message標籤時配置資源文件的位置-->

</struts-config>                       

 

 

DispatchAction

(org.apache.struts.actions.DispatchAction)

DispatchAction類是Action類的子類,他提供了有實現的execute方法。

 

我們寫的自定義Action類,可以繼承DispatchAction類,但不要覆蓋execute方法,可以在自定義類中寫反回值和參數表都與execute方法相同的方

法,可以通過在struts-congfig.xml中爲這個action的配置中添加一個參數,來判斷調哪一個方法,實際上DispatchAction類就是通過反射機制,

通過form中參數調用了自定義Action中的方法,當然這些方法的定義要符合規範,使用繼承DispatchAction類的自定義的Action類,也就會共享同

一的Action路徑。

 

注意:使用繼承DispatchAction類的自定義的Action,只會匹配一個action路徑,只能共享一個ActionForm,如果加上校驗,會產生form表單的參數不一致的情況,會導致校驗無法通過。

例:

public class MyAction extends DispatchAction{

       ActionForward add(ActionForm form,HttpServletRequest request,HttpServletResponse response ActionMapping mapping) throws Exception

        {

                    return mapping.findForward("sucess")                    

        }

}

 

<action path="/add"  type="MyAction" parameter="methodName">

    <!--parameter屬性是和form中隱藏域的名字相對應的-->

    <forward name="sucess" path="/sucess.jsp"/>

</action>

 

<from action="add.do" method="post">

   <input type="hidden" name="methodName" value="add"/>

   <!--

      使用隱藏域爲struts傳遞要調用自定義Action中方法的方法名,是通過與struts-config.xml

      action標籤中的parametername屬性相對應來獲取隱藏域的value

   -->

   <input type="submit" value="submit"/>

</from>

 

LookupDispatchAction

 

(org.apache.struts.actions.LookupDispatchAction)

 

LookupDispatchAction類也是DispatchAction類的子類,他所實現的功能是解決一個表單多種提交問題的

,他是通過使用資源文件,用submit按鈕的value來作爲資源文件中的key所對應的值,通過這個值來找到對用的key,在使用這個key來獲得指定Map中所對應的值,這個值就是要調用的方法名。

 

submitvalue---->MessageResource.properties中的key----->Mapkey對相應的值---->action

 

例:

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

 

<form method="post" action="${pageContext.request.contextPath}/lookup/adddel.do">

       <input type="submit" value="<bean:message key="button.add" />" name="methodName">

        <!--注意name="methodName"是和strut-config.xmlaction標籤中的parameter屬性-->

       <input type="submit" value="<bean:message key="button.delete" />" name="methodName">

</form>

 

MessageResource.properties

 

button.add=add new user

button.delete=delete user

 

注意:在繼承LookupDispatchAction時,要覆蓋getKeyMethodMap()方法,並定義Map,向Map中放入指定的鍵值對。

 

public class AddDelLookupDispatchAction extends LookupDispatchAction

{

       public Map getKeyMethodMap(){

              Map keyMethodMap= new HashMap();

              keyMethodMap.put("button.add", "add");

              keyMethodMap.put("button.delete", "delete");

              return keyMethodMap;

       }    

    public ActionForward add(ActionMapping mapping,ActionForm form,

                   HttpServletRequest request,HttpServletResponse response) throws Exception

    {   

        return mapping.findForward("add");

    }

    public ActionForward delete(ActionMapping mapping,ActionForm form,

                   HttpServletRequest request,HttpServletResponse response) throws Exception

    {

           return mapping.findForward("delete");

    }

}

 

<action   path="/lookup/adddel"  type="alan.struts.actions.AddDelLookupDispatchAction"

                        parameter="methodName">

            <forward name="add" path="/add.jsp"/>

            <forward name="delete" path="/delete.jsp" />

</action>

<message-resources parameter="alan.struts.message.MessageResource" />

 

自定義的Action類的一些規則

1,儘量不要在Action類中使用(靜態)成員變量,如果使用要加上同步。

2,儘量使各模塊間的耦合性降低,最大限度的針對接口編程。

3,可以將共代碼方在覆蓋父類的方法中,最後可以用super.xxx(xxx)來調用父類的方法,使用父類的實現,並加上了自定義的功能。

                         

 

1、動態ActionForm的編寫

       動態ActionForm是爲了避免過多的ActionForm類膨脹而設計的,使用動態ActionForm可以獲得標準ActionForm的所有功能

       * Struts-config.xml配置動態AcitonForm

         <form-beans>

              <form-bean name="dynaActionForm" type="org.apache.struts.action.DynaActionForm" >

                     <form-property name="name" type="java.lang.String"/>  <!-- name屬性要和表單中的name一致 -->

                     <form-property name="age" type="java.lang.Integer"/>  <!-- 必須用包裝類 -->

           </form-bean>

         </form-beans>

        

       * Action中使用動態ActionForm,需要強制轉換爲DynaActionForm類型,同時使用get方法來獲取

           如:

                  DynaActionForm dynaform = (DynaActionForm)form;

                  String name = (String)dynaform.get("name");

                  Integer age = (Integer)dynaform.get("age");

                 

         動態ActionForm其實是把html頁面中元素的值放到了map,所以可以通過get方法取得

         動態ActionForm採用EL表達式的輸出方式: ${dynabean.map.prop}

        

----------------------------------------------------------------------------------------------------------------------

2、採用struts上傳文件

       * 頁面中form的配置,如:<form action="upload.do" method="post" enctype="multipart/form-data">

       * ActionForm中使用FormFile來接收上傳文件,參見:UploadActionForm.java

       * Action中調用FormFile即可取得上傳文件的相關數據,採用流進行輸出,完成上傳,參見:UploadTestAction.java

      

       注意:對文件上傳大小的配置:<controller maxFileSize="10M"/>

 

----------------------------------------------------------------------------------------------------------------------

3、空字段處理

       * 如果html頁面中沒有寫<input>,用jsp腳本接收到的字符串爲null,如果用EL表達式,則爲空串""

       * 如果html頁面中存在<input>,但沒有輸入值,那麼JSP腳本和EL表達式輸出的都是空串

 

----------------------------------------------------------------------------------------------------------------------

4StrutsActionForm類型的自動轉換:

boolean: yes,y,1,trueon這些都可以轉換爲true類型,而且是忽略大小寫的;其它情況會轉換爲false類型

Date類型轉換

       * 如果爲java.sql.Date,頁面的日期類型必須爲yyyy-MM-dd纔可以轉換

       * 如果爲java.util.Date,默認情況下struts無法轉換,要定義自己的converter來轉換

 

自定義轉換器的實現步驟:

       * 實現Converter接口,實現convert方法

       * 將實現的converter註冊,通常情況採用servlet進行註冊(也可以採用struts plugin的方式進行註冊)

       * 注意servlet標籤中的<load-on-startup>標籤的值必須大於等於0

      

StrutsActionForm自動蒐集過程

       * 將頁面數據放到map中,其中mapkey爲頁面中的名稱,map中的value爲頁面中value的值

       * 調用BeanUtils.setProperties方法,將map中的值逐個設置到ActionForm實例上

       * 對於ActionForm中的每一個屬性,根據屬性的類型調用相應的Converter,然後調用Converterconvert方法,將相應的字符串轉換成ActionForm中指定的類型

      

       可以通過BeanUtils.copyProperties(目標對象,源對象) 方法,在ActionForm對象和實體對象之間複製,避免過多的調用get/set方法

 

ActionForward

1、理解全局ActionForward和局部ActionForward的概念

2、理解redirect的使用

   * 設爲true時表示執行重定向操作

   * 設爲false時,表示執行請求轉發操作

 

3、理解動態ActionForward,動態ActionForward可以修改參數

 

4、不能在代碼中更改配置文件的屬性

 

Mapping

  1、在struts-config.xml中。每個<action>對應一個ActionMapping實例

2struts建議最好所有的轉向都由Struts來控制

3、理解ActionMappingforward的含義

4、理解ActionMappingunknow配置的作用

5、瞭解採用jstlstruts html標籤保持頁面數據

 

Struts國際化

      

缺省的Locale是由什麼決定的 (取決於操作系統),Locale是由國家和語言代碼組成的

 

國際化資源文件是由basename + Locale組成的,如:MessagesBundle_zh_CN.properties

    * basename可以是任意合法的字符串

   

native2ascii的位置和使用方法

    * 位置:jdk1.6.0_03/bin/

    * native2ascii 輸入文件(源文件) 輸出文件(目標文件) 如:

        native2ascii.exe o.properties MessageBundle_zh_CN.properties

 

  1struts國際化配置

    * struts-config.xml中加入:

    <message-resources parameter="MessageResources" />

   

2、提供國際化資源文件,並且採用native2ascii.exe將中文轉換爲unicode編碼

 

3、在JSP中使用<bean:message key=""/>讀取國際化消息文本

 

4、利用struts默認把Locale放到session中的特性,採用編程方式手動切換語言的設置

      String lang = request.getParameter("lang");

      Locale currentLocale = null;

      if("zh".equals(lang)) {

          currentLocale = new Locale("zh","CN");

      }

      else if("en".equals(lang)) {

          currentLocale = new Locale("en","US");

      }

      //request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);

      this.setLocale(request, currentLocale);

     

5、如何創建消息?

      區分ActionMessages對象和ActionMessage對象,這兩個對象的意義

          * ActionMessages是一個集合,把ActionMessage對象addActionMessages

            ActionMessages messages = new ActionMessages();

            ActionMessage msg = new ActionMessage(key);<key爲資源屬性文件中的key>

           messages.add(message_id,msg)

           第一個參數表示本ActionMessage對象在ActionMessages對象中區別於其他ActionMessage對象的標識符

 

6、如何傳遞消息?

      調用saveMessages(傳遞普通消息);saveErrors(傳遞錯誤消息)方法

     

7、如何顯示消息?

      通過<html:messages id="msg" message="true" property="error_1"/>標籤來顯示消息

          * (設成message="true"他就會讀messagekey上的值, <既可以顯示普通消息也可以顯示錯誤消息>

      通過<html:errors/>標籤顯示錯誤消息(只能顯示錯誤消息)

 

Struts異常處理

       1、編程式異常

       * 截獲某個異常

       * 創建相應的異常消息

       * 傳遞異常消息

       * 轉到相應的錯誤頁面

   

2<exception path=""/>

    ⑴.key屬性:對應錯誤提示消息文本的key,需要在資源屬性文件中定義

⑵.path屬性和<action-mappings input=""/>input屬性的優先級

            * 既配了input,又配了path之後,以path爲第一優先級

            * 沒有配path,那麼就轉向input所指向的頁面

        ⑶.Type屬性:需要處理哪種類型的Exception

3<html:errors/>標籤在JSP頁面顯示錯誤消息

 

DispatchAction使用

1.     paramter指定的參數不能爲executeperform

2.       瞭解<action>中的parameter屬性

3.       不能覆寫execute方法,如果要覆寫,必須顯式的調用父類的execute方法

4.       瞭解unspecified方法的含義

 

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