Multiple Pages Inside One Portlet

本小妞迷上赌 提交于 2019-12-09 13:47:59

问题


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!


回答1:


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.)



来源:https://stackoverflow.com/questions/1407418/multiple-pages-inside-one-portlet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!