Multiple Pages Inside One Portlet
Question
I am curious if anyone knows if it is possible for a single portlet to contain multiple pages, let's say JSP pages. Furthermore is it possible to link to these different pages within the same portlet?
For example. Let's say I have a single portlet. And in this portlet I want the initial view to be a JSP page with just 5 links on it to 5 different JSP pages. And when a user clicked on one of these 5 links, it would load the appropriate JSP page into the portlet.
The end goal would basically be a little mini website all contained inside a portlet.
Now, I understand that this might not be the best use of a portlet, but for the sake of a project I am working on, I still would like to know if it is possible.
Thanks!
Solution
Sure, a portlet can contain more than one JSP.
You can display any JSP you want via the PortletRequestDispatcher in your doView (or doHelp
or doEdit
) method:
protected void doView(RenderRequest req, RenderResponse resp)
throws PortletException, IOException, UnavailableException {
resp.setContentType("text/html");
String myview = req.getParameter("myview");
String view = "/WEB-INF/jsp/" + (myview==null ? "bar" : myview) + ".jsp";
PortletRequestDispatcher dispatcher =
getPortletContext().getRequestDispatcher(view);
dispatcher.include(req, resp);
}
You could use a parameter to set the view. In the JSP with the links, you'd need to use the Portlet API to create/encode the links to the Portlet. For example:
<portlet:renderURL>
<portlet:param name="myview" value="foo"/>
</portlet:renderURL>
(I haven't really kept abreast of JSR286/Portlet 2.0 - this stuff should work with JSR168/Portlet 1.0 - so it is worth checking the new API if you're using it.)