从jsp访问类的静态字段

发布于 2024-11-04 09:19:38 字数 3286 浏览 1 评论 0原文

在 Web 应用程序中,我的 servlet 打印 html,如下所示

protected void doGet( HttpServletRequest request, HttpServletResponse response )
            throws ServletException, IOException {
        response.setContentType( "text/html" );
        PrintWriter pw = response.getWriter();

        pw.println( "<html><head></head><body>" );
        printBody( pw );
        pw.println( "</body></html>" );
    }

,其中 printBody

private void printBody( PrintWriter pw ) {
        pw.println( "<form id='pool' method='POST'>" );
        pw.println( "<table>" );
        pw.println( "<tr><th>Home Team</th><th>Away Team</th><th>Tiebreaker?</th></tr>" );

        BettingPoolGame[] games = BettingPool.getGames();
        for (int i = 0; i < games.length; i++) {
             pw.println( "<tr><td><input name='home" + i + "' value='" + games[i].getHomeTeam() + "'></td>" );
             pw.println( "<td><input name='away" + i + "' value='" + games[i].getAwayTeam() + "'></td>" );
             pw.print( "<td><input type='radio' name='tiebreaker' value='" + i + "'" );
             if (i == BettingPool.getTieBreakerIndex()) pw.print( " checked" );
             pw.println( " /></td></tr>" );

        }
        pw.println( "</table>" );
        if (BettingPool.isEditable()) {
            pw.println( "<input type='submit' name='save' value='Save' />" );
            pw.println( "<input type='submit' name='save' value='Open Pool' />" );
        }
        pw.println( "</form>" );
    }

BettingPool 类有一些静态字段及其访问器,

public class BettingPool {
     private static int tieBreakerIndex;
     ...
     public static int getTieBreakerIndex() {
        return tieBreakerIndex;
    }
    ...
  }

我想使用 jsp 页面而不是printBody() 方法并尝试了这个

<body>
<jsp:useBean id="bettingpool" class="dyna.pool.BettingPool"></jsp:useBean>
<h3>pooleditorform</h3>
<form id='pool' method='POST'>
    <table>
        <tr><th>Home Team</th><th>Away Team</th><th>Tiebreaker?</th></tr>
        <c:forEach items="${games}" var="x" varStatus="status">
            <tr><td><input name='home${status.index}' value='${x._homeTeam}' ></td>
            <td><input name='away${status.index }' value='${x._awayTeam}'></td>
            <td><input type='radio' name='tiebreaker' value='${status.index}'/></td></tr>
            <c:if test="${status.index == bettingpool.tieBreakerIndex}">
                checked
            </c:if>
        </c:forEach>
    </table>
    <input type='submit' name='save' value='Save'/>
    <input type='submit' name='save' value='Open Pool' />
</form>
</body>

但是,我收到了类似的错误

javax.el.PropertyNotFoundException:在类型 dyna.pool.BettingPool 上找不到属性“tieBreakerIndex”

知道如何从 jsp 页面访问该类的静态字段吗? 谢谢马克

In a web app, my servlet is printing html as below

protected void doGet( HttpServletRequest request, HttpServletResponse response )
            throws ServletException, IOException {
        response.setContentType( "text/html" );
        PrintWriter pw = response.getWriter();

        pw.println( "<html><head></head><body>" );
        printBody( pw );
        pw.println( "</body></html>" );
    }

where printBody is

private void printBody( PrintWriter pw ) {
        pw.println( "<form id='pool' method='POST'>" );
        pw.println( "<table>" );
        pw.println( "<tr><th>Home Team</th><th>Away Team</th><th>Tiebreaker?</th></tr>" );

        BettingPoolGame[] games = BettingPool.getGames();
        for (int i = 0; i < games.length; i++) {
             pw.println( "<tr><td><input name='home" + i + "' value='" + games[i].getHomeTeam() + "'></td>" );
             pw.println( "<td><input name='away" + i + "' value='" + games[i].getAwayTeam() + "'></td>" );
             pw.print( "<td><input type='radio' name='tiebreaker' value='" + i + "'" );
             if (i == BettingPool.getTieBreakerIndex()) pw.print( " checked" );
             pw.println( " /></td></tr>" );

        }
        pw.println( "</table>" );
        if (BettingPool.isEditable()) {
            pw.println( "<input type='submit' name='save' value='Save' />" );
            pw.println( "<input type='submit' name='save' value='Open Pool' />" );
        }
        pw.println( "</form>" );
    }

The BettingPool class has some static fields and their accessors

public class BettingPool {
     private static int tieBreakerIndex;
     ...
     public static int getTieBreakerIndex() {
        return tieBreakerIndex;
    }
    ...
  }

I would like to use a jsp page instead of the printBody() method and tried this

<body>
<jsp:useBean id="bettingpool" class="dyna.pool.BettingPool"></jsp:useBean>
<h3>pooleditorform</h3>
<form id='pool' method='POST'>
    <table>
        <tr><th>Home Team</th><th>Away Team</th><th>Tiebreaker?</th></tr>
        <c:forEach items="${games}" var="x" varStatus="status">
            <tr><td><input name='home${status.index}' value='${x._homeTeam}' ></td>
            <td><input name='away${status.index }' value='${x._awayTeam}'></td>
            <td><input type='radio' name='tiebreaker' value='${status.index}'/></td></tr>
            <c:if test="${status.index == bettingpool.tieBreakerIndex}">
                checked
            </c:if>
        </c:forEach>
    </table>
    <input type='submit' name='save' value='Save'/>
    <input type='submit' name='save' value='Open Pool' />
</form>
</body>

However,I am getting an error like

javax.el.PropertyNotFoundException: Property 'tieBreakerIndex' not found on type dyna.pool.BettingPool

Any idea how I can access the static field of the class from the jsp page?
thanks

mark.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕花眠 2024-11-11 09:19:38

只需删除 static 方法,换句话说,使其成为非静态 getter 方法。因为 JSTL EL 会查找标准访问器方法。

Just remove the static method , in other word make it non static getter method .Because JSTL EL looks for standard accessors method.

护你周全 2024-11-11 09:19:38

通过将类的导入添加到顶部的 jsp 中,以不同的方式调用静态方法。

<%@ page import = "yourpackage.BettingPool" %>
.....
.....
<c:if test="${status.index == BettingPool.getTieBreakerIndex()}">
            checked
        </c:if>

Call your static method differently by adding an import of your class to your jsp at the top.

<%@ page import = "yourpackage.BettingPool" %>
.....
.....
<c:if test="${status.index == BettingPool.getTieBreakerIndex()}">
            checked
        </c:if>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文