【问题标题】:Passing values in `<portlet:param>` using EL使用 EL 在 `<portlet:param>` 中传递值
【发布时间】:2023-05-24 04:18:02
【问题描述】:

我正在尝试将带有&lt;portlet:actionURL&gt; 的参数传递给liferay 中的portlet,但事实证明,使用EL 传递值不起作用,但是,使用JSP 表达式标记工作正常。

这是我的相关代码:

<%
    ResultRow row = (ResultRow)request.getAttribute(WebKeys.SEARCH_CONTAINER_RESULT_ROW);

    Course course = (Course) row.getObject();
    long groupId = themeDisplay.getLayout().getGroupId();
    String name = Course.class.getName();
    String primaryKey = String.valueOf(course.getPrimaryKey());

%>

<liferay-ui:icon-menu>

    <c:if test="<%= permissionChecker.hasPermission(groupId, name, primaryKey, ActionKeys.UPDATE)%>">
        <portlet:actionURL name="editCourse" var="editURL">
            <portlet:param name="resourcePrimaryKey" value="${primaryKey}"/>
        </portlet:actionURL>

        <liferay-ui:icon image="edit" message="Edit" url="${editURL}" />
    </c:if>
</liferay-ui:icon-menu>

如您所见,在&lt;portlet:param&gt; 标签中,我使用EL 来传递value 属性。但它不起作用,当我这样做时,我在我的操作方法中收到 0 for "resourcePrimaryKey" 的值:

long courseId = ParamUtil.getLong(request, "resourcePrimaryKey");
// courseId is 0 here

但是,如果我使用 JSP 表达式标记代替 EL,它可以正常工作:

<portlet:actionURL name="editCourse" var="editURL">
    <portlet:param name="resourcePrimaryKey" value="<%= primaryKey %>"/>
</portlet:actionURL>

现在,我得到了"resourcePrimaryKey" 所需的值。

谁能弄清楚这里发生了什么?令人惊讶的是,其他地方的 EL 工作正常,如您所见 - url 属性的 ${editURL} 值工作正常,并重定向到相应的 url。

我在 apache 邮件存档中遇到了 this thread 关于同一问题,但这并不能真正解决问题。

【问题讨论】:

    标签: java jsp liferay el portlet


    【解决方案1】:

    scriptlet 中的变量不能直接在 EL 中使用,您首先需要将其设置为:

    <c:set var="primKey"><%=primaryKey %></c:set>
    

    并使用${primKey} 或将其设置为请求属性:

    request.setAttribute("primKey", primaryKey);
    

    显然,最好直接使用表达式。

    同样关于${editURL} 的工作,它是一个portlet jsp 标记,它在页面上下文中设置变量,以便EL 可以使用它。

    我们的 wiki 是了解这些事情的好地方,请留意标题Make objects available to EL 来回答这个问题:-)

    【讨论】:

    • 啊哈!不知道那件事。谢谢 :) 实际上,我之前也尝试过使用&lt;c:set&gt;,但我也使用${primaryKey} 来设置属性,因此应该显示与您所说的相同的行为。现在明白了。另外,请你看看my other question吗?
    • 有没有办法首先将这些脚本转换为 JSTL?或在其他地方设置这些值 - ResultRow 等?
    • 我将使用上一个 JSP 页面中的这个标签来到这个地方 - &lt;liferay-ui:search-container-column-jsp path="/html/courseadmin/admin_actions.jsp" align="right" /&gt;。我可以把它寄到其他地方吗?
    • 我认为这些问题与另一个question有关。最好在那个地方讨论这个问题,而不是把它和这个问题混在一起。
    • 无论如何,我担心 liferay 不是无脚本 JSP 的忠实拥护者,而且哲学也鼓励使用 scriptlets,所以答案是否定的,你不能将这些 scriptlet 转换为 JSTL。