File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Using Struts : Populate checkboxes from Database in JSP 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 "Using Struts : Populate checkboxes from Database in JSP " Watch "Using Struts : Populate checkboxes from Database in JSP " New topic
Author

Using Struts : Populate checkboxes from Database in JSP

Ana June
Greenhorn

Joined: Dec 06, 2006
Posts: 1
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.
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

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,


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Satish Kumar
Ranch Hand

Joined: Oct 16, 2006
Posts: 84
can we do the same in the display tag also.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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.


Merrill
Consultant, Sima Solutions
Satish Kumar
Ranch Hand

Joined: Oct 16, 2006
Posts: 84
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

Joined: Feb 15, 2005
Posts: 4864
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

    Joined: Oct 16, 2006
    Posts: 84
    Thanks Merrill. It worked.
    Satish Kumar
    Ranch Hand

    Joined: Oct 16, 2006
    Posts: 84
    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

    Joined: Feb 15, 2005
    Posts: 4864
    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.
    A knibbs
    Ranch Hand

    Joined: Aug 23, 2006
    Posts: 158
    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 )
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Using Struts : Populate checkboxes from Database in JSP
     
    Similar Threads
    custom tag libs pls help very urgent
    Display Tag Issue JSP
    Value Binding at runtime
    checkboxes from a query , How to ?????
    create multiple checkboxes with Data base values in jsf page