portlet之间传参有4种方式:
1、application级别的PortletSession
2、JSR 286 中的共享参数
3、JSR 286 中的事件机制
4、AJAX方式
一,application级别的PortletSession
PortletSession session = request.getPortletSession();//得到session
session.setAttribute("param", "1",PortletSession.APPLICATION_SCOPE);//设置值
session.getAttribute("param",PortletSession.APPLICATION_SCOPE);//获取值
二,JSR 286 中的共享参数
如果你想在多个portlet间共享参数,一种方法就是将共享参数存放在用户的session中.Liferay Portal有一些比较好的设置来共享参数如liferay-portlet.xml 中<shared-session-attributes>和portal-ext.properties 中shared.session.attributes.
但你尝试过session-less,或者通过session让所有用户共享参数?
有一种方式是将共享参数存放在ROOT context中,第一步是在Liferay EXT 环境中创建GlobalStartupAction,如下:
package com.foo.liferay.startup;public class GlobalStartupAction extends SimpleAction { private static List<String> fooList;public List<String> getFooList() {
return fooList;
}
public static void
@Override public void run(String[] ids) throws ActionException {
// Cache all the data
fooList = new ArrayList<String>();
fooList.add(new String("red"));
fooList.add(new String("green"));
fooList.add(new String("blue"));
}
}
GlobalStartupAction需要在portal-ext.properties配置. Liferay自身启动已经指明一个全局启动action,我们可以通过一个逗号分隔的列表,在Liferay补充的GlobalStartupAction:
global.startup.events=com.liferay.portal.events.GlobalStartupAction,com.foo.liferay.startup.GlobalStartupAction
然后在每个portlet中,使用Liferay PortalClassInvoker调用静态getFooList()方法。实用的数字PortalClassInvoker的类装载器的Liferay Portal context之前,要求在指定的方法:import com.liferay.portal.kernel.util.PortalClassInvoker;
public class ApplicationScopeBean {
private static final List fooList;
public ApplicationScopeBean() {
List<String> fooList = (List<String>) PortalClassInvoker.invoke("com.foo.liferay.startup.GlobalStartupAction", "getFooList"));
}
参考资料:http://www.liferay.com/web/ngriffin/blog/-/blogs/sharing-data-between-portlets
三、JSR 286 中的事件机制
1,在portlet1中添加action处理
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws throws PortletException, IOException{
actionResponse.setEvent("simple-event", "simple-event is sent by "
+ portletConfig.getPortletName());
}
2,jsp页面中添加form,触发portlet的action
<form method="POST" action="<portlet:actionURL/>">
<input type="submit" value="点击产生触发事件">
</form>
3,在portlet2中添加事件处理
public void processEvent(EventRequest eventRequest,
EventResponse eventResponse) throws PortletException, IOException {
Event event = eventRequest.getEvent();
eventResponse.setRenderParameter("eventName", event.getName());
eventResponse.setRenderParameter("eventValue", event.getValue().toString());
}
4,jsp页面中显示事件内容
Event name:<%=renderRequest.getParameter("eventName")%>
Event value:<%=renderRequest.getParameter("eventValue")%>
5,在portlet.xml文件中添加
<event-definition>
<name>simple-event</name>
<value-type>java.lang.String</value-type>
</event-definition>
6,在portlet.xml文件每个portlet中添加
<supported-processing-event>
<name>simple-event</name>
</supported-processing-event>
四,AJAX方式
略
五,URL地址传值[试验中]
http://localhost:8080/web/guest/home?p_p_id=FlashPortlet_WAR_ShowFlashportlet_INSTANCE_Qa7V&p_p_lifecycle=0&参数=值