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


2

3

4

5


6

7

8

9

10

11


12

13

14

16

17


18

20

22

23

24

注意,一定要初始化myBean,因為不是非String和本地類型的 類的實體域,默認是為null.如果不初始化在jsp頁面第一次加載時會試圖用ActionForm的值設置表單的值.這時就會有空指針異常.
對應的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"/>
這樣實際會調用 getMyBean().setName(value),或getMyBean().getName().
這樣對于圖片按鈕我們就可以用一個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();
判斷按鈕是否被按下.