2016-09-13 24 views
-1

In diesem Code bekomme ich authorId für jeden Autor den Namen von bookListArray und zeigen Sie es für jedes Buch, mit Iterationselement aBook. Wie kann ich meinen Code ohne Skripts umgestalten? Wie kann ich meine bookListArray in .java Code wiederholen?JSP iteriert mit JSTL

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

Es ist mein Autor und Buchlisten:

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

, wenn Sie an meinem Projekt suchen möchten, können Sie das hier tun: https://github.com/AlBoldyrev/Library/blob/master/bundles/tomcat-7.0.62/webapps/LibraryBook-portlet/WEB-INF/src/com/softwerke/BookAndAuthor.java

+0

@Parkash Ich brauche deine Hilfe! : P – German

+0

Wo ist bookListArray, kann auch diesen Code enthalten –

+0

@ B'bekShakya Ich habe meine Frage aktualisiert – German

Antwort

0

Die Antwort war nicht so einfach . Zuallererst kann ich in meiner Situation nur Daten aus Datenbanktabellen anzeigen. Um dieses Problem zu lösen, sollten wir daher zwei neue Modellklassen erstellen, um dort wichtige Variablen zu speichern.

Ich verstehe, dass es keine Ein-String-Frage war, aber ich bin ein bisschen enttäuscht, dass niemand sogar irgendwelche Ideen gibt, weil für den Profi es sehr einfach ist.

So:

  1. Wir sollten neue Klassen erstellen AuthorModel und BookModel (Ich werde alles Paket auslassen und importieren 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. Danach sollen wir diese Klassen in unserer Portlet-Klasse verwenden:

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

  1. Und unser getAllBooks()/getAllAuthors() Methoden:

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

T Der beste Ansatz dafür besteht darin, die Liste in Ihrem Controller abzurufen und sie der Anforderung als Attribute hinzuzufügen. foreach: Dann können Sie es mit c iterieren

Beispiel:

Controller:

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> 

Dann müssen Sie so etwas wie dies in Ihre SearchContainer umfassen .

Verwandte Themen