Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

auto populating struts2 checkboxes from database

I want to make a checklist webpage in jsp page. I am using struts2 tags in my jsp page. I want when my jsp first loaded the check box should automatically populated based on the value which comes from database. And when a user manually check or uncheck the check box something should get stored in the database, so that when another user access the same URL he can see the same state of check box. I don't want to use submit button. Please help me how to implement it....!!! Thanks in advance

like image 468
monu dwivedi Avatar asked Oct 18 '14 18:10

monu dwivedi


1 Answers

Create String variables in your Struts2 Action files and also getters and setters. Set these variables in the execute() method (or the method you are using in your action class) with true or false values fetched from the database. Then you can access them like this

<s:property value="varname"/>

You can set variables in your jsp like this

<s:set var="varname" value="varname"/>

Then check checkboxes like this

<input type="checkbox" <s:if test="#varname == 'true'">checked="checked"</s:if>/>

To set the values back to the database without submitting the form you will have to make an ajax call. Check out JQuery ajax() function. You will have to create a url with the parameters and values that will be mapped onto your corresponding variables in your action class. You can use these values in your action you specified in the url to write them to the database

like image 54
steven35 Avatar answered Oct 24 '22 06:10

steven35