indiWiz.com

Subhash's Tech Log

Serving Static and Dynamic Resources in JSR 286

without comments

To serve static content packaged as part of the portlet WAR, we generally take this approach:

<%=response.encodeURL(request.getContextPath()+"/js/project.js")%>

For generating dynamic content what do we do? In JSR 168 this was not possible. Thankfully, JSR 286 introduced this important concept. For creating dynamic content, just override the void serveResource(ResourceRequest request, ResourceResponse response) method in javax.portlet.ResourceServingPortlet (implemented by javax.portlet.GenericPortlet). A simple example:

    @Override
    public void serveResource(ResourceRequest request, ResourceResponse response)
            throws PortletException, IOException {
        String paramValue = request.getParameter("abc");
        System.out.println(paramValue);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/plain");
        PrintWriter pw = response.getWriter();
        pw.println(paramValue);
        pw.close();
    }

To link to this dynamic content:

<portlet:resourceURL var="url">
  <portlet:param name="abc" value="Hello World"/>
</portlet:resourceURL>

<a href="<%=url%>">Dynamic link</a>

Written by Subhash Chandran

February 20th, 2009 at 5:15 pm

Posted in Uncategorized

Tagged with , ,

Leave a Reply