Portlet 通信(一) Portlet Session

来源:互联网 发布:网络kvm 编辑:程序博客网 时间:2024/05/21 14:49

Portlet Session

====================================================

actionRequest.getPortletSession().setAttribute("id",id,PortletSession.APPLICATION_SCOPE);

(String)renderRequest.getPortletSession().getAttribute("id",PortletSession.APPLICATION_SCOPE);

PORTLET_SCOPE :只能在同一个portlet中使用
APPLICATION_SCOPE:可以在所有Porlet中使用,也能在Servlet/JSP等中使用

 如果想在任何Servlet和Portlet中共享session
 在portlet中定义session的作用范围时,属性必须为APPLICATION_SCOPE
只能使用在servlet中被定义为APPLICATION_SCOPE的session属性,否则会得到null值

跨WAR包的共享:

<private-session-attributes>false</private-session-attributes>

-----------------------------------------------------------------------------------------------

Emp实体类:

public class Emp implements Serializable {private static final long serialVersionUID = 1L;private String name;private Integer age;private Date time;

-----------------------------------------------------------------------------------------------

代码如下:A向B传递数据(基本数据和对象)

PortletA

package com.test;import java.io.IOException;import java.util.Date;import javax.portlet.ActionRequest;import javax.portlet.ActionResponse;import javax.portlet.EventRequest;import javax.portlet.EventResponse;import javax.portlet.PortletException;import javax.portlet.PortletSession;import javax.portlet.ProcessAction;import javax.portlet.RenderRequest;import javax.portlet.RenderResponse;import com.liferay.portal.kernel.util.ParamUtil;import com.liferay.util.bridges.mvc.MVCPortlet;import entity.Emp;/** * 使用PortletSession */public class TranA extends MVCPortlet { @ProcessAction(name="send1")public void senddata(ActionRequest request,ActionResponse response) throws IOException, PortletException {//获取view-a.jsp表单中的数据,存入PortletSession中发送//发送String类型String name = ParamUtil.getString(request, "name"); String age = ParamUtil.getString(request, "age"); request.getPortletSession().setAttribute("name", name, PortletSession.APPLICATION_SCOPE);request.getPortletSession().setAttribute("age", age, PortletSession.APPLICATION_SCOPE);//发送Emp对象Emp emp = new Emp();emp.setName("LUCY");emp.setAge(20);emp.setTime(new Date());request.getPortletSession().setAttribute("emp", emp, PortletSession.APPLICATION_SCOPE);}}

PortletA 的JSP

<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %><portlet:defineObjects /><portlet:actionURL var="form" name="send1"></portlet:actionURL><html>  <body>    <form action="<%=form%>" method="post">       姓名:<input type="text" name="name"/><br>       年龄:<input type="text" name="age"/><br>       <input type="submit" value="提交"/>    </form>  </body></html>


PottletB

package com.test;import com.liferay.util.bridges.mvc.MVCPortlet;/** * Portlet implementation class TranB */public class TranB extends MVCPortlet { }

PortletB 的JSP

<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %><portlet:defineObjects /><html>  <body>    <p>接收到tran-a的数据</p>    <p>String类型:</p>    <p>name:${name }</p>    <p>age:${age }</p>    <p>接收对象类型:</p>    <p>    姓名:${emp.name }  年龄:${emp.age }  时间:${emp.time }    </p>  </body></html>

-----------------------------------------------------------------------------------------------

A的表单提交后,通过表单action属性找到A中名为send1的方法(@ProcessAction(name="send1")),获取A中JSP表单里面的数据,然后存入PortletSession,可以在B的doView方法中用以下方式获得:
String m = (String)renderRequest.getPortletSession().getAttribute("name", PortletSession.APPLICATION_SCOPE);String n = (String)renderRequest.getPortletSession().getAttribute("age", PortletSession.APPLICATION_SCOPE);
其中name和age为A中绑定的名称,也可以在B的JSP页面中直接通过EL表达式获取
注意标签的使用:
<!-- 可重用 --><instanceable>true</instanceable><!-- 跨war包传递 --><private-session-attributes>false</private-session-attributes><!-- 不需要namespace --><requires-namespaced-parameters>false</requires-namespaced-parameters>


原创粉丝点击