Serving Static and Dynamic Resources in JSR 286
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>
This code snippet did not seem to work, Dynamic Link leads to an empty web page with nothing on it and a familiar very complex URL.
Vivekanand Kurdikeri
12 May 10 at 1:13 pm
It could be a bug in your Portal server implementation. We used Liferay, and it worked perfectly.
Subhash Chandran
12 May 10 at 6:15 pm