从jsp访问类的静态字段
在 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 typedyna.pool.BettingPool
Any idea how I can access the static field of the class from the jsp page?
thanks
mark.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需删除
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.通过将类的导入添加到顶部的 jsp 中,以不同的方式调用静态方法。
Call your static method differently by adding an import of your class to your jsp at the top.