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:

Liferay 7.3 compatibility matrix

Compatibility Matrix Liferay's general policy is to test Liferay Portal CE against newer major releases of operating systems, open s...