Portlet必须实现javax.portlet.Portlet接口,不过在实际的开发中,一般都是实现GenericPortlet,因为GenericPortlet已经提供了一些默认的实现方法.
Portlet和容器的关系如下图所示:Portlet运行在Portlet容器中,负责响应portlet容器的request,对不同的request调用不同的方法,然后生成"html片段",portal server负责将不同的portlet生成的"html片段"组合成一个完整的html页面.(图片来源于Portlet in Action)
render:当有需要显示内容的时候,容器会向portlet发render request.
action:当有需要portlet完成某个特定业务(比如更新数据库)的时候,容器会向portlet发action request,每一个action request伴随着一个render request.
resource:当有需要resource请求的时候,容器会向portlet发resource request.
event:当有event请求的时候,容器会向portlet发eventrequest.
request 类型有render和action之分的原因是,对于Portal页面上的每一个portlet,其中某一个portlet发生内容发更新后,所有的其他portlet也都需要重新"render",调用响应render request的方法.因为其他portlet用来做显示的数据源有可能被刚刚做更新的那个portlet更新了,但是其他portlet的响应action request的方法是不用被调用的.
下面通过一个实例来说明render,action request由Portlet的那个方法来响应.
- public class LifeCyclePortlet extends GenericPortlet {
- private String defaultVal;
- public void init() {
- defaultVal = getPortletConfig().getInitParameter("defaultVal");
- System.out.println("******** LifeCyclePortlet init ********");
- }
- public void init(PortletConfig config) throws PortletException {
- System.out.println("******** LifeCyclePortlet init(PortletConfig config) ********");
- super.init(config);
- }
- protected void doEdit(RenderRequest request, RenderResponse response) throws PortletException,
- IOException {
- getPortletContext().getRequestDispatcher("/WEB-INF/jsp/preferences.jsp").include(request,
- response);
- System.out.println("******** LifeCyclePortlet doEdit ********");
- }
- protected void doHelp(RenderRequest request, RenderResponse response) throws PortletException,
- IOException {
- getPortletContext().getRequestDispatcher("/WEB-INF/jsp/help.jsp")
- .include(request, response);
- System.out.println("******** LifeCyclePortlet doHelp ********");
- }
- protected void doView(RenderRequest request, RenderResponse response) throws PortletException,
- IOException {
- System.out.println("LifeCyclePortlet doView " + request.getParameter("reset"));
- if ("success".equalsIgnoreCase((String) request.getAttribute("actionStatus"))) {
- PortletURL homeUrl = response.createRenderURL();
- request.setAttribute("homeUrl", homeUrl);
- getPortletContext().getRequestDispatcher("/WEB-INF/jsp/success.jsp").include(request,
- response);
- return;
- }
- PortletURL actionUrl = response.createActionURL();
- PortletURL resetRenderUrl = response.createRenderURL();
- request.setAttribute("actionUrl", actionUrl);
- request.setAttribute("resetRenderUrl", resetRenderUrl);
- if (!"error".equalsIgnoreCase((String) request.getAttribute("actionStatus"))) {
- if ("yes".equalsIgnoreCase(request.getParameter("reset"))) {
- defaultVal = "";
- } else {
- defaultVal = getPortletConfig().getInitParameter("defaultVal");
- }
- request.setAttribute("someVal", defaultVal);
- }
- getPortletContext().getRequestDispatcher("/WEB-INF/jsp/inputForm.jsp").include(request,
- response);
- }
- public void processAction(ActionRequest request, ActionResponse response)
- throws PortletException, IOException {
- System.out.println("******** LifeCyclePortlet processAction ********");
- String val = request.getParameter("someVal");
- request.setAttribute("val", val);
- if (val == null || val.trim().equals("")) {
- ResourceBundle bundle = getPortletConfig().getResourceBundle(request.getLocale());
- request.setAttribute("errorMsg", bundle.getString("val.errorMsg.missing"));
- request.setAttribute("actionStatus", "error");
- } else {
- request.setAttribute("actionStatus", "success");
- }
- }
- public void render(RenderRequest request, RenderResponse response) throws PortletException,
- IOException {
- System.out
- .println("LifeCyclePortlet render(RenderRequest request, RenderResponse response) "
- + request.getParameter("reset"));
- super.render(request, response);
- }
public class LifeCyclePortlet extends GenericPortlet {
private String defaultVal;
public void init() {
defaultVal = getPortletConfig().getInitParameter("defaultVal");
System.out.println("******** LifeCyclePortlet init ********");
}
public void init(PortletConfig config) throws PortletException {
System.out.println("******** LifeCyclePortlet init(PortletConfig config) ********");
super.init(config);
}
protected void doEdit(RenderRequest request, RenderResponse response) throws PortletException,
IOException {
getPortletContext().getRequestDispatcher("/WEB-INF/jsp/preferences.jsp").include(request,
response);
System.out.println("******** LifeCyclePortlet doEdit ********");
}
protected void doHelp(RenderRequest request, RenderResponse response) throws PortletException,
IOException {
getPortletContext().getRequestDispatcher("/WEB-INF/jsp/help.jsp")
.include(request, response);
System.out.println("******** LifeCyclePortlet doHelp ********");
}
protected void doView(RenderRequest request, RenderResponse response) throws PortletException,
IOException {
System.out.println("LifeCyclePortlet doView " + request.getParameter("reset"));
if ("success".equalsIgnoreCase((String) request.getAttribute("actionStatus"))) {
PortletURL homeUrl = response.createRenderURL();
request.setAttribute("homeUrl", homeUrl);
getPortletContext().getRequestDispatcher("/WEB-INF/jsp/success.jsp").include(request,
response);
return;
}
PortletURL actionUrl = response.createActionURL();
PortletURL resetRenderUrl = response.createRenderURL();
request.setAttribute("actionUrl", actionUrl);
request.setAttribute("resetRenderUrl", resetRenderUrl);
if (!"error".equalsIgnoreCase((String) request.getAttribute("actionStatus"))) {
if ("yes".equalsIgnoreCase(request.getParameter("reset"))) {
defaultVal = "";
} else {
defaultVal = getPortletConfig().getInitParameter("defaultVal");
}
request.setAttribute("someVal", defaultVal);
}
getPortletContext().getRequestDispatcher("/WEB-INF/jsp/inputForm.jsp").include(request,
response);
}
public void processAction(ActionRequest request, ActionResponse response)
throws PortletException, IOException {
System.out.println("******** LifeCyclePortlet processAction ********");
String val = request.getParameter("someVal");
request.setAttribute("val", val);
if (val == null || val.trim().equals("")) {
ResourceBundle bundle = getPortletConfig().getResourceBundle(request.getLocale());
request.setAttribute("errorMsg", bundle.getString("val.errorMsg.missing"));
request.setAttribute("actionStatus", "error");
} else {
request.setAttribute("actionStatus", "success");
}
}
public void render(RenderRequest request, RenderResponse response) throws PortletException,
IOException {
System.out
.println("LifeCyclePortlet render(RenderRequest request, RenderResponse response) "
+ request.getParameter("reset"));
super.render(request, response);
}
#对上面代码的一个简单解释,以portlet继承了GenericPortlet为例,
当容器初始话的时候,调用init(PortletConfig config)和init()方法.
当有render request到达portlet的时候,render(RenderRequest request, RenderResponse response)方法被调用,根据render的mode不同,会调用doView,doEdit或者doHelp方法.render mode有view,edit,help三种.如下面LifeRay截图,显示A:portlet的"主页面"就是view mode,需要配置preferences(B)为edit mode,需要Help(C)为help mode.
当有action request的时候,调用processAction方法.也可通过Java注解@RenderMode(name = "EDIT"), @ProcessAction(name = "xxxAction")的方式来告诉容器render和action应该有那个方法来响应,这里不做介绍.
如果直接实现Portlet接口的话,只有init(PortletConfig config),render(RenderRequest request, RenderResponse response),processAction(ActionRequest request, ActionResponse response),destroy()四个方法,本例中实现的是GenericPortlet,它对Portlet的方法提供了默认的实现,如render方法会根据render request的mode决定是调用doView,doEdit还是doHelp方法等等.
下面是Portlet用到的显示内容的jsp:
- help.jsp:
- <table width="200px">
- <tr>
- <td>
- This is the help page
- </td>
- </tr>
- </table>
- preference.jsp:
- <table width="200px">
- <tr>
- <td>
- This is the preference page for editing options
- </td>
- </tr>
- </table>
- inputForm.jsp:
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
- <%@ page contentType="text/html" isELIgnored="false" %>
- <fmt:setBundle basename="content.Language-ext"/>
- <form action="<c:out value='${requestScope.actionUrl}'/>" method="POST">
- <table width="200px">
- <tr>
- <td colspan="2">
- <font color="#FF0000"><c:out
- value="${requestScope.errorMsg}"/></font>
- </td>
- </tr>
- <tr>
- <td><font color="#FF0000"><b>*</b></font> <fmt:message key="label.val"/></td>
- <td><input type="text" name="someVal" value="${requestScope.someVal}"></input></td>
- </tr>
- <tr>
- <td> </td>
- </tr>
- <tr align="center">
- <td colspan="2">
- <input type="submit"/>
- <a href="<c:out value='${requestScope.resetRenderUrl}'/>&reset=yes">
- <b><fmt:message key="label.reset"/></b>
- </a>
- </td>
- </tr>
- </table>
- </form>
- success.jsp:
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
- <%@ page contentType="text/html" isELIgnored="false" %>
- <fmt:setBundle basename="content.Language-ext"/>
- <table>
- <tr>
- <td colspan="2">action processed successfully</td>
- </tr>
- <tr>
- <td align="right"><b>inputed value is </b></td>
- <td><c:out value="${requestScope.val}"/></td>
- </tr>
- <tr>
- <td colspan="2">
- <a href="<c:out value='${requestScope.homeUrl}'/>">
- <b><fmt:message key="label.home"/></b>
- </a>
- </td>
- </tr>
- </table>
help.jsp:
<table width="200px">
<tr>
<td>
This is the help page
</td>
</tr>
</table>
preference.jsp:
<table width="200px">
<tr>
<td>
This is the preference page for editing options
</td>
</tr>
</table>
inputForm.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ page contentType="text/html" isELIgnored="false" %>
<fmt:setBundle basename="content.Language-ext"/>
<form action="<c:out value='${requestScope.actionUrl}'/>" method="POST">
<table width="200px">
<tr>
<td colspan="2">
<font color="#FF0000"><c:out
value="${requestScope.errorMsg}"/></font>
</td>
</tr>
<tr>
<td><font color="#FF0000"><b>*</b></font> <fmt:message key="label.val"/></td>
<td><input type="text" name="someVal" value="${requestScope.someVal}"></input></td>
</tr>
<tr>
<td> </td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit"/>
<a href="<c:out value='${requestScope.resetRenderUrl}'/>&reset=yes">
<b><fmt:message key="label.reset"/></b>
</a>
</td>
</tr>
</table>
</form>
success.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ page contentType="text/html" isELIgnored="false" %>
<fmt:setBundle basename="content.Language-ext"/>
<table>
<tr>
<td colspan="2">action processed successfully</td>
</tr>
<tr>
<td align="right"><b>inputed value is </b></td>
<td><c:out value="${requestScope.val}"/></td>
</tr>
<tr>
<td colspan="2">
<a href="<c:out value='${requestScope.homeUrl}'/>">
<b><fmt:message key="label.home"/></b>
</a>
</td>
</tr>
</table>
#help.jsp是在上图中,当点击 "Help"的时候用来显示信息.对应doHelp方法中的Portlet中getPortletContext().getRequestDispatcher("/WEB-INF/jsp/help.jsp")代码.
#preferences.jsp是在上图中,当点击 "Preferences.jsp"的时候用来显示信息.
#inputForm.jsp用来做为Portlet的"主页面",portlet第一次显示的时候会由这个jsp来显示内容.
<form action="<c:out value='${requestScope.actionUrl}'/>" method="POST">的作用指定了form提交时候的action request,这个action url是portlet的java代码PortletURL actionUrl = response.createActionURL();生成的.提交的时候会调用portlet的processAction方法.Portlet URL和普通HTTP URL不同的地方在于,它在普通HTTP URL的基础上加上了一个参数来标识要访问的是哪一个个Portlet,如下面的p_p_id=HelloWorldPortlet_WAR_helloWorld_INSTANCE_MrP9,加上了一个参数用来标识request的类型,如下面的p_p_mode=view,这个URL有Portlet容器解析,就知道了用哪一个portlet的哪一个方法来为一个特定的URL服务了.注意,在不同的Portlet容器中,Portlet URL的参数和值是不同的,下面是LifeRay中的例子.
- http://localhost:8080/web/guest/home?p_p_id=HelloWorldPortlet_WAR_h
- elloWorld_INSTANCE_MrP9&p_p_lifecycle=1&p_p_state=normal&p_p_mo
- de=view&p_p_col_id=column-
- 1&p_p_col_count=1&_HelloWorldPortlet_WAR_helloWorld_INSTANCE_MrP9_action=someAction
http://localhost:8080/web/guest/home?p_p_id=HelloWorldPortlet_WAR_h
elloWorld_INSTANCE_MrP9&p_p_lifecycle=1&p_p_state=normal&p_p_mo
de=view&p_p_col_id=column-
1&p_p_col_count=1&_HelloWorldPortlet_WAR_helloWorld_INSTANCE_MrP9_action=someAction
整个项目的structure如下:完整的代码可以从http://download.csdn.net/detail/kkdelta/4084606下载.