Here is modified code for CheckBoxTag. Maybe it could be usefull to other developpers
as
it enables unchecking checkboxes without having to write some reset() method for
ActionForms.
I worked on Struts 1.0 version of CheckBoxTag, what i modified :
- added indexed tags support
- added generation of hidden field
Here is my new version for doStartTag() method :
public int doStartTag() throws JspException {
// Create an appropriate "hidden" element to put it before the "input" element
// with value false. This will force to uncheck in the ActionForm if the checkbox
// is not checked
StringBuffer resultsHidden = new StringBuffer("<input type=\"hidden\"");
// Create an appropriate "input" element based on our parameters
StringBuffer results = new StringBuffer("<input type=\"checkbox\"");
resultsHidden.append(" name=\"");
results.append(" name=\"");
// Indexed properties handling
if (indexed != null) {
IterateTag iterateTag =
(IterateTag) findAncestorWithClass(this, IterateTag.class);
if (iterateTag == null) {
// this tag should only be nested in iteratetag, if it's not, throw
exception
JspException e =
new JspException(messages.getMessage("indexed.noEnclosingIterate"));
RequestUtils.saveException(pageContext, e);
throw e;
}
if (name != null) {
results.append(name);
resultsHidden.append(name);
}
results.append("[");
results.append(iterateTag.getIndex());
results.append("]");
resultsHidden.append("[");
resultsHidden.append(iterateTag.getIndex());
resultsHidden.append("]");
if (name != null) {
results.append(".");
resultsHidden.append(".");
}
}
// End of Indexed properties handling
results.append(this.property);
results.append("\"");
resultsHidden.append(this.property);
resultsHidden.append("\" value=\"false\" />");
if (accesskey != null) {
results.append(" accesskey=\"");
results.append(accesskey);
results.append("\"");
}
if (tabindex != null) {
results.append(" tabindex=\"");
results.append(tabindex);
results.append("\"");
}
results.append(" value=\"");
if (value == null)
results.append("on");
else
results.append(value);
results.append("\"");
Object result = RequestUtils.lookup(pageContext, name, property, null);
if (result == null)
result = "";
if (!(result instanceof String))
result = result.toString();
String checked = (String) result;
if (checked.equalsIgnoreCase("true")
|| checked.equalsIgnoreCase("yes")
|| checked.equalsIgnoreCase("on"))
results.append(" checked=\"checked\"");
results.append(prepareEventHandlers());
results.append(prepareStyles());
results.append(">");
// Print this field to our output writer
ResponseUtils.write(pageContext, results.toString() + resultsHidden.toString());
// Continue processing this page
this.text = null;
return (EVAL_BODY_TAG);
}
----- Original Message -----
From: "Barry L. White" <[EMAIL PROTECTED]>
Date: Thursday, December 20, 2001 6:31 am
Subject: Re: Checkbox's question.
> BONNET Francois-Xavier wrote:
>
> >You don't even need javascript :
> >I have modified checkboxTag to generate a hidden field with the same name that the
> >checkbox and value false. As Struts BeanUtils.populate(...) method takes first
> value
> >found in the request to populate the field in the ActionForm, i just had to put the
> >hidden field after the checkbox.
> >It seems to work fine, no javascript and other developpers on my project do not
> have to
> >worry about it anymore.
> >
> >Francois-Xavier Bonnet
> >
> >----- Original Message -----
> >From: Dan Cancro <[EMAIL PROTECTED]>
> >Date: Monday, December 17, 2001 6:41 pm
> >Subject: RE: Checkbox's question.
> >
> >>I was just thinking about this problem, too. I think this
> >>checkbox-in-request-only-when-checked anomaly should be handled with
> >>javascript like this:
> >>
> >>The checkbox jsp tag should create a hidden field named after the property,
> >>and a checkbox with some other name. When the checkbox is checked or
> >>unchecked, it should change the value of the hidden field with a generated
> >>line of javascript.
> >>
> >>This way, the controller doesn't have to know that the field is rendered as
> >>a checkbox (and do this special checkbox jig), and the field's value is only
> >>changed when the field exists in the request. So if another jsp doesn't
> >>have the checkbox at all, the value isn't touched.
> >>
> >>Dan
> >>
> >>>-----Original Message-----
> >>>From: Linnea Ahlbeck [mailto:[EMAIL PROTECTED]]
> >>>Sent: Monday, December 17, 2001 10:29 AM
> >>>To: Struts Users Mailing List
> >>>Subject: Re: Checkbox's question.
> >>>
> >>>
> >>>Hi!!
> >>>
> >>>All values from your formbean are sent in the request as
> >>>parameters when you
> >>>press submit - but checkbox values are only in this request
> >>>if they have the
> >>>value on = they are checked. If you use the reset method it
> >>>should work in a
> >>>correct way - values are first set to false by the reset
> >>>method and then,
> >>>if they are present i the request they receive value true,
> >>>otherwise the
> >>>have the value false. /Linn�a
> >>>
> >>>
> >>>
> >>>----- Original Message -----
> >>>From: <[EMAIL PROTECTED]>
> >>>To: <[EMAIL PROTECTED]>
> >>>Sent: Monday, December 17, 2001 7:07 PM
> >>>Subject: RE: Checkbox's question.
> >>>
> >>>
> >>>coincidentally, I was working on the check box today for the
> >>>first time. I
> >>>found the problem too.
> >>>It seems OK when you set the check box "unchecked" as defaut
> >>>(openWindow=null;). but when you set it "checked" as default,
> >>>each time you
> >>>uncheck the box and submit. the value won't change, it still remained
> >>>checked. In fact. the setOpenWindow(..) function has never
> >>>been called when
> >>>the box was unchecked. It is only called when the check box
> >>>is checked.
> >>>Any one knows what went wrong?
> >>>
> >>>Thanks
> >>>
> >>>Yunming Li
> >>>
> >>>-----Original Message-----
> >>>From: Linnea Ahlbeck [mailto:[EMAIL PROTECTED]]
> >>>Sent: Monday, December 17, 2001 11:51 AM
> >>>To: Struts Users Mailing List
> >>>Subject: Re: Checkbox's question.
> >>>
> >>>
> >>>Hi!
> >>>
> >>>Use the reset method in the form bean:
> >>>
> >>>public void reset(ActionMapping mapping, HttpServletRequest request) {
> >>> openWindow= false;
> >>> }
> >>>
> >>>This is a well known problem, see link:
> >>>http://www.jguru.com/faq/view.jsp?EID=471957
> >>>
> >>>Good luck / Linn�a
> >>>
> >>>
> >>>----- Original Message -----
> >>>From: "Hong Xing" <[EMAIL PROTECTED]>
> >>>To: <[EMAIL PROTECTED]>
> >>>Sent: Monday, December 17, 2001 4:05 PM
> >>>Subject: Checkbox's question.
> >>>
> >>>
> >>>Hi all,
> >>>I am a newbie.
> >>>I used <html:checkbox> in my work. But it worked strange. When I
> >>>first view the page and submit the form, the checkbox is OK. Then if I
> >>>view the page again and change the checkbox's state, the
> >>>checkbox is not
> >>>ok. It's always checked. How to fix it?
> >>><html:checkbox property="openWindow" value="on"/>
> >>>
> >>>public void setOpenWindow(String openWindow)
> >>>{
> >>>this.openWindow=openWindow;
> >>>}
> >>>public String getOpenWindow()
> >>>{
> >>>return openWindow;
> >>>}
> >>>private String openWindow="on";
> >>>
> >>>Please help me!!!
> >>>
> >>>Sincerely, Hong Xing
> >>>
> >>>
> >>>==============================
> >>>Bioinformatics Department
> >>>Beijing Genomics Institute
> >>>Beijing Center
> >>>Beijing Airport Industrial Zone B6
> >>>Beijing 101300
> >>>Tel: 0086-10-80494199-3306(Office)
> >>>Email : [EMAIL PROTECTED]
> >>>==============================
> >>>
> >>>
> >>>
> >>>
> >>>--
> >>>To unsubscribe, e-mail:
> >>><mailto:[EMAIL PROTECTED]>
> >>>For additional commands, e-mail:
> >>><mailto:[EMAIL PROTECTED]>
> >>>
> >>>
> >>>
> >>>--
> >>>To unsubscribe, e-mail:
> >>><mailto:[EMAIL PROTECTED]>
> >>>For additional commands, e-mail:
> >>><mailto:[EMAIL PROTECTED]>
> >>>
> >>>
> >>>--
> >>>To unsubscribe, e-mail:
> >>>
> >><mailto:[EMAIL PROTECTED]>
> >>For additional commands, e-mail:
> >><mailto:[EMAIL PROTECTED]>
> >>
> >>--
> >>To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
> >>For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
> >>
> >>
> >
> >
> >--
> >To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
> >For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
> >
> >
> >
> Francois,
>
> Could you post the changes you made to the checkBoxTag to the Struts dev group so it
> can reviewed and possibly added to the next version of the taglibs? Hey, that's
> what open source is all about. :)
>
> Barry
>
>
>
>
> --
> To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
>
>
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>