1

I'm implementing attendance sheet in S2. I need to select only one checkbox among three (P, A, L). User may select all at once if they want. Finally they will take attendance. All these stuff completed. See the below picture for understanding completely.

enter image description here

After clicks on button I make an Ajax call. So the selected values will be send to action class like P##1,L##2,P##3,L##4,P##5,P##6,A##7,P##8,A##9,P##10. In action class, I'll split it while processing request. Is it right way to pass parameters. Can you please tell me is the any other solution to do this.

In JSP

  <s:iterator value="listOfEmployees">
  <s:property value="%{empCode1}" />
  <s:checkbox name="somename%{empCode}" fieldValue="P##%{empCode}" theme="simple" cssClass="first"/>
  <s:checkbox name="somename%{empCode}" fieldValue="A##%{empCode}" theme="simple" cssClass="second"/>
  <s:checkbox name="somename%{empCode}" fieldValue="L##%{empCode}" theme="simple" cssClass="third"/>
  </s:iterator>

In JS, after clicks on button all the checked values will come. With the below line I got above parameter. 1, 2, etc are Ids (assume) and P - present, etc.

  values+=$(this).val()+",";//now values=P##1,L##2,P##3,L##4,P##5,P##6,A##7,P##8,A##9,P##10

  xmlhttp.open("GET","actionname.action?ids="+values,true);//call action
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31

1 Answers1

2

It's wise to encode your query string before you send it to the server, especially because you have hash signs (#) that will be misinterpreted as fragments.

Here is one possible solution using encodeURIComponent() (which will encode hash signs):

xmlhttp.open("GET","actionname.action?ids="+encodeURIComponent(values), true);

You will then send your parameters like this:

actionname.action?ids=P%23%231%2CL%23%232%2CP%23%233%2CL%23%234%2CP%23%235%2CP%23%236%2CA%23%237%2CP%23%238%2CA%23%239%2CP%23%2310

and on the server side you can urldecode() them like this (in PHP):

<?php
echo urldecode( $_GET["ids"] );

or (in JSP):

URLDecoder.decode(Request.getQueryString(), 'UTF-8')

to get back

P##1,L##2,P##3,L##4,P##5,P##6,A##7,P##8,A##9,P##10

(Note: I'm not familiar with JSP, so here is more detailed information about decoding the querystring: How do I correctly decode unicode parameters passed to a servlet)

Community
  • 1
  • 1
Drakes
  • 23,254
  • 3
  • 51
  • 94
  • Without encode I didn't get complete parameter. After encoding only I got complete parameter what I've. xmlhttp.open("GET","actionname.action?values="+encodeURIComponent(values),true);. There is no need of decode in action class. Thank you... – SatyaTNV Jun 25 '15 at 05:21
  • Ok, wonderful. I wasn't sure how JSP handles encoded parameters. – Drakes Jun 25 '15 at 05:22
  • Without hash tag (fieldValue="P##%{empCode}") is there any other way to make it as simple? – SatyaTNV Jun 25 '15 at 05:47
  • You could change the way you format your qs like so: `?ids=P1-L2-P3-L4-P5-P6-A7-P8` using, say, the hyphen (instead of a comma) as a delimter. In a querystring, letters (A–Z and a–z), numbers (0–9) and the characters '*','-','.' and '_' are left as-is. – Drakes Jun 25 '15 at 05:54
  • If I do like this (P1-L2...) then how to take emp code (1,2, etc) in action class from parameter. – SatyaTNV Jun 25 '15 at 05:56
  • In either case (encoded or not), when you pass a series of values in a single parameter (ids), you will have to [split](http://www.tutorialspoint.com/jsp/jstl_function_split.htm) the values on the server to recover the individual values in an array. The other option is to send each value individually like `?id1=P1&id2=L2&...` – Drakes Jun 25 '15 at 05:58
  • It's not possible to pass each value individually. Bcoz emp list will comes from DB dynamically. So we don't know how many of come. – SatyaTNV Jun 25 '15 at 06:08
  • Then it's up to using [split](http://www.tutorialspoint.com/jsp/jstl_function_split.htm). – Drakes Jun 25 '15 at 06:08