ImageButtonBean功用略谈
ImageButtonBean作用略谈在开发中,我们经常会出现一个表单有多个提交按钮的现象,对这种情况的处理,我想大
ImageButtonBean作用略谈
在开发中,我们经常会出现一个表单有多个提交按钮的现象,对这种情况的处理,我想大家应该都不会陌生,前台的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 代码?
- <html>??
- ????<head></head>??
- ????<body>??
- ????????<html:form?action="/test.do"?method="get">??
- ????????????<html:text?property="str"></html:text><br>??
- ????????????<html:image?src="imgs/1.jpg"?property="img"></html:image><br>??
- ????????????<html:image?src="imgs/1.jpg"?property="img1"></html:image><br>??
- ????????????<html:submit>submit</html:submit><br>??
- ????????</html:form>??
- ????</body>??
- </html>??
Form 代码?
- public?class?TestForm?extends?ActionForm?{??
- ????//?对应表单上的两个控件??
- ????private?ImageButtonBean?img?=?new?ImageButtonBean();??
- ??
- ????private?ImageButtonBean?img1?=?new?ImageButtonBean();??
- ??
- ????private?String?str;??
- ??
- ????public?String?getStr()?{??
- ????????return?str;??
- ????}??
- ??
- ????public?void?setStr(String?str)?{??
- ????????this.str?=?str;??
- ????}??
- ??
- ????public?ActionErrors?validate(ActionMapping?mapping,??
- ????????????HttpServletRequest?request)?{??
- ????????return?null;??
- ????}??
- ??
- ????public?void?reset(ActionMapping?mapping,?HttpServletRequest?request)?{??
- ????}??
- ??
- ????public?ImageButtonBean?getImg()?{??
- ????????return?img;??
- ????}??
- ??
- ????public?void?setImg(ImageButtonBean?img)?{??
- ????????this.img?=?img;??
- ????}??
- ??
- ????public?ImageButtonBean?getImg1()?{??
- ????????return?img1;??
- ????}??
- ??
- ????public?void?setImg1(ImageButtonBean?img1)?{??
- ????????this.img1?=?img1;??
- ????}??
- }??
Action 代码?
- public?class?TestAction?extends?Action?{??
- ????public?ActionForward?execute(ActionMapping?mapping,?ActionForm?form,??
- ????????????HttpServletRequest?request,?HttpServletResponse?response)?{??
- ????????TestForm?testForm?=?(TestForm)?form;??
- ????????System.out.println(testForm.getImg().isSelected());??
- ????????System.out.println(testForm.getImg1().isSelected());??
- ????????return?null;??
- ????}??
- }??
struts-config.xml 代码?
- <form-beans>??
- ????<form-bean?name="testForm"?type="com.daniel.form.TestForm"?/>??
- ??
- </form-beans>??
- ??
- <action-mappings>??
- ????<action?attribute="testForm"?name="testForm"?path="/test"??
- ????????scope="request"?type="com.daniel.action.TestAction"?/>??
- </action-mappings>??