從JSP

2011-04-30 66 views
1

訪問類的靜態字段在一個web應用程序,我的servlet是打印HTML如下從JSP

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頁面而不是printBod Y()方法,並嘗試這種

<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:房產 'tieBreakerIndex' 不是類型發現dyna.pool.BettingPool

任何想法如何,我可以從jsp頁面訪問類的靜態字段? 謝謝

mark。

回答

1

只刪除static方法,換句話說就是使其成爲非靜態的getter方法。因爲JSTL EL尋找標準存取方法。

-1

通過向頂部的jsp添加您的類的導入,以不同的方式調用您的靜態方法。

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

我試圖this..but得到一個錯誤..「功能getTieBreakerIndex必須以前綴使用時沒有指定一個默認的命名空間」 – markjason72 2011-05-01 07:04:13