第一步: 写好PortletSession A
1.1 这是PortletSession A 的jsp页面代码
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <%@ page contentType="text/html; charset=utf-8" %> <portlet:defineObjects /> 两个Portlet 用 Session 进行通讯 </br> 这是PortSession A</br> <portlet:actionURL name="sendParamaterAction" var="sendParamater"> </portlet:actionURL> <form action="<%=sendParamater %>" method="post"> username : <input type="text" name="username" /> </br> <input type="submit" value="submit"> </form>
对应的页面效果如下:
1.2 PortletSession A 的java代码
package com.test; import java.io.IOException; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletException; import javax.portlet.PortletSession; import javax.portlet.ProcessAction; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import com.liferay.util.bridges.mvc.MVCPortlet; /** * Portlet implementation class PorletSession */ public class PorletSession extends MVCPortlet { @ProcessAction(name="sendParamaterAction") public void sendParamate(ActionRequest actionRequest,ActionResponse actionResponse) throws IOException, PortletException{ String username = actionRequest.getParameter("username"); System.out.println("sendParamaterAction username = " + username); actionRequest.getPortletSession().setAttribute("username", username,PortletSession.APPLICATION_SCOPE);<pre name="code" class="html">PortletSession.APPLICATION_SCOPE
getPortletSession} }
2.1 是Portletsession B的jsp页面代码
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> <%@ page contentType="text/html; charset=utf-8" %> <portlet:defineObjects /> 两个Portlet 用 Session 进行通讯 </br> 这是PortSession B</br> 从名称为 portletsession的Portlet 传入的username值:${username}
2.2 PortletSession B的java代码
package com.test; import java.io.IOException; import javax.portlet.PortletException; import javax.portlet.PortletSession; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import com.liferay.util.bridges.mvc.MVCPortlet; /** * Portlet implementation class PorletSessionB */ public class PorletSessionB extends MVCPortlet { @Override public void doView(RenderRequest renderRequest,RenderResponse renderResponse) throws IOException, PortletException { String username = (String )renderRequest.getPortletSession().getAttribute("username",PortletSession.APPLICATION_SCOPE); System.out.println("PorletSessionB doView username = " + username); renderRequest.setAttribute("username", username); super.doView(renderRequest, renderResponse); } }
实现的效果如下:
在portletsession A的表单中输入sdfsdf提交后,Portlet session b可以显示出输入的值