我有一个包含我想在我的 jsp 页面中显示的信息的 bean 向量。我目前只是使用标准的 java 表达式来显示这个,我想研究使用 jstl 来分离关注点。这可能吗,怎么做?我一直在谷歌搜索,但我似乎找不到任何东西。
问问题
3712 次
2 回答
1
我认为您正在寻找的是< c:foreach >标签。
例如,在 MyClass 的实例上打印值 myInt 属性(定义如下):
<c:foreach items="${vectors name}" var="pos" >
<!-- print the value of myInt for each position of the array.
Method getMyInt() must exist in pos object.-->
<c:out value="${pos.myInt}"/>
<!-- print the value of myInt for each composed instance.
Method getRelatedInstance() must exist in pos object. -->
<c:out value="${pos.relatedInstance.myInt}"/>
</c:foreach>
其中向量名称是向量的名称,例如,在执行
假设你有一个类 myClass。
public class MyClass{
private MyClass relatedInstance;
//some members and methods
public int getMyInt(){
//return something
}
public MyClass getRelatedInstance(){
return this.relatedInstance;
}
List<myClass> my_vector = getFilledList();
request.setAttribute("vectors name",my_vector)
于 2009-05-03T21:51:00.180 回答
0
为了扩展 Tom 的示例,这里有一些更具体的内容:
<c:foreach items="${myList}" var="myItem">
<c:out value="${myItem.someProperty}"/>
</c:foreach>
其中“myList”是包含您的向量的请求属性。
一个常见的错误是忘记了 ${myList} 周围的 ${} - 如果这样做,JSTL 不会抛出错误,它只会为您生成一个包含单个项目的列表,即字符串“myList”。
于 2009-05-03T21:57:03.127 回答