Sunday, September 25, 2011

Spring Portlet MVC - Ajax

I have created an example that shows how you can use @ResourceMapping annotation of Spring Portlet MVC to return JSP content as response to an Ajax request. You can find the details of the example in the following post in Author Online Forum of Portlets in Action:


  1. It's a nice example, but a very limited one. What's really missing is the @RequestBody stuff for mapping JSON to Models... as it turned out, @ResoureseMapping is not Working with @RequestBody.

    Therefore, I ended up doing it by hand using Jackson and the Validator...

  2. Can you show some code examples how u did it.
    As in I wanted to post json data to a controller but it says @RequestBody not supported.

  3. @RenderMapping
    public ModelAndView handleRenderRequest (RenderRequest request, RenderResponse response)throws Exception {
    Map<String, Object> model = new HashMap<String, Object>();
    return new ModelAndView("view", model);

    public void ajaxHandler(ResourceRequest request, ResourceResponse response) {
    String id= request.getParameter("id");
    List<SomeClass> aClassList =aService.findById(Integer.parseInt(id));

    String test= JSONFactoryUtil.serialize(aClassList);


    try {
    } catch (IOException e) {

    and in jsp put:
    <portlet:resourceURL var="getJsonURL" id="ajaxHandler"/>

    function test(){
    type: "POST",
    url: "<%=getJsonURL%>",
    data: { id: 1 }
    }).done(function( msg ) {