my dog learned polymorphism*
The moose likes Struts and the fly likes how to code for checkboxes on forms? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to code for checkboxes on forms?" Watch "how to code for checkboxes on forms?" New topic
Author

how to code for checkboxes on forms?

Liz Brown
Ranch Hand

Joined: Oct 22, 2002
Posts: 112
Hi,
I have 2 check boxes (not in group). I want to pass 'Y' for check and "" or null for not checked.
In bean I have String return for each get set property.
How to code for this situation?
Hemanth Pallavajula
Ranch Hand

Joined: Oct 07, 2004
Posts: 38
hi,

You can create two hidden fields for each of the checkboxes. For the onSelect() event of the checkbox, set the value for the corresponding hidden field as "Y". By the way, the default value of the hidden fields is "" so that, when the checkbox is not selected, you still get "" rather than nothing. The names of the hidden fields will be the one, given in the form bean.

With Best wishes,
[ March 10, 2005: Message edited by: Hemanth Pallavajula ]

Cheers,<br />Hemanth...<br />(When opportunity doesn't knock, build a door.)
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
You can also code <html:checkbox property="myProperty" value="Y" />
and then in the form's reset() method, code setMyProperty(""). This will have the same effect of putting "Y" in the field if it is checked, and "" in the field if it is unchecked.


Merrill
Consultant, Sima Solutions
Liz Brown
Ranch Hand

Joined: Oct 22, 2002
Posts: 112
So on my jsp I have:

<html:checkbox property="importedCheck" value=""/></STRONG>
ValidataResources"/><BR>
<html:hidden property="importedCheck"/>

In my form I have:

private String importedCheck;

public String getImportedCheck(){
return importedCheck;
}
public void setImportedCheck(String value){
importedCheck=value;
}

In my action i have:
String myimportedcheckreq=(String)request.getAttribute("importedCheck");
System.out.println("myimportedcheckreq is:"+ myForm.getImportedCheck());

I am not getting any value in myimportedcheckreq.
Liz Brown
Ranch Hand

Joined: Oct 22, 2002
Posts: 112
is javascript required anywhere on jsp page when we select the checkbox? Please let me know that too.
thanks.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Liz,

Please understand that Hemanth and I have suggested different approaches. The approach suggested by Hemanth DOES require javascript. Mine does not.

If you want to use my approach, here is what you have to do:
  • remove the hidden field. You don't need it.
  • In the <html:checkbox/> tag, replace value="" with value="Y"
  • In your form, create the following method:

  • public void reset(ActionMapping mapping, HttpServletRequest request) {
    setImportedCheck("");
    }
  • In your action, change the assignment of myimportedcheckreq to:

  • String myimportedcheckreq = myForm.getImportedCheck();

    If you do these things, myimportedcheckreq in your action should have the correct value.
    Liz Brown
    Ranch Hand

    Joined: Oct 22, 2002
    Posts: 112
    Thanks. I can get and set value correctly. However, I have still the problem of reseting the checkbox.

    public void reset(ActionMapping mapping, HttpServletRequest request) {
    setImportedCheck("");
    }

    What's the use of passing ActionMapping mapping, HttpServletRequest request?
    can i use a method without it?
    Merrill Higginson
    Ranch Hand

    Joined: Feb 15, 2005
    Posts: 4864
    Liz,

    The reset() method is defined in the superclass (ActionForm), and is called automatically by the Struts ActionServlet when the form is submitted. What we are doing here is overriding the method, so the signature must match that of the method in the superclass, even if we don't happen to need the parameters.
    Liz Brown
    Ranch Hand

    Joined: Oct 22, 2002
    Posts: 112
    what may be other reasons if my reset is not working as it should be?

    I have done according to what you have said.
    Merrill Higginson
    Ranch Hand

    Joined: Feb 15, 2005
    Posts: 4864
    Liz

    I'm afraid I don't understand your question. If as you state, your action is getting the correct values, then your reset() method is working properly. You do realize that you don't have to call the reset method, right? It's called for you by Struts.

    Please explain in greater detail what is going wrong, and I'll help if I can.

    Merrill
    Liz Brown
    Ranch Hand

    Joined: Oct 22, 2002
    Posts: 112
    public void reset(ActionMapping mapping, HttpServletRequest request) {
    System.out.println("inside reset(). set setImportedCheck to empty");

    setImportedCheck("");
    System.out.println("going out of reset");
    }

    I can see the flow comes in reset method.since it prints the statements.
    I am checking the values what I am getting by myForm.getImportedValue() in Action class.

    1st time when user comes on the form, the checkbox is unchecked.
    After submit of this form if user comes back to the form, he still can see the checkmark.
    The problem is that the checkbox in the jsp is not setting to "unchecked" as 1st time (when I go to the form page again after my execute.)

    If my user comes back to myForm I should show him that this checkbox is not checked as 1st time.
    Merrill Higginson
    Ranch Hand

    Joined: Feb 15, 2005
    Posts: 4864
    Liz,

    One of the main advantages of using Struts <html:xxx> tags is that they are controlled by the property in the form bean. You are essentially linking the form element to the data in the ActionForm. In your case, the property importedCheck controls the checkbox. If the property contains "Y" when the form is generated, the checkbox will be checked. If it contains anything else, it will be unchecked.

    Therefore, if you want the checkbox to be unchecked, you must set the property in your Action class.

    Somewhere in the Action class that forwards to this JSP, you should put the statment:

    myForm.setImportedCheck("");

    Then, when the JSP is displayed, the checkbox will be unchecked. Likewise, if you wanted the box to be checked, you would code the statement:

    myForm.setImportedCheck("Y");

    I hope this answers your question.
    sreenath reddy
    Ranch Hand

    Joined: Sep 21, 2003
    Posts: 415
    Hi

    That reset needs to be implemented only if ur form bean is in session scope
    Merrill Higginson
    Ranch Hand

    Joined: Feb 15, 2005
    Posts: 4864
    Perhaps a word about the reset() method is in order here. In the struts architecture, here is the sequence of events when a user fills out a form and presses the submit button:
  • The html request is submitted to the server
  • The Struts ActionServlet is called
  • ActionServlet either instantiates a new ActionForm or retrieves it from the session scope, depending on the scope you defined.
  • ActionServlet invokes the reset() method on the ActionForm
  • ActionServlet populates the properties of ActionForm based on the parameters passed in the request object.
  • ActionServlet calls validate()
  • ActionServlet determines which Action class to call based on the information in struts-config.xml, instantiates the class, and calls it's execute() method
  • The Execute method does it's work and returns an ActionForward object to ActionServlet, which then forwards to the appropriate JSP.


  • The need for the reset() method exists because html check boxes and radio buttons don't return a value if unchecked. That means that if the property in your ActionForm is "Y", and you uncheck the box, there is no parameter passed in the request to cause another value other than "Y" to be placed in the property, so it remains unchanged. This is not what you want.

    To solve this problem, the reset() method allows youto place the value you want in the property if the check box is unchecked. From the sequence of events above, you can see that this will cause the value to be set before the properties are populated. Therefore, if parameter value for this property is not sent, this property will remain in it's unchecked state. If the parameter is sent due to the check box being checked, the property will have it's checked value.

    If the ActionForm is in request scope, the ActionForm will be instantiated by ActionServlet each time a request is submitted. However, if the check box is unchecked, the setter for the property will not get called, and the property will be null. Since the desired state for unchecked is "", you still need the reset() method even in request scope.

    I hope this clarifies things.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: how to code for checkboxes on forms?
     
    Similar Threads
    jsp code wont run
    comparing elements in one hash set to the elements in another
    int
    How to know that my system have Internet connectivity through JAVA
    How to monitor sim card is changed?