从jsp访问类的静态字段

时间:2011-04-30 11:09:50

标签: java jsp static el

在网络应用中,我的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页面访问类的静态字段吗? 感谢

标记。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:-1)

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

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