【问题标题】:ActionBean is always null on page loadActionBean 在页面加载时始终为空
【发布时间】:2014-07-25 21:42:41
【问题描述】:

我正在使用 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 中设置断点时,它们不会在页面加载时命中,因此看起来可能没有发生绑定。我正在为 Apache/Tomcat 使用 NetBeans 的默认值。这可能是一个简单的解决方案,但除了官方文档之外,关于 Stripes 的文档相对较少。

【问题讨论】:

    标签: java stripes


    【解决方案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">    
    

    【讨论】:

    • 非常感谢!花了一天时间试图弄清楚这一点。想知道为什么它不在示例代码中。
    猜你喜欢
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多