struts2的checkbox传值问题。
要通过action传递checkbox的布尔值时(是布尔值,单个的checkbox,表示此框是否被选中的布尔值),后台Action用一个布尔型变量接收。
在用html的标准checkbox时,传值传的非常纠结,无论前台的checkbox是否被选中,在后台接收到的值总都是true……。
最后找到的解决办法是:用struts的标签<s:checkbox>来代替标准的<checkbox>
<!--
<input type="checkbox" name="isImageNews" id="isImageNews" >
加入到图片新闻
-->
<s:checkbox name="isImageNews" id="isImageNews" >加入到图片新闻</s:checkbox>
<s:checkbox>传递的值总是布尔值。
*****************************************************************************************************
另外,还有要注意的一件事,在用eclipse自动生成set和get函数时,它对布尔型字段所生成的函数可能是不符合struts2 Action的规则的。
比如对于字段 boolean isGood,它生成的函数是:
//get
public boolean isGood() {
return isGood;
}
//set
public void setGood(boolean isGood) {
this.isGood = isGood;
}
而struts2要求的样子是:
//get
public boolean getIsGood() {
return isGood;
}
//set
public void setIsGood(boolean isGood) {
this.isGood = isGood;
}
只有这样子才能被它自动识别。
我由于没注意到这点,导致郁闷了很久。
戒之后人!
个人学习记录……