structs2 DefaultTypeConverter類型轉換說明,datatypeconverter

來源:互聯網
上載者:User

structs2 DefaultTypeConverter類型轉換說明,datatypeconverter

 1.  建立含有需要類型轉化對象的JSP頁面(input.jsp,裡面表單裡含有自訂類)

<body>

  <h3><font color="red">使用逗號將點的兩個座標分隔開</font></h3>

    <s:form action="Register">

          

<s:textfield name="point" label="point"/>

       <s:textfield name="age" label="age"/>

       <s:textfield name="username" label="username"/>

       <s:textfield name="date" label="birthday"/>

          

<s:submit label="submit"></s:submit>

    </s:form>

    </body>

2.  建立資訊輸出JSP頁面(out.jsp)

<html>

     <head>

        <title>My JSP 'output.jsp' starting page</title>

    </head>

     <body>

        point:<s:property value="point"/><br>

        age:<s:property value="age"/><br>

        username:<s:property value="username"/><br>

        date:<s:property value="date"/><br>

     </body>

</html>

3.       建立上面JSP頁面中需要類型轉換的對象所對應的類(Point.java)

public class Point

{

    private int x;

    private int y;

    //getter、setter方法

}

4.  建立form提交接受的Action(RegisterAction.java)

public class RegisterAction extends ActionSupport

{

    private Point point;

    private int age;

    private String username;

    private Date data;

//getter、setter方法

   

    @Override

    public String execute() throws Exception

    {

       return SUCCESS;

    }

}

5.  Struts.xml中配置RegisterAction

<action name="Register" class="com.test.action.RegisterAction">

<result name="success">/output.jsp</result>

</action>

6.  建立能轉換Point的類(PointConvert.java)

注意:與StrutsTypeConverter方式的轉換不同之處!

public class PointConverter extends DefaultTypeConverter

{

public Object convertValue(Map context, Object value, Class toType)

{

    if(Point.class==toType)

    {

        Point point=new Point();

       

        String[] str=(String[])value;

       

        String[] paramValues=str[0].split(",");

       

        int x=Integer.parseInt(paramValues[0]);

        int y=Integer.parseInt(paramValues[1]);

       

        point.setX(x);

        point.setY(y);

       

        return point;

    }

    if(String.class==toType)

    {

        Point point=(Point)value;

       

        int x=point.getX();

        int y=point.getY();

       

        String result="[x="+x+", y="+y+"]";

       

        return result;

    }

    return null;

}

}

引入參數中的toType表示的是目標類型(是字串類型還是Point類型),

當用戶端string提交到pointConvert這個action時候(即:toType==Point.class),value指的是textfield中填寫的字串。

對if(Point.class==toType)過程解釋:

(1) String[] str=(String[])value;-----------------將value顯示轉化為字元數組後賦值為數組str[];

(2)String[] paramValues=str[0].split(",");-------取出str[]的第一個元素,即:str[0],當textfield裡填寫的是“20,40”,str[0]裡的內容也就是一個字串“20,30”,然後通過str[0].split(“,”)將字串“20,30”以逗號為界限拆分成一個數組並賦值給paramValues[],這時paramValues[]裡內容是{“20”,”30”};

(3) int x=Integer.parseInt (paramValues [0]);-----將字串類型的“20”轉換為integer型賦值給x;

7.       配置屬性檔案使得讓Point屬性由PointConvert處理:

⑴  在和要被convert的類的同目錄下建立一個檔案:

RegisterAction -conversion.properites。

RegisterAction --------要轉換的屬性所在的類的類名

-conversion.properies-----固定格式

⑵填寫屬性檔案內容:

point=com.test.converter.PointConvert

point--------具體要轉換哪一個屬性

com.test.converter.PointConverter-----具體用那個convert類來轉換point

注意:如果對多個屬性進行轉換可以添加多個如point=com.test.converter.PointConvert

形式的內容。 

8. 程式運行流程

運行流程分兩步:一、字串欄位被轉化為對應資料類型欄位上傳到action中;二、對應欄位被轉化為字串類型欄位顯示在輸出頁面;

第一步:input.jsp提交表單---->進入表單提交的action(Register.action),在此action裡接受表單穿過來的各個欄位,是基礎資料型別 (Elementary Data Type)欄位struts2會自動進行對其轉換,如果遇到物件類型欄位則進入此action所在的目錄尋找轉換設定檔(RegisterAction-convertion.properties),在轉換設定檔中尋找處理此對象資料類型的類(PointConverter.java)---->物件類型轉化類將form提交過來的與物件類型相關的字串轉化為對象執行個體->轉化後的對象執行個體被設定(set)到action中;

第二步:action(Register.action)中的眾多欄位裡,如果是基礎資料型別 (Elementary Data Type)欄位的struts2會自動轉換為字串類型欄位發送到輸出頁面(out.jsp), 如果是物件類型欄位則會進入action(Register.action)中找轉換設定檔(RegisterAction-convertion.properties),在轉換設定檔中尋找處理此對象資料類型的類(PointConverter.java)---->物件類型抓換類將action中的物件類型欄位轉換為相應字串欄位---->轉換後的字串執行個體被get到輸出頁面。

9.       定義全域類型轉換

上面所示範的是局部類型轉換方式,假如有四個action,分別為PointAction1、PointAction2、PointAction2、PointAction4,每個PointAction裡面都有一個相同的point屬性需要轉換,所以必須在com.test.action包下面建立四個屬性檔案指定每個PointAction都需要ActionConvert類進行轉換,即:

PointAction1-coversion.properties,PointAction2-coversion.properties, PointAction3-coversion.properties,PointAction4-coversion.properties

如果更多類需要轉換,顯然這種方式比較累人,這種情況可以考慮使用全域類型轉換。

配置步驟:

(1)  在src目錄下建立全域屬性檔案,檔案名稱為:

xwork-conversion.properties 檔案名稱是固定不變的。

(2) 全域屬性檔案內容:

com.test.bean.Point=com.test.converter.PointConverter2

等號左邊是要轉換的類所在的包名,右邊是具體用那個convert類來轉換point。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.