论坛首页 Java企业应用论坛

ImageButtonBean作用略谈

浏览 1805 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-11-05  
在开发中,我们经常会出现一个表单有多个提交按钮的现象,对这种情况的处理,我想大家应该都不会陌生,前台的JavaScript控制、Struts中的LookupDispatchAction等,这里介绍另一种方法,通过对 input type=image 元素的操纵完成,多一种选择总是好事。
input type=image元素的简单介绍
创建一个图像控件,该控件单击后将导致表单立即被提交。x 坐标将以控件名称后加 .x 提交,y 坐标将以空间名称后加 .y 提交。任何 value属性都将被忽略。src属性指定了 图片链接地址。做个小实验,当我们将form的method设为get的时候,将会看到提交的地址类似于:webtest.htm?str=&img.x=50&img.y=19,它提交的每次我们鼠标所点击的坐标,而不是该控件的value值。

Struts中的解决办法
在Struts其实给出了相应的解决办法:
org.apache.struts.util.ImageButtonBean
该类即是用来封装所提交的内容的,简单地说,该类有两个属性,x,y(均为String类型)及它们的getter、setter,还有一个比较重要的方法:public boolean isSelected(),即是用来判断提交上来的表单是否为单击该控件而导致的结果。

它的用法与其它Struts中的控件没有什么

下面给出简单的jsp、action、form

xml 代码
 
  1. <html>  
  2.     <head></head>  
  3.     <body>  
  4.         <html:form action="/test.do" method="get">  
  5.             <html:text property="str"></html:text><br>  
  6.             <html:image src="imgs/1.jpg" property="img"></html:image><br>  
  7.             <html:image src="imgs/1.jpg" property="img1"></html:image><br>  
  8.             <html:submit>submit</html:submit><br>  
  9.         </html:form>  
  10.     </body>  
  11. </html>  

Form 代码
 
  1. public class TestForm extends ActionForm {  
  2.     // 对应表单上的两个控件  
  3.     private ImageButtonBean img = new ImageButtonBean();  
  4.   
  5.     private ImageButtonBean img1 = new ImageButtonBean();  
  6.   
  7.     private String str;  
  8.   
  9.     public String getStr() {  
  10.         return str;  
  11.     }  
  12.   
  13.     public void setStr(String str) {  
  14.         this.str = str;  
  15.     }  
  16.   
  17.     public ActionErrors validate(ActionMapping mapping,  
  18.             HttpServletRequest request) {  
  19.         return null;  
  20.     }  
  21.   
  22.     public void reset(ActionMapping mapping, HttpServletRequest request) {  
  23.     }  
  24.   
  25.     public ImageButtonBean getImg() {  
  26.         return img;  
  27.     }  
  28.   
  29.     public void setImg(ImageButtonBean img) {  
  30.         this.img = img;  
  31.     }  
  32.   
  33.     public ImageButtonBean getImg1() {  
  34.         return img1;  
  35.     }  
  36.   
  37.     public void setImg1(ImageButtonBean img1) {  
  38.         this.img1 = img1;  
  39.     }  
  40. }  

Action 代码
 
  1. public class TestAction extends Action {  
  2.     public ActionForward execute(ActionMapping mapping, ActionForm form,  
  3.             HttpServletRequest request, HttpServletResponse response) {  
  4.         TestForm testForm = (TestForm) form;  
  5.         System.out.println(testForm.getImg().isSelected());  
  6.         System.out.println(testForm.getImg1().isSelected());  
  7.         return null;  
  8.     }  
  9. }  

struts-config.xml 代码
 
  1. <form-beans>  
  2.     <form-bean name="testForm" type="com.daniel.form.TestForm" />  
  3.   
  4. </form-beans>  
  5.   
  6. <action-mappings>  
  7.     <action attribute="testForm" name="testForm" path="/test"  
  8.         scope="request" type="com.daniel.action.TestAction" />  
  9. </action-mappings>  
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics