Struts中checkbox的问题 我也遇到同样的问题,虽然在Action里面自行处理可以解决,但是方法不是很简便,应该在formBean里面可以设置的,等待高手解决。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在JSP中:<html:html><head><title></title><html:base/></head><body bgcolor="white"><html:form action="/wcAction.do" method="post"> <html:text property="mybeanvariable1" /> <html:checkbox property="chec" value="123" /> <html:checkbox property="chec" value="444" /> <html:submit value="提交" /> <html:reset value="重写" /> </html:form></body></html:html>/////////////在FormAction中: private String chec; //myfiledata; public String getChec() { return (this.chec); } public void setChec(String chec) { this.chec=chec; }///////////////////////////////在Action中: //函数定义: public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Extract attributes and parameters we will need Locale locale = getLocale(request); MessageResources messages = getResources(request); HttpSession session = request.getSession(); wcForm m_wcForm = (wcForm) form; String chec=m_wcForm.getChec(); if(chec.equals("123")) { m_wcForm.setChec("123"); } else { m_wcForm.setChec("444"); }................/////////////////////////////可实现两个checkbox的选中一个。如果只定义一个checkbox,会出现楼主说的现象,只能在Action中根据需要进行设置。 你在actionform的类中重载reset方法,将chkCheckbox赋值为null就好了。chkCheckbox = null;注意:如果有多个checkbox控件,struts只递交当前选中的控件。并且赋值为on或者你指定的value。其他没选中的checkbox控件不会产生递交的动作,所以你要在reset方法中清空以前的数据。 public void reset(ActionMapping mapping, HttpServletRequest request) { checkboxInput = new String();}或者:public void reset(ActionMapping mapping, HttpServletRequest request) { checkboxInput = null;} 如果你看了struts的文档知道了,可能是faq里面。you can write initialize(ActionMapping mapping) in you reset method, like: public void reset(ActionMapping mapping, HttpServletRequest request) { initialize(mapping); } //// ///ActionForm: reset public void reset(ActionMapping mapping, HttpServletRequest request) { ; // Default implementation does nothing } ///DynaActionForm: initialize(ActionMapping mapping) public void initialize(ActionMapping mapping) { String name = mapping.getName(); if (name == null) { return; } FormBeanConfig config =mapping.getModuleConfig().findFormBeanConfig(name); if (config == null) { return; } FormPropertyConfig props[] = config.findFormPropertyConfigs(); for (int i = 0; i < props.length; i++) { set(props[i].getName(), props[i].initial()); } } ///DynaActionForm: reset public void reset(ActionMapping mapping, HttpServletRequest request) { ; // Default implementation does nothing } 如果只有一个页面的话这样做没有问题,但是如果页面数据有前页次页的情况就不好处理了。如果清空的话前页的选中项目也清空了,如果不在reset()里面清空的话,前页次页的选中项目只会越来越多,如果把选中的再去掉的话,Action里面是得不到的。 最好不用这个,用multicheckbox,不知道拼错没有。 用multibox 可以实现自动保存的功能?是多页那种的也可以吗? multibox在没有选中的情况下不是也不提交吗 还是用 tyouvivi(tyouvivi) 说的方法把当前页的数据清空其它页的保存就可以了谢谢各位了 请问文本框中只允许输入数字(包括小数点) java在html页面跳转时保存数据 小白:请高手推荐关于jsp+servlet的项目…… 一个关于过滤器的问题一时思路转不过弯....... hibernate 调用存储过程问题~~~急~~~ java新人求助大神。 一个去嵌套javabean的问题 100分紧急求助! 请教J2EE/ejb项目比较合理的开发方式 想做个j2ee范畴的综合一点的项目,给提提建议!:) struts-config.xml大疑问?(散分!!!) struts 国际化问题
<html:html>
<head>
<title></title>
<html:base/>
</head>
<body bgcolor="white">
<html:form action="/wcAction.do" method="post">
<html:text property="mybeanvariable1" />
<html:checkbox property="chec" value="123" />
<html:checkbox property="chec" value="444" />
<html:submit value="提交" />
<html:reset value="重写" />
</html:form>
</body>
</html:html>/////////////
在FormAction中:
private String chec; //myfiledata; public String getChec()
{
return (this.chec);
} public void setChec(String chec)
{
this.chec=chec;
}
///////////////////////////////
在Action中:
//函数定义:
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// Extract attributes and parameters we will need
Locale locale = getLocale(request);
MessageResources messages = getResources(request);
HttpSession session = request.getSession();
wcForm m_wcForm = (wcForm) form;
String chec=m_wcForm.getChec();
if(chec.equals("123"))
{
m_wcForm.setChec("123");
}
else
{ m_wcForm.setChec("444");
}
................
/////////////////////////////
可实现两个checkbox的选中一个。
如果只定义一个checkbox,会出现楼主说的现象,只能在Action中根据需要进行设置。
chkCheckbox = null;
注意:如果有多个checkbox控件,struts只递交当前选中的控件。并且赋值为on或者你指定的value。其他没选中的checkbox控件不会产生递交的动作,所以你要在reset方法中清空以前的数据。
checkboxInput = new String();
}
或者:
public void reset(ActionMapping mapping, HttpServletRequest request) {
checkboxInput = null;
}
you can write initialize(ActionMapping mapping) in you reset method, like:
public void reset(ActionMapping mapping, HttpServletRequest request) {
initialize(mapping);
}
////
///ActionForm: reset
public void reset(ActionMapping mapping, HttpServletRequest request) {
; // Default implementation does nothing
}
///DynaActionForm: initialize(ActionMapping mapping)
public void initialize(ActionMapping mapping) {
String name = mapping.getName();
if (name == null) {
return;
}
FormBeanConfig config =mapping.getModuleConfig().findFormBeanConfig(name);
if (config == null) {
return;
}
FormPropertyConfig props[] = config.findFormPropertyConfigs();
for (int i = 0; i < props.length; i++) {
set(props[i].getName(), props[i].initial());
}
}
///DynaActionForm: reset
public void reset(ActionMapping mapping, HttpServletRequest request) {
; // Default implementation does nothing
}
但是如果页面数据有前页次页的情况就不好处理了。
如果清空的话前页的选中项目也清空了,
如果不在reset()里面清空的话,
前页次页的选中项目只会越来越多,
如果把选中的再去掉的话,
Action里面是得不到的。
不是也不提交吗