• 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

Struts2 Checkbox: How to set value back to Bean

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Y'ALL:

I wonder if anyone can help. I am migrating from Struts-1 to Struts-2, and have problem with Struts-2 Checkbox. The idea is that, I have a List that contains a list of beans (CourseBean). One field inside the CourseBean is "selected", which is a boolean - along with its set/get. When my JSP iterates thru the List to display each CourseBean into rows, each row will have a checkbox where I can check/uncheck. Doing so, I need Struts-2 to call CourseBean.setSelected() to true or false. The problem is, Strut-2 never invokes the setSelected() method, while it does execute the CourseBean.getSelected() because the original value is displayed (true or false).

I know something must be missing in my process because this cannot be a bug in Struts-2. Please HELP, I will appreciate it.

Here is some code snippet:
* BEAN-1 *
public class CourseBean implements Serializable {
private boolean selected;

public boolean getSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}

//.... other get/set
}

* BEAN-2 *
public class ESignBean {
private List<CourseBean> courses = new ArrayList<CourseBean>();

public void setCourses(List<CourseBean> courses) {
this.courses = courses;
}

public List<CourseBean> getCourseList() {
return courses;
}
}

* JSP - I tried two different ways to define the checkbox tag, but none works *
<s:form action="signCourses" method="post">
<table width="100%">
<tr>
<td> </td>
<td><s:text name="label.courseID"/></td>
<td><s:text name="label.courseDesc"/></td>
</tr>
<s:iterator value="#eSignBean.courseList" var="courseBean" status="iterStatus">
<tr>
<td>
<div><s:checkbox theme="simple" name="#eSignBean.courseList[#iterStatus.index].selected" /></div>
<div><s:checkbox theme="simple" name="#courseBean.selected" /></div>
</td>
<td><div><s:property value="%{#courseBean.id}" /></div></td>
<td><div><s:property value="%{#courseBean.description}" /></div></td>
</tr>
</s:iterator>
</table>
<br/>
<s:submit value="%{getText('button.sign')}" align="left" />
</s:form>
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags. Very hard to read otherwise.As far as I know, for this to work you'd need a setCourseList() method in eSignBean.

Aside from that the first thing I'd try is to make the "selected" getter boolean, i.e. isSelected() and see if that does the trick. My guess is that it won't, but it's easy enough to try.

Secondly, calling the property "#courseBean.selected" won't work, because the type conversion process will expect a courseBean action property--and only one at that--which isn't what you intend.

Edit: I think those two issues are the culprit, not the isSelected() method.
 
Nval Btawi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David, thanks a LOT for the reply.

I was still unable to make it to work with the following changes per your suggestion:
1. I modified the eSignBean to have:


2. I modified the courseBean to have (below) and removed the getSelected() method:


3. I kept the JSP to have this checkbox syntac (below) and removed the other checkbox syntax:


NOTE: on item#3 above, that syntax does not return the "isSelected()" value to the JSP to display when I hard-coded the value to "TRUE". While the sytnax I removed returned the value such that the checkbox is "checked".

Anyway, I'm clueless now why this is still not working to change the bean's "selected" value based on clicking/unclicking the checkbox. Also, you said that using the other checkbox syntax using: "#courseBean.selected" won't work unless I do "courseBean action property". I am not quite get what you meant. Could you please point me where I can learn more about what this means and how to do it?

THANKS!
 
Nval Btawi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nval Betawi wrote:Hi Y'ALL:

I wonder if anyone can help. I am migrating from Struts-1 to Struts-2, and have problem with Struts-2 Checkbox. The idea is that, I have a List that contains a list of beans (CourseBean). One field inside the CourseBean is "selected", which is a boolean - along with its set/get. When my JSP iterates thru the List to display each CourseBean into rows, each row will have a checkbox where I can check/uncheck. Doing so, I need Struts-2 to call CourseBean.setSelected() to true or false. The problem is, Strut-2 never invokes the setSelected() method, while it does execute the CourseBean.getSelected() because the original value is displayed (true or false).

I know something must be missing in my process because this cannot be a bug in Struts-2. Please HELP, I will appreciate it.

Here is some code snippet:
* BEAN-1 *


* BEAN-2 *


* JSP - I tried two different ways to define the checkbox tag, but none works *


 
Nval Btawi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UPDATE:
I start to believe this is either LIMITATION or BUGS in Struts2 framework. This is what I found when using Struts 2 checkbox:
1. You can NEVER set the value directly to the bean using the struts2 tag. Instead, you must provide the set/get method in your action class as a wrapper to do it. That set/get methods are the ones to update your bean.
2. If you use iterator to display your checkbox, things become more complicated. Your action's set/get method MUST accept/return an array. Otherwise, struts2 simply ignores it and nothing happen. No exception. Nothing.

Given those, I modified my code as follow:

* JSP *


* Action java *


The code above now works to set the "selected" param inside each course bean that I select via the checkbox.

HOWEVER THE ISSUE now: If I select nothing, struts2 never invokes my Action setSelectedCourseID() because the array is empty. I'm really frustrated by this behavior.

Can someone please give me some insight how to force my beans to be "deselect" if I uncheck all the checkboxes? Struts2 only invokes the Action method if I check something. THANKS!
 
Nval Btawi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UPDATE:
I am using Struts 2.1.6. I goggled around and found that struts2 checkbox had been having few issues and bugs in the past. Some fixes had been submitted as patches prior to the Struts 2.1.6 release. But SERIOUSLY, this is a very simple behavior where user will select nothing, and struts2 better invokes the Action call to update the selected values.

Does someone have any idea that this is not a bug, and there is a way to address it?
 
Nval Btawi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UPDATE:
After further investigation, research and debugging: This is indeed Struts 2 DEFECT for release 2.1.16 (which was one I used). We resolved the issue by modifying the CheckboxInterceptor.java class. Thanks to Tim Stavenger for his idea:

https://issues.apache.org/struts/browse/WW-2802

Hope this helps everyone else out there who have similar issue.
CHEERS Y'ALL!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the information Nval, I came across the same problem recently and your messages really helped me.

Manjit
 
Nval Btawi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manjit,
I'm glad my posting provided some help to anyone who happened to come across.

Cheers.
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nval Btawi,

I am new struts

As you said that

[Post New]posted Sunday, June 21, 2009 9:38:29 AM private message
Quote [Up]
UPDATE:
After further investigation, research and debugging: This is indeed Struts 2 DEFECT for release 2.1.16 (which was one I used). We resolved the issue by modifying the CheckboxInterceptor.java class. Thanks to Tim Stavenger for his idea: "



So should we have to modify our action class similar to CheckboxInterceptor.java

or we have create a new interceptor. or can we handle in Action class itself


i really confused javascript:emoticon(''); can you please guide me
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No he's saying they changed the checkbox interceptor.

The thing is, without also modifying the checkbox list template that wouldn't do anything, unless there was some sort of action-specific configuration, which seems pretty fragile.

The checkbox interceptor works because the single checkbox template adds a hidden form field with an unchecked value to be submitted, the checkbox list template didn't. (In fact the checkbox list template is similar to how checkboxes have *always* worked in HTTP--unchecked checkboxes aren't submitted to the server.)

Check the patches included in the Struts 2 issue; you may just need to change the checkbox template.
 
kajal mukergi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks David,

So you are saying that we have to just add the patch, it will be enough right no need to import new latest jar file.
 
Nval Btawi
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kajal - Dont bang your head against the board. It hurts

First: I would follow David's suggestion to check if Struts2 has the patch or latest release to address the checkbox interceptor issue. Please note that I used Struts 2.1.16 version. This issue has been noticed by Struts2 team, methinks. Follow this URL for the update: https://issues.apache.org/struts/browse/WW-2802.

Second: For quick fix without the patch, I did it my own by modifying the org.apache.struts2.interceptor.CheckboxInterceptor.java. Get this file from the struts source jar, and drop it into your Java IDE with exactly the same package. I commented out the following linecode, which caused the issue in question:



Compile and include this hacked interceptor in your WAR. I went thru my test and didn't encounter any other issue due to this hack.
Good luck.
 
kajal mukergi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank very much Btawi .....

what i did is

in jsp




in java file i have



so when i select 5th and 10th check box and click submit then in action class i am able to retrieve that i have selected 5th and 10th id
and when i don't select any of the check boxes and click submit then i will get key as false;

now it easy for me to.. handle in this way.

and last doubt

Btawi

if i do the modification what you said will i get all the data of that complete row which was checked or only the row id which we selected.

since in my modification i get the row id only again in action class i am making a doa call to get the values for this particular id.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

i am new to struts, and as i have just gone through your all conversation in this thread, i got a little idea about the checkboxes.

i have a problem , hope you people can solve this ....

i am getting the records from database to my jsp, in a tabular format as 1 Record in 1 ROW.
for ex.

>> First ROW Record >> 1. selectbox 2. textfield 3. checkbox 4. checkbox 5. textfield 6. checkbox
>> Second ROW Record >> 1. selectbox 2. textfield 3. checkbox 4. checkbox 5. textfield 6. checkbox

My problem is how can i get the checkbox value checked / unchecked from the database true / false?
How can i save this form with checkbox value checked / unchecked to the database true / false?

hope you people don't mind to provide me the code and helpful tips, as i am new to struts i feel you people will help me this way ASAP.

One more thing, please provide me some example tutorials for struts, so that i'll understand,


-Thanks in Advance.
Kaushal K. Sharma

 
kaushal sharma
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ALL,


Hope you people will have time to give me some solutions.



-Thanks & Regards,
Kaushal K. Sharma



 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nival,

I am facing the same problem. Can you please let me know what change has been done in CheckboxInterceptor.java. I am unable to access the link related to that issue posted.

Advance Thanks for the help.

Thanks & Regards,
G.S.VijayVarma
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please start a new thread; this one is months old. When you do, please be very specific as to your setup and the problem you're having.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have came across the same problem, fixed it by this way....

Bean Classes - 2


InitialAction (Action to populate list of Products)


JSP to display list of Products and with a Checkbox to select each Product:


If you want any other value than products.number, you can alter the hidden tag.
Remember, you must have the productsBean GETTER, SETTER on the Target Action.

SelectedProducts Submission Action:


I couldn't find any other way....
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic