Search code examples
javastripes

ActionBean is always null on page load


I'm using Stripes to build a small Java application. I'm able to post back to my ActionBeans, but on page load $(actionBean == null) always returns true. To narrow down the possible issues, I'm using a sample Hello World program.

My ActionBean:

package stripesbook.action;

import java.util.Date;
import java.util.Random;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;

public class HelloActionBean implements ActionBean {/* (1) */
    private ActionBeanContext ctx;
    public ActionBeanContext getContext() { return ctx; }
    public void setContext(ActionBeanContext ctx) { this.ctx = ctx; }

    private Date date;/* (2) */
    public Date getDate() {
        return date;
    }
    @DefaultHandler
    public Resolution currentDate() {/* (3) */
        date = new Date();
        return new ForwardResolution(VIEW);
    }
    public Resolution randomDate() {
        long max = System.currentTimeMillis();
        long random = new Random().nextLong() % max;
        date = new Date(random);
        return new ForwardResolution(VIEW);
    }
    private static final String VIEW = "/hello.jsp";
}

and my jsp page:

<%@page contentType="text/html;charset=ISO-8859-1" language="java"%>
<%@taglib prefix="s" uri="http://stripes.sourceforge.net/stripes.tld"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>Hello, Stripes!</title>
  </head>
  <body>
    <h3>Hello, Stripes!</h3>
    <p>
      Date and time:
      <br>
      <b>
        <p>${actionBean == null}</p>
        <fmt:formatDate type="both" dateStyle="full"
          value="${actionBean.date}"/>
      </b>
    </p>
    <p>
      <s:link beanclass="stripesbook.action.HelloActionBean"
        event="currentDate">
        Show the current date and time
      </s:link> |
      <s:link beanclass="stripesbook.action.HelloActionBean"
        event="randomDate">
        Show a random date and time
      </s:link>
    </p>
  </body>
</html>

When I set breakpoints in the ActionBean, they're not hit on page load, so it seems like maybe the binding is not occurring. I'm using NetBeans' defaults for Apache/Tomcat. This is probably a simple solution but there's relatively little documentation on Stripes outside of the official docs.


Solution

  • To use a bean you need to declare it.

    Insert this:

     <jsp:useBean id="actionBean" class="stripesbook.action.HelloActionBean"/>
    

    At the top of your JSP like this:

    <%@page contentType="text/html;charset=ISO-8859-1" language="java"%>
    <%@taglib prefix="s" uri="http://stripes.sourceforge.net/stripes.tld"%>
    <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
    
    <jsp:useBean id="actionBean" class="stripesbook.action.HelloActionBean"/>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">