【发布时间】:2012-05-23 04:30:37
【问题描述】:
我正在构建一个 UI 模板,因此没有 java 文件。但是我需要 java 对象,所以我的 jsp 中只有类定义。我需要在“结果”数组中打印每个对象的 gameState 属性。怎么打印?
这行“${sessionScope['results'][innerItemNumber].gameState}[br]”有问题。我不确定语法。
我收到错误“javax.el.PropertyNotFoundException: Property 'gameState' not readable on type java.lang.String”。
test.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
class Result {
String gameState;
String result;
String optional;
public Result(String gameState, String result, String optional) {
this.gameState = gameState;
this.result = result;
this.optional = optional;
}....
.....// Getters & setters
}
%>
<% java.util.ArrayList<Result> results = new java.util.ArrayList<Result>();
results.add(new Result("Kick off", "14:00", ""));
results.add(new Result("1st Half", "2-2", ""));
results.add(new Result("1st Half", "2-1", ""));
results.add(new Result("Peanalties", "3-6", "(1-1 FT)"));
results.add(new Result("2nd Half", "2-0", ""));
%>
....
.....
<c:forEach items="0,1,2,3,4,5,6" var="innerItemNumber">
<cell class="vertical-align-middle" width="33%" text-align="center">
${sessionScope['results'][innerItemNumber].gameState}[br] ?????????
</cell>
</c:forEach >
【问题讨论】:
-
希望您需要致电
${sessionScope.results}或<% session.getAttribute("results"); %> -
我需要访问 results.Something 的属性,例如 ${sessionScope.results.gameState}。正确的语法是什么?
-
如果你的'results'是一个列表..你可以像${sessionScope.results.get(i).gameState}一样访问,就像你在一个列表上操作一样。查看 JSP 的容器生成代码以更好地理解。
标签: jsp session-variables