在Portlet环境中使用jsp:param / c:param

时间:2010-02-28 16:59:58

标签: java jsp portlet

我正在尝试在Portlet环境中包含jsp:param的JSP页面(使用Pluto portlet容器)。

例如,

<jsp:include page="test.jsp">
   <jsp:param name="foo" value="bar"/>
</jsp:include>

并在test.jsp中,

<c:out value="${foo}"/> or <%= request.getParameter("foo") %>

输出始终为null,我也尝试使用c标记,但结果相同。

<c:import url="test.jsp">
   <c:param name="foo" value="bar"/>
</c:import>

我在网上搜索过很多人都面临同样的问题,除了没有解决方法。

这是一个限制还是有不同的方法来做到这一点?

2 个答案:

答案 0 :(得分:2)

这在普通的Servlet环境中运行良好,但我从谷歌的一些googling看到,portlet环境似乎打破了它。这是一种耻辱,但表明portlet规范是直言不讳地打破了。

如果<jsp:param>不适合您,则替代方法是使用请求属性:

<c:set var="foo" value="bar" scope="request"/>
<jsp:include page="test.jsp"/>

test.jsp

<c:out value="${requestScope.foo}"/>

或者只是:

<c:out value="${foo}"/>

它不像使用params那样整洁和包含,但它应该适用于portlet。

答案 1 :(得分:1)

我遇到了同样的问题。我的解决方案是使用Portlet的renderRequest对象(可以从包含的jsp文件访问)。在我的portlet中,我在我的JSP中设置了RenderRequest对象的属性(通过jsp:include包含)。我使用Portlet API来访问隐式的renderRequest对象。这是一个示例:

时,MyPortlet:

public void doView(RenderRequest request, RenderResponse response) {
  request.setAttribute("myBean", new MyBean());
  getPortletContext().getRequestDispatcher("myMainJSP.jsp").include(request, response);
}

myMainJSP.jsp:

<jsp:include page="header.jsp"/>

header.jsp中:

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<% page import="sample.MyBean" %>
<portlet:defineObjects/>
<%
  MyBean myBean = (MyBean)renderRequest.getAttribute("myBean");
%>
... html code...