在不同的Portlet之间传参数,这想这是所有用Liferay的人都想实现的,但在以前的版本中确实做不到。现在的版本可以了,并且很简单
首先,打开portlet.xml文件,找到你想实现传值和接收值的portlet片段,在每个片段中加上下面这行代码:
<supported-public-render-parameter>参数名</supported-public-render-parameter>
然后,在最后面定义这个参数,写法是这样的:
<public-render-parameter>
<identifier>参数名</identifier>
<qname xmlns:x="http://www.liferay.com/public-render-parameters">x:参数名</qname>
</public-render-parameter>
整体是这样
<?xml version="1.0"?>
<portlet-app
version="2.0"
xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
>
<portlet>
<portlet-name>chartcontroler</portlet-name>
<display-name>ChartControler</display-name>
<portlet-class>
com.noah.action.ChartControlerPortlet
</portlet-class>
<init-param>
<name>view-jsp</name>
<value>/html/chartcontroler/view.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
</supports>
<resource-bundle>content/Language</resource-bundle>
<portlet-info>
<title>ChartControler</title>
<short-title>ChartControlerPortlet</short-title>
<keywords></keywords>
</portlet-info>
<portlet-preferences>
<preference>
<name>years</name>
<value>2010,2011,2012</value>
</preference>
</portlet-preferences>
<security-role-ref>
<role-name>administrator</role-name>
</security-role-ref>
<security-role-ref>
<role-name>guest</role-name>
</security-role-ref>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
<supported-public-render-parameter>currentYear</supported-public-render-parameter>
<supported-public-render-parameter>report</supported-public-render-parameter>
<supported-public-render-parameter>partner</supported-public-render-parameter>
<supported-public-render-parameter>portfolio</supported-public-render-parameter>
</portlet>
<portlet>
<portlet-name>chart</portlet-name>
<display-name>Chart</display-name>
<portlet-class>com.noah.action.ChartPortlet</portlet-class>
<init-param>
<name>view-jsp</name>
<value>/html/chartportlet/view.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
</supports>
<resource-bundle>content/Language</resource-bundle>
<portlet-info>
<title>Chart</title>
<short-title>ChartPortlet</short-title>
<keywords></keywords>
</portlet-info>
<security-role-ref>
<role-name>administrator</role-name>
</security-role-ref>
<security-role-ref>
<role-name>guest</role-name>
</security-role-ref>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
<supported-public-render-parameter>currentYear</supported-public-render-parameter>
<supported-public-render-parameter>report</supported-public-render-parameter>
<supported-public-render-parameter>partner</supported-public-render-parameter>
<supported-public-render-parameter>portfolio</supported-public-render-parameter>
</portlet>
<public-render-parameter>
<identifier>currentYear</identifier>
<qname xmlns:x="http://www.liferay.com/public-render-parameters">x:currentYear</qname>
</public-render-parameter>
<public-render-parameter>
<identifier>report</identifier>
<qname xmlns:x="http://www.liferay.com/public-render-parameters">x:report</qname>
</public-render-parameter>
<public-render-parameter>
<identifier>partner</identifier>
<qname xmlns:x="http://www.liferay.com/public-render-parameters">x:partner</qname>
</public-render-parameter>
<public-render-parameter>
<identifier>portfolio</identifier>
<qname xmlns:x="http://www.liferay.com/public-render-parameters">x:portfolio</qname>
</public-render-parameter>
</portlet-app>
对参数进行赋值的话,就是在Action中用下面这句话:
actionResponse.setRenderParameter("参数名", 参数值);
获取的话,就直接像取其它参数一下直接获取:
在Action中,String 变量 = ParamUtil.getString(request, "参数名", "默认值");
在JSP中,<%=renderRequest.getParameter("参数名")%>
整体代码如下:
/** *//**
* @author noah.xiang
*/
public class ChartControlerPortlet extends MVCPortlet {
@Override
public void processAction(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
String currentYear = ParamUtil.getString(actionRequest, "currentYear", "2010");
String report = ParamUtil.getString(actionRequest, "report", "");
String partner = ParamUtil.getString(actionRequest, "partner", "Bocholter Energie- und Wasserversorgung GmbH");
String portfolio = ParamUtil.getString(actionRequest, "portfolio", "");
actionResponse.setRenderParameter("currentYear", currentYear);
actionResponse.setRenderParameter("report", report);
actionResponse.setRenderParameter("partner", partner);
actionResponse.setRenderParameter("portfolio", portfolio);
//actionResponse.setRenderParameter("jspPage", "/html/chartportlet/view.jsp");
super.processAction(actionRequest, actionResponse);
}
@Override
public void render(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
String currentYear = ParamUtil.getString(request, "currentYear", "2010");
String report = ParamUtil.getString(request, "report", "");
String partner = ParamUtil.getString(request, "partner", "");
String portfolio = ParamUtil.getString(request, "portfolio", "");
PortletPreferences prefs = request.getPreferences();
String years = prefs.getValue("years", "2010,2011,2012");
//String portfolios = prefs.getValue("portfolios", "Absatzportfolio: A-a,Absatzportfolio: B-b,Absatzportfolio: C-c");
//String partners = prefs.getValue("partners", "");
//顺便说一下,这一行,可以实现通过Session传值,有兴趣的可以研究一下
request.getPortletSession().setAttribute(DataPoolUtil.SESSION_SHARED_YEAR, currentYear, PortletSession.APPLICATION_SCOPE);
request.setAttribute("years", years);
request.setAttribute("portfolios", portfolio);
request.setAttribute("currentYear", currentYear);
request.setAttribute("report", report);
request.setAttribute("partner", partner);
super.render(request, response);
}
}
眼镜蛇