• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Struts : Populate checkboxes from Database in JSP

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I) I'm using Struts.I need to populate the checkboxes in JSP page from the database at the time of loading.

II) I'm able to get the value from the database onto the Action class

1. how to forward the value to JSP page
2. how to manipulate the value passed onto JSP in a scriplet and check the checkboxes accordingly.
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. how to forward the value to JSP page



Put the value you got from database into request object in your Action class.
request.setAttribute("<key>","<value>");

2. how to manipulate the value passed onto JSP in a scrip let and check the check boxes accordingly.



get those values you previously stored in request object in JSP using same key which you used to store those values in action class.

check the value in JSP, decide whether check box should be checked or not.

hth,
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can we do the same in the display tag also.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In struts, another option is to use the struts <html:checkbox> or <html:multibox> tag and store the value in a Struts ActionForm. Since the Struts tags are automatically tied to the value of the Actionform property, they will appear as checked or unchecked based on the value of the ActionForm property. Example:

in your ActionForm that forwards to the JSP:

In the JSP:

The above will show as checked if x is true and unchecked if x is false.
 
Satish Kumar
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Merrill,
I have a small doubt here...
How is actionForm.setX(x);
goin to give the default value to the bean variable "X" to be set properly to the value it will ultimately teke.
Its not workin with me..

if i am wrong somewhere please tell me...my code is as follows...
in the action class
ViewProformaFB proformaFB = new ViewProformaFB(); // the bean for the form(chargeForm) variables.
proformaFB.setIntDisputeOrValidateCheck(intArrSequenceIDs); //property for the checkbox and "intArrSequenceIDs" has the values to be set into the property "X" according to your code.

Obviously this is not working for me. So where did i go wrong?
and what exactly should i correct to make it work.

and my jsp is having:
<html-el:multibox name="chargeForm" property="intDisputeOrValidateCheck"
value="${line.intUnbilledChargeId}" tabindex="9" >
</html-el:multibox>
intUnbilledChargeId has the value i want to set into the jsp...u can treat intUnbilledChargeId and intArrSequenceIDs to be the same.


So as i guess i am wrong at
ViewProformaFB proformaFB = new ViewProformaFB();
as this instance of the form is no way related to the one i am going to have when my jsp is rendered on the screen. So how do i make sure that these instances are the same?
 
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
You are correct in your assumption that the problem is with the statement:

To fix this problem:
  • Make sure you specify ViewProformaFB as the Form bean for this action in the action mapping definition in struts-config.xml.
  • Change the above statement to:
  • Where form represents the ActionForm instance passed into your execute() method as a parameter.
    [ December 10, 2006: Message edited by: Merrill Higginson ]
     
    Satish Kumar
    Ranch Hand
    Posts: 85
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Merrill. It worked.
     
    Satish Kumar
    Ranch Hand
    Posts: 85
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Merrill,
    I am using display tag for displaying the data to the user in the same scenario as above.
    My requirement needs to take input into the checkboxes and the checkboxes are in a column in the display tag.

    After selecting the checkboxes I need to save the status of the checkboxes on a SAVE button that submits the form to the bean and i can get the values from there before re-displaying the page again.
    My problem is that suppose i am on X-page link inside the display tag and do the above procedure to save the data from the bean in some collection...How do i make sure that the same X-page is displayed in the display tag.
     
    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
    Sandeep,

    I'm afraid I'm not going to be much help. I've never used the display tag library, so am not aware of its idiosyncrasies.
     
    Ranch Hand
    Posts: 158
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Sandeep Wagh:
    Hi Merrill,
    I am using display tag for displaying the data to the user in the same scenario as above.
    My requirement needs to take input into the checkboxes and the checkboxes are in a column in the display tag.

    After selecting the checkboxes I need to save the status of the checkboxes on a SAVE button that submits the form to the bean and i can get the values from there before re-displaying the page again.
    My problem is that suppose i am on X-page link inside the display tag and do the above procedure to save the data from the bean in some collection...How do i make sure that the same X-page is displayed in the display tag.



    are you using something like an iterator to display them ? (I haven't used the display tag either, but trying to help if I can )
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic