16 May, 2013

Spring MVC Portlet AJAX call using Resource URL

You can follow below steps to make AJAX call using resource URL

  1. Create Spring MVC portlet 
  2. In view.jsp write below code to generate resource URL    
    • <portlet:resourceURL var='myInfo' id='myInfo' />
  3. Write AJAX call scriptin view.jsp

    <script type="text/javascript">
    loadMyInfo();

    function loadMyInfo(){
        var myInfoUrl = "<%=myInfo%>";
        $.ajax({
            url: myInfoUrl ,
            type: 'GET',
            datatype:'json',
            success: function(data){
                var obj = $.parseJSON(data);
                alert(obj);
            }
        });
    }
    </script>
  4. Go to controller and write method as given below
    @ResourceMapping(value="myInfo")
            public void getMyInformation(ResourceRequest request, ResourceResponse response) throws IOException  {
               
                JSONObject json = JSONFactoryUtil.createJSONObject();
                try{               
                ThemeDisplay themeDisplay  =(ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
                User user = themeDisplay .getUser();
                 response.setCharacterEncoding("UTF-8");
                json.put("firstName",user != null ? user.getFirstName() :"");
                json.put("lastName",user != null ? user.getLastName() :"");
                response.getWriter().write(json.toString());
                }catch (Exception e) {
                }           
            }       
  5. Now deploy your portlet and see the result  ,On load it will make ajax call and render the data

Rate Me:

6 comments:

  1. In your example you use jQuery in the Javascript-Part. Since Liferay 6.0 the AlloyUI-Framework is used.

    It would be useful to show how to use AlloyUI instead of jQuery.

    ReplyDelete
  2. How we can pass a javascript variable in portlet:param,
    I am always getting String value
    when I am passing like below:

    var something ='1';
    var myUrl = '';

    I am always getting 'something' in server side.
    can you please help me on this?

    ReplyDelete
  3. Hi ,

    I am doing for ibm wps 8.0 . please tell me how to send json response back to jsp .
    Here in your example what is class of JSONFactoryUtil and JSONObject.

    ReplyDelete
  4. i am getting response like [object,Object] not getting like [test,Test]
    please can you help me out in this

    ReplyDelete
  5. How i can do it with freemarker?

    I tried this:

    <#assign portlet=JspTaglibs["http://java.sun.com/portlet_2_0"]>



    script type="text/javascript">
    loadMyInfo();

    function loadMyInfo(){
    var myInfoUrl = "${variable}";
    $.ajax({
    url: myInfoUrl ,
    type: 'GET',
    datatype:'json',
    success: function(data){
    alert(data);
    }
    });
    }
    /script>

    But it does not work. As i understand, variable created as EL, and i dont know how i can get it's value from freemarker.

    ReplyDelete

Your Comments on this post

Create Servlet Project as Module

You would have seen that many times we are writing out own servlet in Liferay portal to achieve some specific use case. In DXP, You can st...