JSP 使用 jstl 进行迭代
JSP's iterating using jstl
在这段代码中,我从 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
答案并不那么容易。首先,在我的情况下,我只能显示数据库表中的数据。因此,要解决这个问题,我们应该创建 2 个新模型 classes 来保存重要变量。
我明白了,这不是一个字符串问题,但我有点失望,因为没有人给出任何想法,因为对于专业人士来说,这很容易。
所以:
我们应该创建新的 classes 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();
}
}
- 之后,我们应该在我们的 portlet 中使用这个 classes class:
List<BookModel> bookModelList = ArrayUtil.getAllBooks();
List<AuthorModel> authorModelList = ArrayUtil.getAllAuthors();
request.setAttribute("bookModels", bookModelList);
request.setAttribute("authorModels", authorModelList);
- 我们的 getAllBooks()/getAllAuthors() 方法:
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;
}
}
最好的方法是在控制器中检索列表并将其作为属性添加到请求中。
然后你可以用 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 中。
在这段代码中,我从 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
答案并不那么容易。首先,在我的情况下,我只能显示数据库表中的数据。因此,要解决这个问题,我们应该创建 2 个新模型 classes 来保存重要变量。
我明白了,这不是一个字符串问题,但我有点失望,因为没有人给出任何想法,因为对于专业人士来说,这很容易。
所以:
我们应该创建新的 classes
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();
}
}
- 之后,我们应该在我们的 portlet 中使用这个 classes class:
List<BookModel> bookModelList = ArrayUtil.getAllBooks();
List<AuthorModel> authorModelList = ArrayUtil.getAllAuthors();
request.setAttribute("bookModels", bookModelList);
request.setAttribute("authorModels", authorModelList);
- 我们的 getAllBooks()/getAllAuthors() 方法:
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;
}
}
最好的方法是在控制器中检索列表并将其作为属性添加到请求中。 然后你可以用 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 中。