我有一个bean矢量,包含我想在jsp页面中显示的信息。我目前只是使用标准的java表达式来显示它,我想研究使用jstl来分离问题。这有可能,怎么样?我一直在谷歌搜索,但我似乎找不到任何东西。

有帮助吗?

解决方案

我认为你要找的是 <!> lt; c:foreach <!> gt; 标记。

例如,在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)

其他提示

要花费汤姆的例子,这里有更具体的内容:

<c:foreach items="${myList}" var="myItem">
  <c:out value="${myItem.someProperty}"/>
</c:foreach>

其中<!> quot; myList <!> quot;是一个请求属性,其中包含您的向量。

一个常见的错误是忘记$ {myList}周围的$ {} - 如果你这样做,JSTL不会抛出错误,它只会为你生成一个包含单个项目的列表,字符串<! > QUOT; myList中<> QUOT;!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top