viki
viki

Reputation: 65

Display list values got by Javascript on JSP page

I have this link on my test.jsp page

<a onclick="treeViewAjax('${searchDummyUrl}/view/${search.DummyNumber}/1')">View</a>


Now when I click on this then treeViewAjax ,A JavaScript method is being invoked as you can see in the link
Here is the treeViewAjax method

function treeViewAjax(Url){

        $.ajax(Url, function(data) {
            alert(data);
        });
    }


and at the same time my Spring controller's searchDummyView method invoked

@RequestMapping(value = "/Dummy/searchDummy/view/{dummyNumber}/{dummyTypeId}", method = RequestMethod.POST)
    public @ResponseBody
     List<Report> searchDummyView(ModelMap modelMap, @PathVariable("dummyNumber") Integer dummyNumber, @PathVariable("dummyTypeId") Integer dummyTypeId) {
        List<Report> reportList = new ArrayList<>();
        reportList.add(dummyService.readReport(dummyNumber, dummyTypeId));
        //modelMap.addAttribute("reportList", reportList);
        return reportList;
    }


Now when as Spring expert's can understand that I have used @ResponseBody annotation as to make to ajaxical request So it then again send response back to the requested URL.Now here again control is on my JS method treeViewAjax and when I alert the data then it show the list values perfectly.
Now I am stuck here that
How can I capture reportList returned by searchDummyView method on JSP page as well as How to iterate/Show its value on the JSP page using EL.

Any suggestions?

Note: I have tried to show reportList like this but it didn't work for me

<c:choose>
            <c:when test="${reportList.size() > 0}">
<c:forEach items="${reportList}" var="list">
                                        //iterations over list but 
                                    </c:forEach>
...

... /> 


It did't display anything from list because of my condition < 0 and I guess it is returning 0 size here OR not accessible here.(could be any reason)

Upvotes: 2

Views: 1471

Answers (1)

AllTooSir
AllTooSir

Reputation: 49372

This code will not work in your case :

<c:choose>
        <c:when test="${reportList.size() > 0}">
        <c:forEach items="${reportList}" var="list">
                     //iterations over list but 
        </c:forEach>
         ...

... /> 

Your JSTL and EL will be processed by the server and sent to the browser . But in your case the JSP is already rendered in the browser and you are firing a AJAX request which will give back some data . You are getting an AJAX response from server , not a full fledged response which the browser will try to render . To create a table with the data of AJAX response I guess there are two options :

  1. Build the table using HTML and javascript dynamically once you get the AJAX response back.

  2. Have a <div> inside your JSP and load another JSP with the table in that AJAX success.

Upvotes: 1

Related Questions