Search code examples
jspliferayjstlelscriptlet

JSP's iterating using jstl


In this code I get authorId for each author's name from bookListArray and show it for each book, using iterating element aBook. How can I refactor my code with no scriplets? How can I iterate my bookListArray in .java code?

<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 />

It's my author and book lists:

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);

if you want to look at my project you can do it here: https://github.com/AlBoldyrev/Library/blob/master/bundles/tomcat-7.0.62/webapps/LibraryBook-portlet/WEB-INF/src/com/softwerke/BookAndAuthor.java


Solution

  • The answer was not so easy. First of all, in my situation, I can only show data from database tables. Therefore, to solve this problem, we should create 2 new model classes to hold there important variables.

    I understand, that it wasn't one-string-question, but Im a bit dissapointed that no one gives even any ideas, because for professional it is very easy.

    So:

    1. We should create new classes AuthorModel and BookModel (I'll omit all package and import info):

      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();
    }
    }
    
    1. After that, we should use this classes in our portlet class:

    List<BookModel> bookModelList = ArrayUtil.getAllBooks();
    List<AuthorModel> authorModelList = ArrayUtil.getAllAuthors();
    request.setAttribute("bookModels", bookModelList);
    request.setAttribute("authorModels", authorModelList);
    

    1. And our getAllBooks()/getAllAuthors() methods:

    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;
    }
    }