Question

I have found a situation where I would like to include the same content on many pages by importing the output of an shared ActionBean.

What I would like to do is have an ActionBean which takes some parameters and does some processing and then returns a ForwardResolution to a JSP which renders the output of that ActionBean using standard Stripes constructs like ${actionBean.myValue.

I would then like to "call" this ActionBean from other JSPs. This would have the effect of placing the output HTML from the first ActionBean into the Second JSP's output.

How can I do this?

Was it helpful?

Solution

You can get the desired result by using the <jsp:include> tag.

SharedContentBean.java

@UrlBinding("/sharedContent")
public class SharedContentBean implements ActionBean {

    String contentParam;

    @DefaultHandler
    public Resolution view() {
        return new ForwardResolution("/sharedContent.jsp");
    }
}

In your JSP

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

web.xml

Make sure to add INCLUDE to your <filter-mapping> tag in web.xml:

<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>

OTHER TIPS

Have every ActionBean that you wish to contain the same content extend the same BaseAction and put the getters/setters in there. For instance:

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top