日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Action 3 ActionForm(未整理)

系統 1902 0

JavaBean的子類 可以自動的對輸入進行效驗 Struts標簽也允許任何javabean來填充某個Html組件

1 ActionForm的創建

1 由于ActionForm本身不能實例化 所以必須是ActionForm的子類

2 必須對每個Html控件定義一個公共的屬性

另外 如果想發給Action之前就進行效驗 可以實現validate方法 如果想重置數據可以實現reset方法 這兩個方法都會在ActionForm填充前被調用

void reset ( ActionMapping mapping, javax.servlet.http.HttpServletRequestrequest)
Reset bean properties to their default state, as needed.
void reset ( ActionMapping mapping, javax.servlet.ServletRequestrequest)
Reset all bean properties to their default state.
ActionErrors validate ( ActionMapping mapping, javax.servlet.http.HttpServletRequestrequest)
Validate the properties that have been set for this HTTP request, and return an ActionErrors object that encapsulates any validation errors that have been found.
ActionErrors validate ( ActionMapping mapping, javax.servlet.ServletRequestrequest)
Validate the properties that have been set for this non-HTTP request, and return an ActionErrors object that encapsulates any validation errors that have been found.

例子 Action 1 里的LogonForm.java

public ActionErrorsvalidate(ActionMappingmapping,HttpServletRequestrequest)
... {

ActionErrorserrors
= new ActionErrors();

if ((username == null ) || (username.length() < 1 ))
errors.add(
" username " ,
new ActionError( " error.username.required " ));

if ((password == null ) || (password.length() < 1 ))
errors.add(
" password " ,
new ActionError( " error.password.required " ));

return errors;

}

那么它在struts-config.xml中的映射就比較簡單

< form-beans >
< form-bean name ="logonForm" type ="ergal.LogonForm" />
</ form-beans >

另外還有一個DynaActionForm是ActionForm的子類 它是通過配置文件來定義屬性的

< form-bean name ="myForm"
type
="org.apache.struts.action.ActionForm" >
< form-property name ="name"
type
="java.long.string" />

< form-property name ="password
type="
java.long.string" />
</ form-bean >

這樣不需要編碼來實現JavaBean可以更快更安全

2 ActionForm的主要工作

填充自己的域 數據緩存 數據效驗 類型轉換和對象傳遞

填充自己的域

html表單通過web傳到服務器時是 文本格式 表單里的元素變成了 名字-數值 對 都是字符串 而不是二進制

html上傳文件和附件也都是經過轉化成文本數據 然后再轉化回去的方法

那么Struts是將輸入域轉化成javabean的屬性 當ActionForm的某個屬性和請求中的某個參數名一致時 Struts會自動的將參數值賦給javabean的相應的屬性

數據緩存

ActionForm可以充當HTML控件中缺少的緩存區 在傳給其他的域之前 ActionForm會保存數據 還可以進行簡單的驗證

數據效驗

前面所講的validate方法可以簡單效驗是否符合基本標準 如果不對可以全部返回 讓用戶修改 也就時效驗類型是否符合真正的業務層的需求

類型轉換

可以在ActionForm內部進行一些類型轉換 但是ActionForm只能使用string和boolean屬性的值 所以可以附加一些函數來對數據進行轉換

簡單的例子

public StringkeyDisplay = null ;
public StringgetKeyDisplay()
... {
return keyDisplay;
}

public IntegergetKey()
... {
return new Integer(getKeyDisplay());
}

然后業務層就可以使用getKey()方法來得到需要的數據類型

ActionForm可以內含其他bean

Struts標簽和自動填充機制可以支持用點分隔語法來訪問ActionForm中的其他bean

如:

< html:text property ="values.telephoneText"
size
="4"
maxlength
="14" >

可以使用ActionServlet的包裝類ActionServletWrapper來進行保護 這個包裝類里面的

protected ActionServlet servlet
The servlet instance to which we are attached.

受到保護

3 使用ActionForm

1 實現業務邏輯

如果業務層有接口可用 可以實現這個接口 這樣就可以直接傳遞給業務層

2 當內含bean數據對象時 屬性可以用點分隔來引用

3 設置不可改變的數據對象

暫略...

4 設置可改變的數據對象

暫略...

5 使用工廠方法

要在ActionForm中來完成這個轉換過程 如果必須創建數據對象 好方法就是工廠方法

例子:

public AgetA()
... {
Abean
= new A( this .getA1(), this .getA2(), this .getA3())
return bean;
}

還可以傳遞一個存在的數據對象

6 傳遞一個Map對象

首先將一個屬性添加到ActionForm中

private Mapmap = null ;
public void setMap(Mapmap)
... {
this .map = map;
}

public MapgetMap()
... {
return this .map;
}

然后添加一個屬性來訪問Map

public void setValue(Stringkey,Objectvalue) throws Exception
... {
getMap().put(key,value);
}

public ObjectgetValue(Stringkey) throws Exception
... {
return getMap.get(Key);
}

在Struts標簽中可以用下面方法來訪問Map的元素

< html:text property ="value(key)" />
< bean:write name ="formbean" property ="value(key)" />

Action 3 ActionForm(未整理)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 阿巴嘎旗| 龙口市| 塔河县| 广昌县| 漳浦县| 阿拉善右旗| 出国| 府谷县| 克什克腾旗| 叶城县| 吴堡县| 长宁县| 仙桃市| 太谷县| 阿拉尔市| 班玛县| 广东省| 和平县| 阳高县| 霍城县| 涿鹿县| 江永县| 体育| 建瓯市| 饶平县| 潞城市| 抚顺县| 收藏| 图们市| 普安县| 静乐县| 邯郸市| 开化县| 阜城县| 廊坊市| 西安市| 万年县| 安泽县| 米易县| 晋中市| 晴隆县|