【问题标题】:How to retrieve bean from Struts Tiles and use in a JSP如何从 Struts Tiles 中检索 bean 并在 JSP 中使用
【发布时间】:2014-02-28 18:20:17
【问题描述】:

我们在旧版 Web 应用程序中使用 Struts Tiles 1.x。在a.jsp,我们这样做:

<tiles:put name="bean" beanName="change"></tiles:put>

后来,b.jsp 引用了a.jsp。在b.jsp,我们目前有一个scriptlet,我们在其中做:

<% DomainObject domObject = (DomainObject) TilesHelper.getAttribute("bean", pageContext); %>

这是getAttribute(String,PageContext) 的样子:

public class TilesHelper {
    public static Object getAttribute(String name, PageContext pageContext) throws JspException {
        ComponentContext compContext = (ComponentContext) pageContext.getAttribute(
                ComponentConstants.COMPONENT_CONTEXT, PageContext.REQUEST_SCOPE);

        if (compContext == null) {
            throw new JspException("Error - tag.getAttribute : component context is not defined. Check tag syntax");
        }

        return compContext.getAttribute(name);
    }

这可行,但我不喜欢 scriptlet。小脚本很丑陋。我不想要这个 JSP 中的任何小脚本。我想使用标签来访问在请求范围中传递的对象。我试过了

<bean:define id="domObject" name="bean" scope="request"/>

这失败,错误找不到 bean bean in scope request.

我如何访问在<tiles:put> 调用中传递的对象,其标记为<bean:define><jsp:useBean>

谢谢

【问题讨论】:

    标签: jsp struts tiles


    【解决方案1】:

    如果这对以后的任何人有帮助: 经过多次令人眼花缭乱的论坛浏览和documentation的重读,我能够破译正确的标签使用是

    <tiles:useAttribute id="beanId" name="beanNameHere" classname="theBeanClass" scope="request"/>.

    这在请求范围内声明了一个 Java 变量,然后可以像任何其他 bean 一样访问它,在我的例子中使用 EL,例如:

    <c:if test="${beanId.class.simpleName == 'someInterestingClass'}">
        <c:out value="${beanId.someInterestingProperty}"/> 
    </c:if>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 1970-01-01
      相关资源
      最近更新 更多