Portlet 通信(五) 通过 URL 传递参数

来源:互联网 发布:php httpclient 编辑:程序博客网 时间:2024/06/01 17:21

语法如下:

HttpServletRequest request = PortalUtil.getHttpServletRequest(renderRequest); 
String articleId = PortalUtil.getOriginalServletRequest(request).getParameter("articleId"); 

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

PortletM

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


JSP

<%@ page contentType="text/html; charset=UTF-8" %><%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %><portlet:defineObjects /><!-- 链接地址为N所在Portlet --><a href="http://localhost:8080/web/guest/tean-n?book=Liferay学习">向N中传递参数book,名称为Liferay学习</a>

PortletN

package com.test;import java.io.IOException;import javax.portlet.PortletException;import javax.portlet.RenderRequest;import javax.portlet.RenderResponse;import javax.servlet.http.HttpServletRequest;import com.liferay.portal.util.PortalUtil;import com.liferay.util.bridges.mvc.MVCPortlet;/** * 通过 URL 传递参数 */public class TranN extends MVCPortlet { @Overridepublic void doView(RenderRequest request,RenderResponse response) throws IOException, PortletException {//接收超链接URL中传递过来的参数HttpServletRequest re = PortalUtil.getHttpServletRequest(request);String s = PortalUtil.getOriginalServletRequest(re).getParameter("book");System.out.println("书名为:"+s);//修改后传递给JSP显示s = s + "OK6";request.setAttribute("b", s);super.doView(request, response);}}
在N中接收参数输出,修改后传递到JSP


JSP

<%@page import="com.liferay.portal.kernel.util.ParamUtil"%><%@ page contentType="text/html; charset=UTF-8" %><%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %><portlet:defineObjects /><%    String strb = (String)request.getAttribute("b");%>获取URL传递的参数-修改后:<%=strb %>

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

注意:此种传递参数只能是一个页面中的Portlet能接收到参数,另一个页面若有相同Portlet则接收不到你参数,因为超链接只能链接到一个指定的页面,传递参数时候,可以是同一个页面,也可以是不同的页面