速度模板中的arraylist(javaObect)迭代

时间:2017-02-16 21:45:42

标签: foreach velocity

我有两个表作者,书籍。和POJO,每个表的.hbm.xml。我写了一个select查询,它给出了两个表中的一些细节。

select A.id.BookNum,A.id.AuthorName,B.id.DatePublished,B.id.Price
,B.id.Condition,B.id.BookBought from Author A, Book B where
B.id.Condition == 'NEW' and  A.id.BookNum = A.id.BookNum order by
A.id.AuthorName;

我得到了结果,并将其存储在ArrayList中。 UnApprovedBookList当我尝试在力度模板中显示它时,我无法迭代这个。我把我的POJO作为Author,Book分别对AuthorId和BookId有getter()/ Setter(),而AuthorId,BookId对其中的字段有getter / setter。

    public class Book implements java.io.Serializable {
        private static final long serialVersionUID = 1L;
        private BookId id;
        public Book (){ }
        public Book (BookId id){this.id = id;}
        public BookId getId(){return id;}
        public void setId(BookId id) {this.id = id;}
    }

和我的BookId中包含字段的getter / setter,基本上是所有表格列。

public class BookId implements java.io.Serializable {
        private static final long serialVersionUID = 1L;
        private BookNum;
        public BookId (){   }
        public String getBookNumid() {return BookNum;}
        public void setBookNum(String bookNum) {bookNum= bookNum;}
      }

我的hbm如下:

    <hibernate-mapping>
        <class name="com.bookStore.hibernate.dao.Book" table="BOOK" schema="ORG">
            <composite-id name="id" class="com.bookStore.hibernate.dao.BookId">
                <key-property name="BookNum" type="string">
                    <column name="SH_BOOK_NUM" length="17" />
                </key-property>
                <key-property name="DatePublished" type="date">
                    <column name="SH_DATE_PUB" length="13" />
                </key-property>
                <key-property name="Price" type="int">
                    <column name="SH_PRICE" length="5" />
                </key-property>
                <key-property name="Condition" type="string">
                    <column name="SH_CONDITION" length="10" />
                </key-property>
                <key-property name="BookBought " type="date">
                    <column name="SH_BOOK_BOUGHT" length="13" />
                </key-property>
            </composite-id>
        </class>
    </hibernate-mapping>  

当我尝试迭代并在Velocity中显示时,我发现很难。我的代码片段如下:

      #foreach($unApprovedBook in $unApprovedBookList)
           <input type="text" id="bookNum" value="$unApprovedBook.getId().getBookNumid()">
         #set($i = $i + 1)

通过这样做,我在UI中看到 $ unApprovedBook.getId()。getBookNumid() 如果我将代码更改为:

#foreach($unApprovedBook in $unApprovedBookList)
<input type="text" id="bookNum" value="$unApprovedBook">
#set($i = $i + 1)

我看到:UI中的 [Ljava.lang.Object; @ 3160316 。 请让我知道如何在Velocity中迭代我的javaobject。

  [1]: https://i.stack.imgur.com/eBrYQ.jpg

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

您应该使用属性表达式$unApprovedBook.id.BookNum。尝试遵循Java命名约定。

  <input type="text" id="bookNum" value="${unApprovedBook.id.BookNum}">