【问题标题】:Referencing an attribute for a simple tag and Struts 2.x为简单标签和 Struts 2.x 引用属性
【发布时间】:2011-09-06 08:33:09
【问题描述】:

我正在编写一个包含 4 个属性的简单标签,如下所示:

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ attribute name="menuName" description="Name of Major Menu" required="true" %>
<%@ attribute name="menuSize" description="Number of Items In Menu" required="true" type="java.lang.Integer" %>
<%@ attribute name="menuImage" description="Icon for Image" required="true" %>
<%@ attribute name="menuNumber" description="Order of Positioning" required="true" %>

在 Struts 2 标记中有一点我需要像这样引用这些属性之一:

<s:set name="it" value="%{#application['tools'][${menuName}]}" />

所以你看到我正在根据传入的菜单名称在 OGNL 表达式中进行查找。从我看到的所有示例中,EL 是引用属性 menuName 的常用方法,但在 Struts 2.x 中出于安全原因,EL 已禁用。

有没有办法引用我需要引用的属性。我真的不想考虑任何涉及我使用以前版本的 jstl 或 struts 的解决方案。

【问题讨论】:

  • &lt;s:set name="menuName" value="%{#attr['menuName']}" /&gt; &lt;s:set name="it" value="%{#application['tools'][#menuName]}" /&gt; 以前从不知道#attr,多么整洁。必须再等 7 小时才能回答。
  • 澄清一下,在 Struts2 中没有禁用 EL。它只是作为 struts 标签的参数值被禁用。您可以在您的 JSP 或您自己的标记文件中使用 EL。
  • 是的,感谢您的澄清。我的意思是专门在一个struts标签中,比如set。对不起,我的词汇量很懒。

标签: struts2 el ognl custom-tags


【解决方案1】:
<s:set name="menuName" value="%{#attr['menuName']}" /> 
<s:set name="it" value="%{#application['tools'][#menuName]}" />

以前从不知道#attr,多么整洁

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多