2

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)

3
  • 1
    Since you have commented //modelMap.addAttribute("reportList", reportList); this , the AJAX response is read in JS and there are no reportList in any of the scope. Commented Jun 17, 2013 at 6:43
  • @TheNewIdiot I dont think so.Read the question carefully.Op is using @ResponseBody due to apply ajax thing and in this case we always return the data neither add it to in modelmap Commented Jun 17, 2013 at 6:47
  • @freak Yes but then EL will never work , JSP is already in place he is just getting the data JSTL will not work in this case because JSTL will be processed at server side. Commented Jun 17, 2013 at 7:01

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I dont want to create it dynamically.Can you please expalin poit 2? I want to load div on the same JSP
If you use @ResponseBody to translate a object to JSON, then you can't use the JSP engine to generate a response. To leverage JSP ELs, you need to keep the action returning a View, and inside the view (JSP file), generate only the fragment that is relevant to your ReportList. Take a look at jQuery's load method: $("#someelement").load("url"), and use it instead of $.ajax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.