How do I render a JSON view/response via AJAX using Spring MVC annotation-based controller for portlets?
Asked Answered
E

5

5

I've spent the last six hours scouring Google and stackoverflow for an answer to this question. I'm originally a PHP developer, so bear with me - returning a JSON array from a PHP controller is trivial.

I'm using Spring MVC 3.0, and I simply want to return a JSON object back to some Javascript from my Spring MVC Controller. It seems that there is no easy way to currently do this using a portlet (https://jira.springsource.org/browse/SPR-7344). Solutions I have seen suggest creating another DispatcherServlet that serves up JSON responses, but I have yet to find a well-documented example of this. If anyone knows a good way to accomplish this (preferably with annotations), please do tell!

Enlargement answered 25/4, 2011 at 22:46 Comment(0)
E
6

I ended up finding a workaround to return "JSON" from a Spring MVC portlet controller. Here's how I did it.

In my controller:

@ResourceMapping("ajaxTest")
public void ajaxHandler(ResourceRequest request, ResourceResponse response)
        throws IOException {
    OutputStream outStream = response.getPortletOutputStream();
    StringBuffer buffer = new StringBuffer();

    Map<String, String> testMap = new HashMap<String, String>();
    testMap.put("foo", "bar");

    String test = new JSONObject(testMap).toString();
    buffer.append(test);

    outStream.write(buffer.toString().getBytes());
}

In "view.jsp":

<portlet:resourceURL var="ajaxtest" id="ajaxTest"/>

<script type="text/javascript">
  $.get('<%= ajaxtest %>', function(response) {
    var json = eval('(' + response + ')');
  });
</script>

Since the @ResourceMapping annotation currently doesn't support returning JSON, I just used org.json.JSONObject to convert my map to a JSON object, and then returned the toString() of this object. The value of @ResourceMapping should match the id of the resourceURL. The use of eval to convert the JSON string to Javascript poses a security risk, but I just included it because it's the simplest example. If you are worried about security, use a JSON parser.

Enlargement answered 27/4, 2011 at 19:2 Comment(0)
O
3

http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/ does not apply to portlets at this time. See: https://jira.springsource.org/browse/SPR-7344

Orlosky answered 17/12, 2011 at 22:35 Comment(0)
E
2

I was able to make it work in Spring 3.0.5 with two simple modifications:

  • Implementing custom WebArgumentResolver to parse an object from JSON from ResourceRequest body
  • Returning MappingJacksonJsonView from my controller's method.

Available in Spring 3.1 only - you may want to use setExtractValueFromSingleKeyModel in your MappingJacksonJsonView

If anybody is in intereset, I can post java code

Exemption answered 23/12, 2011 at 13:43 Comment(0)
R
0

Elaborating on @alex answer:

    @ResourceMapping(value = "showJson")
    public ModelAndView showJson(ResourceRequest request) {
    ModelAndView mav = new ModelAndView(new 
                                   MappingJacksonJsonView());
    mav.addObject("key", myBeanToBeSerializedAsJson);
    return mav;
    }
Recover answered 18/7, 2017 at 15:56 Comment(0)
S
-1

Starting with Spring 3, a controller will automatically convert to json if using ajax get/post with application type of json (i.e. .getJSON and .postJSON in jQuery). This functionality is identical between the servlet and portlet variations of Spring MVC.

Here is a blog post explaining it.

http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/

Simarouba answered 18/7, 2011 at 21:1 Comment(1)
This is not true for portlets : see jira.springsource.org/browse/SPR-7344Clarance

© 2022 - 2024 — McMap. All rights reserved.