Blogs

Blogs

JSR 286 new features (part 2)--resources

This feature is a very simple and useful in AJAX and JSON like things.  It is a way to get pure response code as you set.

1. TestResourcePortle.java

public class TestResourcePortlet implements Portlet, ResourceServingPortlet {

......

    public void serveResource(ResourceRequest resourceRequest,
            ResourceResponse resourceResponse) throws PortletException,
            IOException {
        PortletRequestDispatcher portletRequestDispatcher = portletConfig
                .getPortletContext().getRequestDispatcher(
                        "/WEB-INF/jsp/resource.jsp");
        portletRequestDispatcher.include(resourceRequest, resourceResponse);
    }

........

}

2. view.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
<%@taglib prefix="portlet" uri="http://java.sun.com/portlet_2_0"%>
<portlet:defineObjects />
<a href="<portlet:resourceURL/>">Resource URL</a></td>

 

3. resources.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>
<%@taglib prefix="portlet" uri="http://java.sun.com/portlet_2_0"%>
<portlet:defineObjects />
portlet resource page
 

I don't understand. It looks like something is missing in the code above.

How does the <portlet:resourceURL/> tag know what resource response class to link to?
A good question!
Ok, JSR-268 utilizes annotation(feature in JDK 1.5). In this sample, it uses the default serveResource() method. If for the common way, it should be like this:

view.jsp

<a href="<portlet:resourceURL><portlet:param name="javax.portlet.action" value="testresource" /></portlet:resourceURL>">Click me to request Resource URL</a>

TestResourcePortle.java
......................................
@Resource(name="testresource")
public void serveResource(ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws PortletException,
IOException {
PortletRequestDispatcher portletRequestDispatcher = portletConfig
.getPortletContext().getRequestDispatcher(
"/WEB-INF/jsp/Resource.jsp");

portletRequestDispatcher.include(resourceRequest, resourceResponse);
}
..........................

Does this make sense? Thanks!