Struts - Problem with nested iteration or double iteration

R

Rudi

Hi Everyone,

I'm trying to implement a nested iteration in a jsp and can't seem
to get this to work.

Can anyone please help?

Below is all the code. Thanks in advance.

Best regards,

Rudi

=========================================================
bookList.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic"
prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested"
prefix="nested" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<html>
<head>
<title>Show book list</title>
</head>
<body>
<table border="1">
<tbody>

<tr>
<td>Author</td>
<td>Book name</td>
<td>Available</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<%-- check if book exists and display message or iterate over books
--%>
<logic:empty name="bookListForm" property="books">
<tr>
<td colspan="5">No books available</td>
</tr>
</logic:empty>
<logic:notEmpty name="bookListForm" property="books">
<logic:iterate name="bookListForm" property="books" id="book">
<tr>
<%-- print out the book informations --%>
<td><bean:write name="book" property="author" /></td>
<td><bean:write name="book" property="title" /></td>
<td><html:checkbox disabled="true" name="book"
property="available" />
</td>

<%-- print out the edit and delete link for each book --%>
<td><html:link action="bookEdit.do?do=editBook" paramName="book"
paramProperty="id" paramId="id">Edit</html:link></td>
<td><html:link action="bookEdit.do?do=deleteBook"
paramName="book"
paramProperty="id" paramId="id">Delete</html:link></td>
</tr>
</logic:iterate>
</logic:notEmpty>

<%-- end iterate --%>

</tbody>

</table>

<br>

One Iteration
<UL>
<c:forEach var="book" items="${books}">
<LI>Name = ${book.author}
</c:forEach>
</UL>

<UL>
<c:forEach var="book" items="${books}">
<LI>Name = <c:eek:ut value="${book.author}" />
</c:forEach>
</UL>


<!-- I tried this to get at least the top level name, but didn't work.
I'm not sure
how to setup the inner forEach correctly. I tried but also get
errors.
Error message is can't find bean -->

<UL>
<c:forEach var="bookSection" items="$
{bookListForm.bookSectionList}">
<LI>Name = <c:eek:ut value="${booksection.sectionName}" />
</c:forEach>
</UL>


<!-- The code below doesn't work. Error message is unbalanced
nested:nest tag -->

<nested:nest property="bookSection" />
<b><nested:write property="sectionName" /></b>
<nested:iterate property="book" />
<UL>
<LI><nested:write property="title" /></LI>
</UL>
</nested:iterate>
</nested:nest>


</body>
</html>

=========================================================
Book.java

package com.mycompany.client;

public class Book implements java.io.Serializable {

private static final long serialVersionUID = 1L;

private long id;
private String title;
private String author;
private String available;

public Book() {}

public Book(long id, String title, String author, String available) {
this.id = id;
this.title = title;
this.author = author;
this.available = available;
}

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getAvailable() {
return available;
}

public void setAvailable(String available) {
this.available = available;
}

}

=========================================================
BookSection.java

package com.mycompany.client;

import java.util.ArrayList;
import java.util.List;

public class BookSection implements java.io.Serializable {

private static final long serialVersionUID = 1L;

private List<Book> bookSec = new ArrayList<Book>();
private Book latestBook;
private String sectionName = null;

public List<Book> getBookSec() {
return bookSec;
}

public void setBookSec(List<Book> bookSec) {
this.bookSec = bookSec;
}

public Book getLatestBook() {
return latestBook;
}

public void setLatestBook(Book latestBook) {
this.latestBook = latestBook;
}

public String getsSectionName() {
return sectionName;
}

public void setSectionName(String sectionName) {
this.sectionName = sectionName;
}


}

=========================================================
BookListForm.java

package com.mycompany.client.struts.form;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import com.mycompany.client.Book;
import com.mycompany.client.BookSection;

public class BookListForm extends ActionForm {

private Collection books;
private List<BookSection> bookSectionList;


public Collection getBooks() {
return books;
}

public void setBooks(Collection books) {
this.books = books;
}

public void reset(ActionMapping mapping, HttpServletRequest request)
{
books = new ArrayList();
}

public List<BookSection> getBookSectionList() {
return bookSectionList;
}

public void setBookSectionList(List<BookSection> bookSectionList) {
this.bookSectionList = bookSectionList;
}

}

=========================================================
BookListAction.java

package com.mycompany.client.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.html.HTMLDocument.Iterator;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.DynaActionForm;

import com.mycompany.client.Book;
import com.mycompany.client.struts.form.BookListForm;
import com.mycompany.client.hibernate.*;

import org.hibernate.*;
import org.hibernate.criterion.Projections;

import com.mycompany.client.BookSection;

import java.util.*;

import java.util.ArrayList;
import java.util.List;

public class BookListAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

BookListForm bookListForm = (BookListForm) form;

SessionFactory factory = null;
Session session = null;
Collection <Book> books = new ArrayList<Book>();
String author = null;
String title = null;

try {

factory = HibernateSessionFactory.getSessionFactory();
session = (Session) factory.openSession();

List<Book>bks = session.createQuery("from Book ").list();

System.out.println("Query Size = " + bks.size());

if (bks.isEmpty()) {
System.out.println("Could not get book using embedded
query");
} else {
for (int i = 0; i < bks.size(); i++) {
System.out.println("The title is " +
bks.get(i).getTitle());
}
}


// Declare a BookSection List
List<BookSection> booksList = new ArrayList<BookSection>();

// Declare a BookSection
BookSection bs1 = new BookSection();
List<Book> sectionBook = new ArrayList<Book>();

Book book1 = new Book();
book1.setId(1);
book1.setAuthor("Tom Clancy");
book1.setTitle("The Hunt For Red October");
book1.setAvailable("Y");
sectionBook.add(book1);

bs1.setSectionName("Mystery");

Book book1Latest = new Book();
book1Latest.setId(4);
book1Latest.setAuthor("Agatha Christie");
book1Latest.setTitle("And Then There Were None");
book1Latest.setAvailable("Y");
bs1.setLatestBook(book1Latest);

// Declare another BookSection
BookSection bs2 = new BookSection();
List<Book> sectionBook2 = new ArrayList<Book>();

Book book2 = new Book();
book2.setId(1);
book2.setAuthor("Naomi Wolf");
book2.setTitle("Give Me Liberty");
book2.setAvailable("Y");
sectionBook2.add(book2);

bs2.setSectionName("History");

Book book2Latest = new Book();
book2Latest.setId(4);
book2Latest.setAuthor("Bob Woodward");
book2Latest.setTitle("The War Within");
book2Latest.setAvailable("Y");
bs2.setLatestBook(book2Latest);

booksList.add(bs1);
booksList.add(bs2);


// Reset and assign the list to the Form attribute
// and the request
bookListForm.reset(mapping, request);
bookListForm.setBooks(bks);
request.setAttribute("books", bks);

request.setAttribute("booksect", booksList);

bookListForm.setBookSectionList(booksList);


} finally {
session.close();
}

return mapping.findForward("showList");

}
}
 
D

Donkey Hottie

<!-- The code below doesn't work. Error message is unbalanced
nested:nest tag -->

<nested:nest property="bookSection" />
<b><nested:write property="sectionName" /></b>
<nested:iterate property="book" />
<UL>
<LI><nested:write property="title" /></LI>
</UL>
</nested:iterate>
</nested:nest>

You end the nested:nest row with />

It should of course be >


<nested:nest property="bookSection">
<b><nested:write property="sectionName" /></b>
<nested:iterate property="book" />
<UL>
<LI><nested:write property="title" /></LI>
</UL>
</nested:iterate>
</nested:nest>
 
R

Rudi

You end the nested:nest row with />

It should of course be >

<nested:nest property="bookSection">
  <b><nested:write property="sectionName" /></b>
  <nested:iterate property="book" />
     <UL>
       <LI><nested:write property="title" /></LI>
     </UL>
  </nested:iterate>
</nested:nest>


Hi DH,

Thanks for the reply.

I tried your suggestion, but it didn't work. Still getting the
following error:

org.apache.jasper.JasperException: /jsp/bookList.jsp(87,2) The end
tag
"&lt;/nested:iterate" is unbalanced

I'm using MyEclipse. JSTL 1.1.2. Struts 1.1 libraries.

Thanks,

Rudi
 
D

Donkey Hottie

Hi DH,

Thanks for the reply.

I tried your suggestion, but it didn't work. Still getting the
following error:

org.apache.jasper.JasperException: /jsp/bookList.jsp(87,2) The end
tag
"&lt;/nested:iterate" is unbalanced

That's because you did the same mistake with nested:iterate tag! Note
that I fixed nested:nest that you said failed!

The correct form is:

<nested:nest property="bookSection">
  <b><nested:write property="sectionName" /></b>
  <nested:iterate property="book">
     <UL>
       <LI><nested:write property="title" /></LI>
     </UL>
  </nested:iterate>
</nested:nest>


You NEED to learn basics of XML or HTML if you code like this!

Ending a tag with /> really ends it on the same row it started. Ending it
later with </tag> is extra!
 
R

Rudi

That's because you did the same mistake with nested:iterate tag! Note
that I fixed nested:nest that you said failed!

The correct form is:

<nested:nest property="bookSection">
  <b><nested:write property="sectionName" /></b>
  <nested:iterate property="book">
     <UL>
       <LI><nested:write property="title" /></LI>
     </UL>
  </nested:iterate>
</nested:nest>

You NEED to learn basics of XML or HTML if you code like this!

Ending a tag with /> really ends it on the same row it started. Ending it
later with </tag> is extra!


Hi DH,

Thanks for the reply, but please try to keep things civil. It seems
that you're
getting all upset over stuff. No need for exclamation marks and no
need to express
yourself that way.

You know what, if you look at your first post, I copied and pasted
the code from
that and didn't realize that there was that / in the book part. You
fixed the bookSection
but not book. I realized that later when trying different things to
make that work and
fixed that part in my code. Then after trying some things I came here
and saw your post.

Anyway, taking all the / out from bookSection and book, still didn't
work.

Error:

org.apache.jasper.JasperException: An exception occurred processing
JSP page /jsp/bookList.jsp
at line 80

That's the sectionName line that seems to work in whatever
environment you're running.

javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot
find bean in any scope


Anyway, I decided to forget about nested tags for now and used a
forEach to make things work:

<UL>
<c:forEach var="bookSection" items="${booksect}">
<LI>Section Name = <c:eek:ut value="${bookSection.sectionName}" />

<br>
<c:forEach var="book" items="${bookSection.bookSec}">
<LI>Book Title = <c:eek:ut value="${book.title}" />
</c:forEach>
<br><br>
</c:forEach>
</UL>

And that works fine.

Thanks for trying to help. Yes, there're many things that I need to
learn,
but politeness is not one of them.

Best regards,

Rudi
 
R

Rudi

That is no way to show gratitude!

Donkey Hottie helped you, and even gave you coaching as to what is a minimum
level of expertise to which to study, and you get all snotty.

Shame.


Lew,

I did write thanks to Donkey and appreciated the help. However, I
wrote to
"keep things civil" because of the way he wrote back. He sounded (at
least I
thought) like he was upset (because of the exclamation marks and the
way he
wrote things).

Maybe I misinterpreted the post. I thought that he was upset because
I said
that the solution that he suggested didn't work for me. I wasn't
questioning
his answer, just saying that it didn't work in my enviroment even
after removing
those two (2) slashes (even though I posted the code wrong with one
having a slash).

In any event, this material is new for me and I know that I have a
very long and
difficult road ahead of me. So if people point out something that I
need to read I
don't mind. However, since people are not talking directly, one can
only go by how
the other person writes to figure out how they're expressing
themselves. I don't want
to make too much out of this and just leave it as a misunderstanding
on my part.

I'm not here to offend anyone, sound ungrateful, or look for
trouble.

Thanks again for the help.

Peace,

Rudi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top