• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

How to code for checkboxes on forms?

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Liz Brown
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is javascript required anywhere on jsp page when we select the checkbox? Please let me know that too.
thanks.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 4864
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 4864
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 112
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 4864
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Ranch Hand
    Posts: 415
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi

    That reset needs to be implemented only if ur form bean is in session scope
     
    Merrill Higginson
    Ranch Hand
    Posts: 4864
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Greenhorn
    Posts: 3
    Monad Java ME Postgres Database
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liz Brown wrote: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?


    you have to pass "checked" - but not Y and "" or null !
     
    I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic