标签:报表 war 后台 splay name 四种 介绍 trend 资源
init() =〉 render() =〉 processAction() =〉 processEvent() =〉 serveResource() =〉destroy()
在Liferay容器部署portlet时,启动portlet实例化
init有两个写法:
PortletConfig对象用于读取portlet配置(定义在portlet.xml)
HTML的UI输出,是最常用到的方法了,一个例子
@Override public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException { _log.info(" 做些你自己定义的事情"); super.render(request, response); }
值得注意的是:
action 处理,后面再详细介绍,这里只要知道ActionRequest也是一个接口继承
监听时间处理
通过resource URL处理资源
portlet卸载时的处理
见:
首先,介绍Render URL,有四种方式:
view.jsp
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@page import="com.liferay.portal.kernel.portlet.LiferayPortletMode"%>
<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%>
<portlet:renderURL var="renderUrl" windowState="<%=LiferayWindowState.NORMAL.toString() %>" copyCurrentRenderParameters="true" portletMode="<%=LiferayPortletMode.VIEW.toString()%>"> <portlet:param name="param" value="XXXXXX"/> </portlet:renderURL> <a href="${renderUrl}">RenderURL Created by Portlet Tag</a>
render()
private static final Log _log = LogFactoryUtil.getLog(YourPortlet.class.getName()); @Override public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException { _log.info(" This is render method"); String data = request.getParameter("param"); String data1= ParamUtil.getString(request, "param",""); super.render(request, response); }
最终会实际产生以下URL:
http://localhost:8080/web/portletTest/test?
p_p_id 是portlet Id
p_p_lifecycle是生命周期的当前阶段,定义是:0 – render;1 – action;2 – serve resource;
p_p_col_id是多列布局中的当前页第几列;
p_p_col_count是当前页的布局总列数;
最后一个就是用户定自定url参数了,由jsp传递;
这种是服务端直接控制,jsp基本不用写什么逻辑,后台控比较喜欢用
render()
@Override public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException { _log.info(" This is render method of RenderURLByJavaAPIPortlet"); String data = request.getParameter("param"); String data1= ParamUtil.getString(request, "param",""); ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY); PortletURL renderUrl = PortletURLFactoryUtil.create(request, themeDisplay.getPortletDisplay().getId(), themeDisplay.getPlid(), PortletRequest.RENDER_PHASE); renderUrl.setWindowState(LiferayWindowState.NORMAL); renderUrl.setPortletMode(LiferayPortletMode.VIEW); renderUrl.setParameter("param", "This parameter comes from Render URL generated with Java API"); request.setAttribute("renderUrlByJavaAPI", renderUrl.toString()); super.render(request, response); }
其中,plid 是page layout id
在jsp中,只用一行代码:
<a href="${renderUrlByJavaAPI}">Render Url created by Java API</a>
还有一种灵活方式,即可以由jsp来直接输出
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %> <liferay-theme:defineObjects/> <% PortletURL renderUrlFromJSP = renderResponse.createRenderURL(); renderUrlFromJSP.setParameter("param1", "This portletULR is created with API in JSP"); renderUrlFromJSP.setWindowState(LiferayWindowState.NORMAL); renderUrlFromJSP.setPortletMode(LiferayPortletMode.VIEW); %> <a href="<%=renderUrlFromJSP%>">Render Url created by JSP</a>
view.jsp <%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %> <liferay-portlet:renderURL var="openPortletURL" copyCurrentRenderParameters="true" portletMode="<%=LiferayPortletMode.VIEW.toString() %>" windowState="<%=LiferayWindowState.NORMAL.toString()%>"> <liferay-portlet:param name="param" value="This is from Liferay TAG"/> </liferay-portlet:renderURL> <a href="${openPortletURL}">Render Url created by Liferay TAG in JSP</a>
render() 同第一种写法(Portlet Tag方式)
view.jsp <%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%> <%@page import="com.liferay.portal.kernel.portlet.LiferayPortletMode"%> <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %> <%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %> <liferay-theme:defineObjects/> <portlet:defineObjects /> <a id="renderURLWithJS" href=""> This render URL link is created with Javascript</a> <aui:script> AUI().use(‘liferay-portlet-url‘, function(A) { var renderUrl1 = Liferay.PortletURL.createRenderURL(); renderUrl1.setWindowState("<%=LiferayWindowState.NORMAL.toString() %>"); renderUrl1.setParameter("param","This value comes from Javascript"); renderUrl1.setPortletMode("<%=LiferayPortletMode.VIEW %>"); renderUrl1.setPortletId("<%=themeDisplay.getPortletDisplay().getId() %>"); A.one("#renderURLWithJS").set(‘href‘,renderUrl1.toString()); alert("renderUrl1 is ->"+renderUrl1.toString()); alert(A.one("#renderURLWithJS").attr("href")); }); </aui:script> 这种比较不常用
render() 同第一种写法(Portlet Tag方式)
在涉及页面处理业务逻辑,或其他Portlet交互,或者Form提交Action时,就需要我们定义Action方法,actionURL 就是用来传递Action的id
它是和jsp一对一对应的,比如jsp中:
view.jsp <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <portlet:defineObjects /> User Name is : <b> ${userName}</b> <portlet:actionURL name="actionMethod1" var="sampleActionMethodURL"> </portlet:actionURL> <form action="${sampleActionMethodURL}" method="post"> UserName :-<input type="text" name="userName"><br> <input type="submit" value="Submit"> </form> <portlet:actionURL name="addName" var="addNameUrl"></portlet:actionURL> <a href="${addNameUrl}">Add Name</a>
前提还要定义portlet.xml
portlet.xml首先要定义 <portlet-name>custom-liferaymvc</portlet-name> <display-name>Custom Liferaymvc</display-name> <portlet-class>com.companyname.portlet.CustomMVCPortlet</portlet-class> <init-param> <name>view-jsp</name> <value>/jsp/view.jsp</value> </init-param>
编写一个继承MVCPortlet的类
@ProcessAction(name="addName") public void addName(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException, PortalException, SystemException{ actionRequest.setAttribute("userName", "Wangxin"); } @ProcessAction(name="actionMethod1") public void sampleActionMethod(ActionRequest request, ActionResponse response) throws IOException, PortletException, PortalException, SystemException{ _log.info("This is sampleActionMethod ..."); String userName = ParamUtil.get(actionRequest, "userName", StringPool.BLANK); actionRequest.setAttribute("userName", userName); }
如果跟踪一下,会是自动产生以下url:
http://localhost:8080/web/home?p_auth=e6cvA8tX
&p_p_id=testactionmethod_WAR_testactionmethodportlet
&p_p_lifecycle=1
&p_p_state=normal
&p_p_mode=view
&p_p_col_id=column-1
&_testactionmethod_WAR_testactionmethodportlet_javax.portlet.action=actionMethod1
另外PortletURLFactoryUtil 是一种后台产生action url的常用方式
java code: @Override public void render(RenderRequest request, RenderResponse response) throws PortletException, IOException { ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY); PortletURL actionUrl = PortletURLFactoryUtil.create(request, themeDisplay.getPortletDisplay().getId(), themeDisplay.getPlid(), PortletRequest.ACTION_PHASE); actionUrl.setWindowState(LiferayWindowState.NORMAL); actionUrl.setPortletMode(LiferayPortletMode.VIEW); actionUrl.setParameter("javax.portlet.action", "actionMethodByJavaApi"); actionUrl.setParameter("sampleParam", "This parameter comes from Action URL generated with Java API"); request.setAttribute("actionUrlByJavaAPI", actionUrl.toString()); super.render(request, response); } @ProcessAction(name="actionMethodByJavaApi") public void actionMethodByJavaApi(ActionRequest request, ActionResponse response) throws IOException, PortletException, PortalException, SystemException{ String sampleParam = ParamUtil.get(request, "sampleParam", "defaultValue"); _log.info("Sample Param is ::"+sampleParam); } view.jsp <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <portlet:defineObjects /> <a href="${actionUrlByJavaAPI}">Action Url created by Java API in Portlet Class</a> <br>
还有两种方式就不介绍了:
liferay-portlet:actionURL 和 JavaScript (AUI module – Liferay.PortletURL)
一个portlet交互的流程图
Render & action是最早被定义的规范,见portlet specification 1.0 (JSR-168).
resource serving 被定义于portlet specification 2.0 (JSR-286)
一句话概括serve resource做的事情:动态产生输出到客户端,负责向客户端发送动态内容
比如:下载文件,发送一个excle报表,发送xml、json文件......,这些在Render 周期是无法实现的
serve resource过程
这里有比较好的介绍:
http://www.oracle.com/technetwork/java/jsr286-2-141964.html
Liferay7 BPM门户开发之17: Portlet 生命周期
标签:报表 war 后台 splay name 四种 介绍 trend 资源
原文地址:http://www.cnblogs.com/starcrm/p/6029771.html