IBM WebSphere serveResource Example using dojo v1.8

IBM WebSphere v8.x  serveResource (dynamic resources) Example




This example only covers how to access resources using serverResource and dojo.

1.    Declare javascript variable on portlet jsp page.

    <script type="text/javascript">

        var resourceUrl = "<portlet:resourceURL/>";


    </script>





2.    Write an AJAX javascript method to make service call. You can make it general AJAX call extending/making it dynamic.

    function getUserList(){

        require(["dojo/request/xhr"], function(xhr){
            xhr.get(resourceUrl, {
                handleAs : "text/json",
                preventCache : true,
                query : {
                    "resourceName" : "getUserList"
                }
            }).then(function(data){
                console.log("Service response data =" + data);
                users = data;
                //update user list          
            }, function(error){
                console.log("ERROR occurred while processing getUserList." + error);
            }, function(event){
                //do nothing.
            });
        });

    }

3.    Override serveResource method in portlet class.

     /**
     * Serve Resource to get collection of users.
     * @author Yogesh
     */
    @Override
    public void serveResource(ResourceRequest request, ResourceResponse response)
        throws PortletException, IOException
    {
        super.serveResource(request, response);
        String responseData = null;
        String resourceName =  getRequestParamValue(request, "resourceName");

        if(resourceName != null){

            if(resourceName.equalsIgnoreCase("getUserList")){
                JSONObject jsonObj = new JSONObject();
                jsonObj.put("userId", "1000");
                jsonObj.put("userName", "Yogesh");
                responseData = jsonObj.toString();
            }
        }else{
            System.out.println("No resource name found.");
        }

        response.setContentType("text/json");

        PrintWriter pw = null;

        try {

            pw= response.getWriter();
            pw.write(responseData);
        } catch (Exception e) {
            System.out.println("ERROR thrown. ");
            e.printStackTrace();
        }finally {
            if(pw != null){
                pw.close();  
            }
        }
    }



    public String getRequestParamValue(ResourceRequest request, String paramName){


        String paramValue = null;


        if(paramName != null && request.getParameter(paramName) != null){

            paramValue = (String) request.getParameter(paramName);
        }else{
            //Not found
        }
        return paramValue;
    }

 This is very quickest and small example to implement AJAX call in WebSphere.
 

Comments

Post a Comment

Popular posts from this blog

SVN plugin setup in IBM RAD8 - Checkout project from SVN

How to Upgrade Dojo in IBM WebSphere Portal v8.0

Dojo Enhanced Grid with Checkbox like radio button exmaple