[英]accessing static field of class from jsp
In a web app, my servlet is printing html as below在 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>" );
}
where printBody
is 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>" );
}
The BettingPool
class has some static fields and their accessors BettingPool
class 有一些 static 字段及其访问器
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我想使用 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>
However,I am getting an error like但是,我收到一个错误,例如
javax.el.PropertyNotFoundException
: Property 'tieBreakerIndex' not found on typedyna.pool.BettingPool
javax.el.PropertyNotFoundException
:在类型dyna.pool.BettingPool
上找不到属性“tieBreakerIndex”
Any idea how I can access the static field of the class from the jsp page?知道如何从 jsp 页面访问 class 的 static 字段吗? thanks
谢谢
mark.标记。
Just remove the static
method, in other word make it non static getter method.Because JSTL EL looks for standard accessors method.只需删除
static
方法,换句话说,使其成为非 static getter 方法。因为 JSTL EL 寻找标准访问器方法。
Call your static method differently by adding an import of your class to your jsp at the top.通过将 class 的导入添加到顶部的 jsp 以不同方式调用 static 方法。
<%@ page import = "yourpackage.BettingPool" %>
.....
.....
<c:if test="${status.index == BettingPool.getTieBreakerIndex()}">
checked
</c:if>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.