在此代码中,我从bookListArray
获取每个作者姓名的authorId,并使用迭代元素aBook
为每本书显示该名称。如何在不使用scriplet的情况下重构代码?如何在.java代码中迭代bookListArray
?
<liferay-ui:search-container>
<liferay-ui:search-container-results results="${bookListArray}" />
<liferay-ui:search-container-row className="builder.model.Book" keyProperty="bookId" modelVar="aBook">
<liferay-ui:search-container-column-text property="bookName" name="book-Name" />
<liferay-ui:search-container-column-text property="bookDescription" name="description" />
<%
Author bookAuthor = AuthorLocalServiceUtil.getAuthor(aBook.getAuthorId());
%>
<liferay-ui:search-container-column-text value="<%=bookAuthor.getAuthorName() %>" name="Author" />
<liferay-ui:search-container-column-jsp path="/html/actionBook.jsp" align="right" />
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
这是我的作者和图书清单:
List<Book> bookList = BookLocalServiceUtil.getBooks(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
List<Author> authorList = AuthorLocalServiceUtil.getAuthors(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
request.setAttribute("bookListArray", bookList);
request.setAttribute("authorListArray", authorList);
如果您想查看我的项目,可以在此处执行:https://github.com/AlBoldyrev/Library/blob/master/bundles/tomcat-7.0.62/webapps/LibraryBook-portlet/WEB-INF/src/com/softwerke/BookAndAuthor.java
答案 0 :(得分:0)
答案并不那么容易。首先,在我的情况下,我只能显示数据库表中的数据。因此,要解决这个问题,我们应该创建2个新的模型类来保存重要的变量。
我理解,这不是一个字符串问题,但我有点失望,没有人给出任何想法,因为对于专业人士来说这很容易。
所以:
我们应该创建新的类AuthorModel
和BookModel
(我将省略所有包和导入信息):
public class AuthorModel {
private Author author;
private int count;
public AuthorModel(Author author) {
this.author = author;
this.count = BookLocalServiceUtil.countByAuthor(author.getAuthorId());
}
public String getAuthorName() {
return author.getAuthorName();
}
public long getAuthorId() {
return author.getAuthorId();
}
public int getCount() {
return count;
}
}
-
public class BookModel {
private Book book;
private Author author;
private long authorId;
public BookModel(Book book) {
this.book = book;
}
public String getBookName() {
return book.getBookName();
}
public String getBookDescription() {
return book.getBookDescription();
}
public String getAuthorName() throws PortalException, SystemException {
this.authorId = book.getAuthorId();
author = AuthorLocalServiceUtil.getAuthor(authorId);
String authorName = author.getAuthorName();
return authorName;
}
public long getBookId() {
return book.getBookId();
}
}
List<BookModel> bookModelList = ArrayUtil.getAllBooks();
List<AuthorModel> authorModelList = ArrayUtil.getAllAuthors();
request.setAttribute("bookModels", bookModelList);
request.setAttribute("authorModels", authorModelList);
public class ArrayUtil {
protected static List<AuthorModel> getAllAuthors() throws SystemException{
List<AuthorModel> authors = new ArrayList<AuthorModel>();
List<Author> authorList = AuthorLocalServiceUtil.getAuthors(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
for (Author author : authorList) {
authors.add(new AuthorModel(author));
}
return authors;
}
protected static List<BookModel> getAllBooks() throws SystemException{
List<BookModel> books = new ArrayList<BookModel>();
List<Book> bookList = BookLocalServiceUtil.getBooks(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
for (Book book: bookList) {
books.add(new BookModel(book));
}
return books;
}
}
答案 1 :(得分:0)
此方法的最佳方法是检索控制器中的列表并将其作为属性添加到请求中。 然后你可以用c:foreach:
迭代它示例:
控制器:
List<Author> authorList = AuthorLocalServiceUtil.getAuthors(QueryUtil.ALL_POS, QueryUtil.ALL_POS);
request.setAttribute("authorModels", authorList);
JSP:
<c:foreach items="${authorList}" var="author">
<c:out value="${author.attr1}"/>
<c:out value="${author.attr2}"/>
</c:foreach>
然后你必须在SearchContainer中包含这样的内容。