0

I got this loop in a jsp file

<%    for (int i = 0; i < length; i++)
                 {
                    for( int j = 0; j < width; j++)
                    {
                        element = MAP_LIST[j][i];
                        if (element.equals("A"))
                        {} else if (j == width-1 && i == length-1){
                        %>
                        <%=element%><%}
                        else
                        {
                        %>
                        <%=element%>,<%}
                    }
                 } 
%>

which gets me a csv list from an oracle database for my autocomplete text field by using jquery

function Mapsheets(type,nomos)
{   
    $(function() {
        var f_data;

        $.get('/gaec_web/MapSheets.jsp',{'datasrc-select':datasource, 'type_1': type, 'nomos': nomos}, function(data){
            f_data = data.split(',');
        $( "#fx_no" ).autocomplete({
            source: f_data,
            minLength: 2
        });
        });
            });
}

everything works like a charm, i type the first 2 chars and the autocomplete pops up displays every thing as it was supposed to and when I try to pick a value i get the value with several (5) extra spaces in the tail. And then when it gets submitted it fails cause it doesnt match the mapname in question. the results look like this

"     320-197"

So what is causing this? if i run the jsp page alone also get normal results for example

372-146, 376-146, 372-149, 368-149, 376-149, 380-149, 380-152, 376-152, 372-152, 368-152, 368-155, 376-155, 372-155, 380-155, 368-158, 380-158, 376-158, 372-158

thanks in advance

elasticrash
  • 1,181
  • 1
  • 12
  • 30
  • When you do a "view source" on the page when it gets to the browser, what does that list of values look like? Those spaces will not get trimmed for you by just calling "split()" - you have to trim them yourself. – Pointy Jan 17 '11 at 15:49

2 Answers2

2

I never used jsp, but I guess that the reason of the extra spaces is the indentation you use inside the for loop

                   {} else if (j == width-1 && i == length-1){
                    %>
#there are extra space<%=element%><%}
                    else
                    {
                    %>
                    <%=element%>,<%}
                }

you should put the code in this way and maybe the spaces will be ignored:

                   {} else if (j == width-1 && i == length-1){
                    %><%=element%><%}
                    else
                    {
                    %><%=element%>,<%}
                }
javiertoledos
  • 991
  • 11
  • 17
  • btw sorry if this doesn't work but I had a similar problem in php and I resolved it in this way. – javiertoledos Jan 17 '11 at 15:57
  • That's correct. OP is in essence incorrectly using JSP instead of a Servlet for emitting non-HTML data. Since JSP is a template/view technology, all the whitespace outside the *scriptlets* get printed as well. Related question: http://stackoverflow.com/questions/4112686/update-current-page-with-a-servlet – BalusC Jan 17 '11 at 16:00
  • Interesting related question and answer. I just left the office and I am going home. Though out of curiosity I'll boot up the server and see if I am getting the correct results. Basically I got almost no idea about web apps and servelets. I was asked to add autocompletes to an old project and i am just mimicking what I already found. – elasticrash Jan 17 '11 at 16:25
  • silly me yeah that was the problem, tomorrow iam going to look if it worths to convert all services to Servelts. I could trim the space like the guy said above but I wanted to know why it was happening. – elasticrash Jan 17 '11 at 18:00
0

I've never used JSP either. but would it be a viable option for you to output your array as json encoded? jQuery can easily parse an array passed as json_encoded.

I googled for json_encode equivalent for JSP and it gave me this link. What is the JSP equivalent to json_encode ( in PHP )?

in your javascript you then use $.getJSON(url, [data], [callback]) just like you used to do. except for now, the data parameter of the callback function holds a json-decoded array, ergo your previous normal array, but then in javascript-form.

Just provide that data var to the source for the autocomplete.

Community
  • 1
  • 1
Daan Timmer
  • 14,771
  • 6
  • 34
  • 66