【问题标题】:Using Stripes, can I include the output resolution of an ActionBean into a jsp?使用 Stripes,我可以将 ActionBean 的输出分辨率包含到 jsp 中吗?
【发布时间】:2012-08-22 15:34:28
【问题描述】:

我发现了一种情况,我想通过导入共享 ActionBean 的输出在许多页面上包含相同的内容。

我想做的是有一个 ActionBean,它接受一些参数并进行一些处理,然后将 ForwardResolution 返回给 JSP,JSP 使用标准 Stripes 构造(如 ${actionBean.myValue)呈现该 ActionBean 的输出。

然后我想从其他 JSP 中“调用”这个 ActionBean。这会产生将第一个 ActionBean 的输出 HTML 放入第二个 JSP 的输出的效果。

我该怎么做?

【问题讨论】:

    标签: java jsp jstl stripes


    【解决方案1】:

    使用<jsp:include>标签可以得到想要的结果。

    SharedContentBean.java

    @UrlBinding("/sharedContent")
    public class SharedContentBean implements ActionBean {
    
        String contentParam;
    
        @DefaultHandler
        public Resolution view() {
            return new ForwardResolution("/sharedContent.jsp");
        }
    }
    

    在您的 JSP 中

    <!-- Import Registration Form here -->
    <jsp:include page="/sharedContent">
        <jsp:param value="myValue" name="contentParam"/>
    </jsp:include>
    

    web.xml

    确保将INCLUDE 添加到web.xml 中的&lt;filter-mapping&gt; 标记中:

    <filter-mapping>
        <filter-name>StripesFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <servlet-name>StripesDispatcher</servlet-name>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    

    【讨论】:

    • Stripes 有一个非常强大的布局机制,如果你发现自己经常这样做的话。 stripesframework.org/display/stripes/Layout+Reuse
    • 谢谢。我们使用 Stripes Layout 标签。在这种情况下,我们想要导入的组件不在一个一致的位置,并且我们不希望它出现在 每个 页面上,所以我认为 Stripes 布局结构不会对这种情况有所帮助。可能我错过了一些东西。
    【解决方案2】:

    让您希望包含相同内容的每个 ActionBean 扩展相同的 BaseAction 并将 getter/setter 放入其中。例如:

    BaseAction.class

    package com.foo.bar;
    
    public class BaseAction implements ActionBean {
    
      private ActionBeanContext context;
    
      public ActionBeanContext getContext() { return context; }
      public void setContext(ActionBeanContext context) { this.context = context; }
    
      public String getSharedString() {
        return "Hello World!";
      }
    
    }
    

    index.jsp

    <html>
      <jsp:useBean id="blah" scope="page" class="com.foo.bar.BaseAction"/>
      <body>
        ${blah.sharedString}
      </body>
    </html>
    

    【讨论】:

    • 这是一个过于笼统的解决方案,在这种情况下,我希望 HTML (jsp) 也位于 java 代码的外部。
    • 更新了 JSP 以反映您的需求的新变化。
    • 这个方案不太适合返回复杂的信息,比如getSharedString()应该可以返回一个jsp的输出。此解决方案也不是非常 IoC,因为可能存在 ActionBean 需要多个 BaseAction bean 的情况。抱歉,我应该在之前的评论中解释过。
    • “多个 BaseAction bean”没有任何意义,因为它是其他操作 bean 扩展的基本操作 bean。如果您希望在这里获得帮助,恐怕您将不得不准确地解释您要完成的工作。
    • 如果您有一个 BaseAction bean,那么您将在全球范围内公开所有组件。正如我所说,我希望能够在其他页面中包含 HTML 小部件,而不仅仅是原始文本。我已经自己回答了这个问题,请随时查看我的 SO 答案以获得正确的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多