圖片按鈕,HTML中的一個(gè)特例.在規(guī)范中說明,瀏覽器會(huì)把圖片按鈕當(dāng)成一個(gè)image map對待.這就意味的不像其它的按鈕會(huì)返回一個(gè)String類型的value值.圖片按鈕會(huì)返回一個(gè)X和Y的坐標(biāo). 像下面:
myImageButton.x=200
myImageButton.y=300
在Struts中,對于很多控件,我們可以創(chuàng)建在ActionForm中一個(gè)簡單String的屬性.但對圖片按鈕不行.因?yàn)閳D片按鈕提交的不在是簡單的 name=value的形式.
幸運(yùn)的事,在Struts中提供了ActionForm嵌套bean的能力.就是說ActionForm中的屬性可以是另外一個(gè)bean的類型.比如我們有一個(gè)MyForm類:


2

3

4

5


6

7

8

9

10

11


12

13

14

16

17


18

20

22

23

24

注意,一定要初始化myBean,因?yàn)椴皇欠?/span>String和本地類型的 類的實(shí)體域,默認(rèn)是為null.如果不初始化在jsp頁面第一次加載時(shí)會(huì)試圖用ActionForm的值設(shè)置表單的值.這時(shí)就會(huì)有空指針異常.
對應(yīng)的MyBean可以是:
class MyBean{
String name;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
}
這樣我們在JSP頁面中表單中可以有:
<html:text property=" myBean.name"/>
這樣實(shí)際會(huì)調(diào)用 getMyBean().setName(value),或getMyBean().getName().
這樣對于圖片按鈕我們就可以用一個(gè)ImageButtonBean的類型(struts中已經(jīng)提供了).在我們的ActionForm中可以寫為:
private ImageButtonBean logonButton = new ImageButtonBean();
private ImageButtonBean cancelButton = new ImageButtonBean();
public void setLogonButton(ImageButtonBean button) {
this.logonButton = button;
}
public ImageButtonBean getLogonButton() {
return this.logonButton;
}
public void setCancelButton(ImageButtonBean button) {
this.cancelButton = button;
}
public ImageButtonBean getCancelButton() {
return this.cancelButton;
}
在Action中可以簡單的用
Boolean selected = ((myForm) form). getCancelButton().isSelecte();
判斷按鈕是否被按下.