ActionBean在页面加载时始终为null

时间:2014-07-24 21:36:39

标签: java stripes

我正在使用Stripes构建一个小型Java应用程序。我可以回发到我的ActionBeans,但是在页面加载$(actionBean == null)上总是返回true。为了缩小可能的问题,我正在使用示例Hello World程序。

我的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";
}

和我的jsp页面:

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

当我在ActionBean中设置断点时,它们不会在页面加载时被点击,因此似乎可能没有发生绑定。我正在使用NetBeans的Apache / Tomcat默认值。这可能是一个简单的解决方案,但官方文档之外的Stripes文档相对较少。

1 个答案:

答案 0 :(得分:1)

要使用bean,您需要声明它。

插入此内容:

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

在JSP的顶部像这样:

<%@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">