Struts - Getter method

M

mpprpp

Hi Everyone,

I'm new working with Struts and also got this
"javax.servlet.jsp.JspException: No getter method for property author
of
bean book" Sorry to post the code, but I'm hoping that someone
could tell me what I'm doing wrong. I haven't been able to figure this
out
and it's not like what was previously mentioned.

The code is as follows:

-- Book.java:

package com.mycompany;

public class Book implements java.io.Serializable {

private static final long serialVersionUID = 1L;

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

public Book() {}

public Book(long id, String title, String author, char 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 char getAvailable() {
return available;
}

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

============================================================

-- BookListForm.java

package com.mycompany.struts.form;

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

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
* MyEclipse Struts
* Creation date: 06-24-2008
*
* XDoclet definition:
* @struts.form name="bookListForm"
*/

public class BookListForm extends ActionForm {

private Collection books;

/**
* @return the books
*/
public Collection getBooks() {
return books;
}

/**
* @param books the books to set
*/
public void setBooks(Collection books) {
this.books = books;
}

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

}

============================================================

-- BookListAction.java


package com.mycompany.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 com.mycompany.form.BookListForm;
import com.mycompany.hibernate.*;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import java.util.Collection;

/**
* MyEclipse Struts
* Creation date: 06-24-2008
*
* XDoclet definition:
* @struts.action path="/bookList" name="bookListForm"
input="/jsp/bookList.jsp" scope="request" validate="true"
*/

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 books = null;

try {

factory = HibernateUtil.getSessionFactory();

session = (Session) factory.openSession();

books = session.createQuery("select id, title, author, available from
Book t
").list();

bookListForm.setBooks(books);

} finally {
session.close();
}

return mapping.findForward("showList");

}
}

============================================================

-- 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"
%>

<html>
<head>
<title>Show book list</title>
</head>
<body>
<table border="1">
<tbody>
<%-- set the header --%>
<tr>
<td>Author</td>
<td>Book name</td>
<td>Available</td>
<td> </td>
<td> </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 information --%>
<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>

<%-- print out the add link --%>
<tr>
<td colspan="5"><html:link action="bookEdit.do?do=addBook">Add a new
book</html:link>
</td>
</tr>

<%-- end interate --%>

</tbody>
</table>
</body>
</html>

============================================================

Sorry for posting the code. Thanks in advance for any help.

Best regards,
 
R

Rudi28

To the OP: We love that you posted source code. It is a good thing. This
group loves source code, as a rule.


The JSP Standard Tag Library (JSTL) has largely supplanted the logic: taglib
from Struts.

<c:forEach>, etc.
<http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html>

You don't have to get into that right away. Learn logic:iterate and c:forEach
will make sense, in a different but similar way.

Sun's JSTL tutorial is a good starting place, when you're ready.
<http://java.sun.com/javaee/5/docs/tutorial/doc/bnakc.html>




Hi guys,

Thanks Joe and Lew for your replies. I'm glad I did the right thing
posting the code then.

I implemented the change that Joe suggested, I replaced:

<logic:iterate name="bookListForm" property="books" id="book">

with

<logic:iterate name="bookListForm" property="books" id="book"
type="com.mycompany.Book">

but I get the following error:

Jun 27, 2008 2:01:21 PM org.apache.catalina.core.ApplicationDispatcher
invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: [Ljava.lang.Object;
at org.apache.jsp.jsp.bookList_jsp._jspService(bookList_jsp.java:133)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
393)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
320)


I'm now reading about JSTL to see what I can do in terms of getting
this to work.

Thanks for your help and any other ideas are welcome. Hope that I
can get this
going soon since I need it for a project at work.
 
R

Rudi

Thanks Joe and Lew for your replies. I'm glad I did the right thing
posting the code then.
I implemented the change that Joe suggested, I replaced:
<logic:iterate name="bookListForm" property="books" id="book">

<logic:iterate name="bookListForm" property="books" id="book"
type="com.mycompany.Book">
but I get the following error:
Jun 27, 2008 2:01:21 PM org.apache.catalina.core.ApplicationDispatcher
invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: [Ljava.lang.Object;
at org.apache.jsp.jsp.bookList_jsp._jspService(bookList_jsp.java:133)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
393)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
320)
I'm now reading about JSTL to see what I can do in terms of getting
this to work.
Thanks for your help and any other ideas are welcome. Hope that I
can get this
going soon since I need it for a project at work.

Sorry I should have caught this the first time through but I am not as
familiar with hibernate. Now that I'm looking again, I don't see anyplace
where you are actually constructing any Book objects. In your
BookListAction code you have this:

books = session.createQuery("select id, title, author, available from Book
t").list();
bookListForm.setBooks(books);

Now if you look at the javadoc api's for the org.hibernate.Query class
you'll find the following explanation for the list() method:

"Return the query results as a List. If the query contains multiple results
pre row, the results are returned in an instance of Object[]."

So in my best estimate, unless there is some hibernate magic at work behind
the scenes here I can't see, you never actually create any
com.mycompany.Book objects.


Hi Joe,

Thanks again for the reply. I'm new at this so it's taking me some
time to get things
going. I appreciate your help.

I think I understand what you're telling me. I'm putting the result
set of the query
(objects) in a Collection, but it's not a collection of type Book.
There's no connection
between the Collection that I defined called books and the actual Book
Class.

So maybe I shouldn't have even defined that Collection in the Book
Class.

I guess what I need to do is iterate through the result set from the
Query, while creating
instances of type Book and setting the attributes of each instance
accordingly with the
corresponding value from the result set (List). As each instance of
the Book object is created,
I need to load those instances in a Collection. Then I would have a
Collection of type Book to
go through and write to the jsp page.

Have a nice day and holiday. Hope that you're doing well. I'll try
to figure out how to do
this and will post again.

Take care,

Ruben
 
R

Rudi

Joe Blow wrote:
Sorry to post the code,
First of all. . .never aplogize for posting the source code when you
are
asking for help with a programming problem.
To the OP: We love that you posted source code. It is a good thing.
This
group loves source code, as a rule.
<logic:iterate name="bookListForm" property="books" id="book"
type="com.mycompany.Book">
The JSP Standard Tag Library (JSTL) has largely supplanted the logic:
taglib
from Struts.
<c:forEach>, etc.
<http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html>
You don't have to get into that right away. Learn logic:iterate and
c:forEach
will make sense, in a different but similar way.
Sun's JSTL tutorial is a good starting place, when you're ready.
<http://java.sun.com/javaee/5/docs/tutorial/doc/bnakc.html>
--
Lew
Hi guys,
Thanks Joe and Lew for your replies. I'm glad I did the right thing
posting the code then.
I implemented the change that Joe suggested, I replaced:
<logic:iterate name="bookListForm" property="books" id="book">
with
<logic:iterate name="bookListForm" property="books" id="book"
type="com.mycompany.Book">
but I get the following error:
Jun 27, 2008 2:01:21 PM org.apache.catalina.core.ApplicationDispatcher
invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: [Ljava.lang.Object;
at org.apache.jsp.jsp.bookList_jsp._jspService(bookList_jsp.java:133)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
393)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
320)
I'm now reading about JSTL to see what I can do in terms of getting
this to work.
Thanks for your help and any other ideas are welcome. Hope that I
can get this
going soon since I need it for a project at work.
Sorry I should have caught this the first time through but I am not as
familiar with hibernate. Now that I'm looking again, I don't see
anyplace
where you are actually constructing any Book objects. In your
BookListAction code you have this:
books = session.createQuery("select id, title, author, available from
Book
t").list();
bookListForm.setBooks(books);
Now if you look at the javadoc api's for the org.hibernate.Query class
you'll find the following explanation for the list() method:
"Return the query results as a List. If the query contains multiple
results
pre row, the results are returned in an instance of Object[]."
So in my best estimate, unless there is some hibernate magic at work
behind
the scenes here I can't see, you never actually create any
com.mycompany.Book objects.
Thanks again for the reply. I'm new at this so it's taking me some
time to get things
going. I appreciate your help.
I think I understand what you're telling me. I'm putting the result
set of the query
(objects) in a Collection, but it's not a collection of type Book.
There's no connection
between the Collection that I defined called books and the actual Book
Class.
So maybe I shouldn't have even defined that Collection in the Book
Class.
I guess what I need to do is iterate through the result set from the
Query, while creating
instances of type Book and setting the attributes of each instance
accordingly with the
corresponding value from the result set (List). As each instance of
the Book object is created,
I need to load those instances in a Collection. Then I would have a
Collection of type Book to
go through and write to the jsp page.
Have a nice day and holiday. Hope that you're doing well. I'll try
to figure out how to do
this and will post again.
Take care,

Yes, I think it looks like you have understood me perfectly. I believe what
you stated is exactly what is causing your problem.


Hi Joe,

Thanks again for the reply. Hope that you're doing well.

I tried several different variation of things to get the code
working. So far
I haven't succeeded.

Below is the change that I made to the BookListAction.java in order
to get the
value of the items returned from the Query. I understand that it's a
list, but the
list (basically rows with data) seems to have an Object in each index.
So it's a
list of Objects where every Object has the 4 values retrieved for a
record in the
database.

To make things simple I added a where clause (for id = 1) so that it
matches only
one row. I'm able to display that the size of books2 is 1, but I can't
get to the
values somehow.

I'll appreciate your help or any ideas regarding this problem. The
new code for
the BookListAction.java is shown below. The other files have no
changes.

Thanks again,

Ruben

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

BookListAction.java

package com.mycompany.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 com.mycompany.Book;
import com.mycompany.struts.form.BookListForm;
import com.mycompany.hibernate.*;
import org.hibernate.*;
import org.hibernate.criterion.Projections;

import java.util.*;

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>();
List <Object> books2 = new ArrayList<Object>();
Object books3 = null;
Book book1 = null;

try {

factory = HibernateUtil.getSessionFactory();

session = (Session) factory.openSession();

// The line below works and I can see the values in
debug mode. I added the
// where clause to limit the resultset, but it works
without it too

books2 = (List <Object>) session.createQuery("select id,
title, author, available from Book where id = 1 ").list();


// The line below prints the total rows returned
from the query. With the
// where clause, it prints the value 1 since only
one row matches that criteria

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


// Below are the different things that I tried in
order to get the value from
// books2. Nothing works.

Object[] firstResult = (Object[]) books2.get(0);

// int id = ((Integer) firstResult[0]).intValue();
String title = ((String) firstResult[1]).toString();

System.out.println(id);
System.out.println(title);


// Criteria crit = session.createCriteria(Book.class);
// System.out.println("Query Size = " + books2.size());

// for (int i = 0; i < books2.size(); i++) {
// Book book = (Book) books2.getInt(0);
// }

// for (int i = 0; i < books2.size(); i++) {
// books3 = books2.get(i);
// System.out.println(books3);
// }


// for (int i = 0; i < books2.size(); i++) {
// System.out.println(books2);
// book1 = null;
// book1 = (Book) books2.get(i);
// System.out.println(book1.getTitle());
// }


bookListForm.setBooks(books2);

} finally {
session.close();
}

return mapping.findForward("showList");

}
}


==========================================================================
 
R

Rudi

news:d3f66ee1-e208-4a48-9243-cd870e9e124d@f24g2000prh.googlegroups.com...
Joe Blow wrote:
Sorry to post the code,
First of all. . .never aplogize for posting the source code when you
are
asking for help with a programming problem.
To the OP: We love that you posted source code. It is a good thing.
This
group loves source code, as a rule.
<logic:iterate name="bookListForm" property="books" id="book"
type="com.mycompany.Book">
The JSP Standard Tag Library (JSTL) has largely supplanted the logic:
taglib
from Struts.
<c:forEach>, etc.
<http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html>
You don't have to get into that right away. Learn logic:iterate and
c:forEach
will make sense, in a different but similar way.
Sun's JSTL tutorial is a good starting place, when you're ready.
<http://java.sun.com/javaee/5/docs/tutorial/doc/bnakc.html>
--
Lew
Hi guys,
Thanks Joe and Lew for your replies. I'm glad I did the right thing
posting the code then.
I implemented the change that Joe suggested, I replaced:
<logic:iterate name="bookListForm" property="books" id="book">
with
<logic:iterate name="bookListForm" property="books" id="book"
type="com.mycompany.Book">
but I get the following error:
Jun 27, 2008 2:01:21 PM org.apache.catalina.core.ApplicationDispatcher
invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: [Ljava.lang.Object;
at org.apache.jsp.jsp.bookList_jsp._jspService(bookList_jsp.java:133)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
393)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
320)
I'm now reading about JSTL to see what I can do in terms of getting
this to work.
Thanks for your help and any other ideas are welcome. Hope that I
can get this
going soon since I need it for a project at work.
Sorry I should have caught this the first time through but I am not as
familiar with hibernate. Now that I'm looking again, I don't see
anyplace
where you are actually constructing any Book objects. In your
BookListAction code you have this:
books = session.createQuery("select id, title, author, available from
Book
t").list();
bookListForm.setBooks(books);
Now if you look at the javadoc api's for the org.hibernate.Query class
you'll find the following explanation for the list() method:
"Return the query results as a List. If the query contains multiple
results
pre row, the results are returned in an instance of Object[]."
So in my best estimate, unless there is some hibernate magic at work
behind
the scenes here I can't see, you never actually create any
com.mycompany.Book objects.
Hi Joe,
Thanks again for the reply. I'm new at this so it's taking me some
time to get things
going. I appreciate your help.
I think I understand what you're telling me. I'm putting the result
set of the query
(objects) in a Collection, but it's not a collection of type Book.
There's no connection
between the Collection that I defined called books and the actual Book
Class.
So maybe I shouldn't have even defined that Collection in the Book
Class.
I guess what I need to do is iterate through the result set from the
Query, while creating
instances of type Book and setting the attributes of each instance
accordingly with the
corresponding value from the result set (List). As each instance of
the Book object is created,
I need to load those instances in a Collection. Then I would have a
Collection of type Book to
go through and write to the jsp page.
Have a nice day and holiday. Hope that you're doing well. I'll try
to figure out how to do
this and will post again.
Take care,
Ruben
Yes, I think it looks like you have understood me perfectly. I believe what
you stated is exactly what is causing your problem.

Hi Joe,

Thanks again for the reply. Hope that you're doing well.

I tried several different variation of things to get the code
working. So far
I haven't succeeded.

Below is the change that I made to the BookListAction.java in order
to get the
value of the items returned from the Query. I understand that it's a
list, but the
list (basically rows with data) seems to have an Object in each index.
So it's a
list of Objects where every Object has the 4 values retrieved for a
record in the
database.

To make things simple I added a where clause (for id = 1) so that it
matches only
one row. I'm able to display that the size of books2 is 1, but I can't
get to the
values somehow.

I'll appreciate your help or any ideas regarding this problem. The
new code for
the BookListAction.java is shown below. The other files have no
changes.

Thanks again,

Ruben

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

BookListAction.java

package com.mycompany.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 com.mycompany.Book;
import com.mycompany.struts.form.BookListForm;
import com.mycompany.hibernate.*;
import org.hibernate.*;
import org.hibernate.criterion.Projections;

import java.util.*;

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>();
List <Object> books2 = new ArrayList<Object>();
Object books3 = null;
Book book1 = null;

try {

factory = HibernateUtil.getSessionFactory();

session = (Session) factory.openSession();

// The line below works and I can see the values in
debug mode. I added the
// where clause to limit the resultset, but it works
without it too

books2 = (List <Object>) session.createQuery("select id,
title, author, available from Book where id = 1 ").list();

// The line below prints the total rows returned
from the query. With the
// where clause, it prints the value 1 since only
one row matches that criteria

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

// Below are the different things that I tried in
order to get the value from
// books2. Nothing works.

Object[] firstResult = (Object[]) books2.get(0);

// int id = ((Integer) firstResult[0]).intValue();
String title = ((String) firstResult[1]).toString();

System.out.println(id);
System.out.println(title);

// Criteria crit = session.createCriteria(Book.class);
// System.out.println("Query Size = " + books2.size());

// for (int i = 0; i < books2.size(); i++) {
// Book book = (Book) books2.getInt(0);
// }

// for (int i = 0; i < books2.size(); i++) {
// books3 = books2.get(i);
// System.out.println(books3);
// }

// for (int i = 0; i < books2.size(); i++) {
// System.out.println(books2);
// book1 = null;
// book1 = (Book) books2.get(i);
// System.out.println(book1.getTitle());
// }

bookListForm.setBooks(books2);

} finally {
session.close();
}

return mapping.findForward("showList");

}

}

==========================================================================

Hi Joe,

Thanks for the help.

I got things working with Hibernate. The code is in this link:

http://groups.google.com.pk/group/comp.lang.java.programmer/browse_thread/thread/2c31665169c1e5ad#

However, I get some problems when trying to display the result in a
jsp. I can send it
to the console fine. I think probably the problem has to do with
Struts and the Form and Action working together. Probably it can't
resolve the collection that is part of the Form.

Thanks,

Rudi
 
R

Rudi

Joe Blow wrote:
Sorry to post the code,
First of all. . .never aplogize for posting the source code when you
are
asking for help with a programming problem.
To the OP: We love that you posted source code. It is a good thing.
This
group loves source code, as a rule.
<logic:iterate name="bookListForm" property="books" id="book"
type="com.mycompany.Book">
The JSP Standard Tag Library (JSTL) has largely supplanted the logic:
taglib
from Struts.
<c:forEach>, etc.
<http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index.html>
You don't have to get into that right away. Learn logic:iterate and
c:forEach
will make sense, in a different but similar way.
Sun's JSTL tutorial is a good starting place, when you're ready..
<http://java.sun.com/javaee/5/docs/tutorial/doc/bnakc.html>
--
Lew
Hi guys,
Thanks Joe and Lew for your replies. I'm glad I did the right thing
posting the code then.
I implemented the change that Joe suggested, I replaced:
<logic:iterate name="bookListForm" property="books" id="book">
with
<logic:iterate name="bookListForm" property="books" id="book"
type="com.mycompany.Book">
but I get the following error:
Jun 27, 2008 2:01:21 PM org.apache.catalina.core.ApplicationDispatcher
invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: [Ljava.lang.Object;
at org.apache.jsp.jsp.bookList_jsp._jspService(bookList_jsp.java:133)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
393)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:
320)
I'm now reading about JSTL to see what I can do in terms of getting
this to work.
Thanks for your help and any other ideas are welcome. Hope that I
can get this
going soon since I need it for a project at work.
Sorry I should have caught this the first time through but I am not as
familiar with hibernate. Now that I'm looking again, I don't see
anyplace
where you are actually constructing any Book objects. In your
BookListAction code you have this:
books = session.createQuery("select id, title, author, available from
Book
t").list();
bookListForm.setBooks(books);
Now if you look at the javadoc api's for the org.hibernate.Query class
you'll find the following explanation for the list() method:
"Return the query results as a List. If the query contains multiple
results
pre row, the results are returned in an instance of Object[]."
So in my best estimate, unless there is some hibernate magic at work
behind
the scenes here I can't see, you never actually create any
com.mycompany.Book objects.
Hi Joe,
Thanks again for the reply. I'm new at this so it's taking me some
time to get things
going. I appreciate your help.
I think I understand what you're telling me. I'm putting the result
set of the query
(objects) in a Collection, but it's not a collection of type Book.
There's no connection
between the Collection that I defined called books and the actual Book
Class.
So maybe I shouldn't have even defined that Collection in the Book
Class.
I guess what I need to do is iterate through the result set from the
Query, while creating
instances of type Book and setting the attributes of each instance
accordingly with the
corresponding value from the result set (List). As each instance of
the Book object is created,
I need to load those instances in a Collection. Then I would have a
Collection of type Book to
go through and write to the jsp page.
Have a nice day and holiday. Hope that you're doing well. I'll try
to figure out how to do
this and will post again.
Take care,
Ruben
Yes, I think it looks like you have understood me perfectly. I believe what
you stated is exactly what is causing your problem.
Thanks again for the reply. Hope that you're doing well.
I tried several different variation of things to get the code
working. So far
I haven't succeeded.
Below is the change that I made to the BookListAction.java in order
to get the
value of the items returned from the Query. I understand that it's a
list, but the
list (basically rows with data) seems to have an Object in each index.
So it's a
list of Objects where every Object has the 4 values retrieved for a
record in the
database.
To make things simple I added a where clause (for id = 1) so that it
matches only
one row. I'm able to display that the size of books2 is 1, but I can't
get to the
values somehow.
I'll appreciate your help or any ideas regarding this problem. The
new code for
the BookListAction.java is shown below. The other files have no
changes.
Thanks again,



package com.mycompany.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 com.mycompany.Book;
import com.mycompany.struts.form.BookListForm;
import com.mycompany.hibernate.*;
import org.hibernate.*;
import org.hibernate.criterion.Projections;
import java.util.*;
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>();
List <Object> books2 = new ArrayList<Object>();
Object books3 = null;
Book book1 = null;
factory = HibernateUtil.getSessionFactory();
session = (Session) factory.openSession();
// The line below works and I can see the values in
debug mode. I added the
// where clause to limit the resultset, but it works
without it too
books2 = (List <Object>) session.createQuery("select id,
title, author, available from Book where id = 1 ").list();
// The line below prints the total rows returned
from the query. With the
// where clause, it prints the value 1 since only
one row matches that criteria
System.out.println("Query Size = " + books2.size());
// Below are the different things that I tried in
order to get the value from
// books2. Nothing works.
Object[] firstResult = (Object[]) books2.get(0);
// int id = ((Integer) firstResult[0]).intValue();
String title = ((String) firstResult[1]).toString();
System.out.println(id);
System.out.println(title);

// Criteria crit = session.createCriteria(Book.class);
// System.out.println("Query Size = " + books2.size());
// for (int i = 0; i < books2.size(); i++) {
// Book book = (Book) books2.getInt(0);
// }
// for (int i = 0; i < books2.size(); i++) {
// books3 = books2.get(i);
// System.out.println(books3);
// }
// for (int i = 0; i < books2.size(); i++) {
// System.out.println(books2);
// book1 = null;
// book1 = (Book) books2.get(i);
// System.out.println(book1.getTitle());
// }

} finally {
session.close();
}
return mapping.findForward("showList");


==========================================================================

Hi Joe,

Thanks for the help.

I got things working with Hibernate. The code is in this link:

http://groups.google.com.pk/group/comp.lang.java.programmer/browse_th...

However, I get some problems when trying to display the result in a
jsp. I can...

read more »


Hi Everyone,

I actually got this to work finally. The last problem was solved
after
I replaced Strut nested tags with plain Struts iterate tags.

The code is in this link:

http://groups.google.com.pk/group/c...ad/thread/2c31665169c1e5ad/56fa92f8d4e1f9b9?#

Thanks again to Joe and everyone else for all the help. :)
 
R

Rudi

Joe Blow wrote:
Sorry to post the code,
First of all. . .never aplogize for posting the source code when you
are
asking for help with a programming problem.
To the OP: We love that you posted source code. It is a good thing.
This
group loves source code, as a rule.
<logic:iterate name="bookListForm" property="books" id="book"
type="com.mycompany.Book">
The JSP Standard Tag Library (JSTL) has largely supplanted the logic:
taglib
from Struts.
<c:forEach>, etc.
<http://java.sun.com/products/jsp/jstl/1.1/docs/tlddocs/index..html>
You don't have to get into that right away. Learn logic:iterate and
c:forEach
will make sense, in a different but similar way.
Sun's JSTL tutorial is a good starting place, when you're ready.
<http://java.sun.com/javaee/5/docs/tutorial/doc/bnakc.html>
--
Lew
Hi guys,
Thanks Joe and Lew for your replies. I'm glad I did the right thing
posting the code then.
I implemented the change that Joe suggested, I replaced:
<logic:iterate name="bookListForm" property="books" id="book">
with
<logic:iterate name="bookListForm" property="books" id="book"
type="com.mycompany.Book">
but I get the following error:
Jun 27, 2008 2:01:21 PM org.apache.catalina.core.ApplicationDispatcher
invoke
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.ClassCastException: [Ljava.lang.Object;
at org.apache.jsp.jsp.bookList_jsp._jspService(bookList_jsp.java:133)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:
393)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet..java:
320)
I'm now reading about JSTL to see what I can do in terms of getting
this to work.
Thanks for your help and any other ideas are welcome. Hope that I
can get this
going soon since I need it for a project at work.
Sorry I should have caught this the first time through but I am not as
familiar with hibernate. Now that I'm looking again, I don't see
anyplace
where you are actually constructing any Book objects. In your
BookListAction code you have this:
books = session.createQuery("select id, title, author, available from
Book
t").list();
bookListForm.setBooks(books);
Now if you look at the javadoc api's for the org.hibernate.Query class
you'll find the following explanation for the list() method:
"Return the query results as a List. If the query contains multiple
results
pre row, the results are returned in an instance of Object[]."
So in my best estimate, unless there is some hibernate magic at work
behind
the scenes here I can't see, you never actually create any
com.mycompany.Book objects.
Hi Joe,
Thanks again for the reply. I'm new at this so it's taking me some
time to get things
going. I appreciate your help.
I think I understand what you're telling me. I'm putting the result
set of the query
(objects) in a Collection, but it's not a collection of type Book..
There's no connection
between the Collection that I defined called books and the actual Book
Class.
So maybe I shouldn't have even defined that Collection in the Book
Class.
I guess what I need to do is iterate through the result set from the
Query, while creating
instances of type Book and setting the attributes of each instance
accordingly with the
corresponding value from the result set (List). As each instance of
the Book object is created,
I need to load those instances in a Collection. Then I would have a
Collection of type Book to
go through and write to the jsp page.
Have a nice day and holiday. Hope that you're doing well. I'll try
to figure out how to do
this and will post again.
Take care,
Ruben
Yes, I think it looks like you have understood me perfectly. I believe what
you stated is exactly what is causing your problem.
Hi Joe,
Thanks again for the reply. Hope that you're doing well.
I tried several different variation of things to get the code
working. So far
I haven't succeeded.
Below is the change that I made to the BookListAction.java in order
to get the
value of the items returned from the Query. I understand that it's a
list, but the
list (basically rows with data) seems to have an Object in each index..
So it's a
list of Objects where every Object has the 4 values retrieved for a
record in the
database.
To make things simple I added a where clause (for id = 1) so that it
matches only
one row. I'm able to display that the size of books2 is 1, but I can't
get to the
values somehow.
I'll appreciate your help or any ideas regarding this problem. The
new code for
the BookListAction.java is shown below. The other files have no
changes.
Thanks again,
Ruben
==========================================================================
BookListAction.java
BookListAction.java
package com.mycompany.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 com.mycompany.Book;
import com.mycompany.struts.form.BookListForm;
import com.mycompany.hibernate.*;
import org.hibernate.*;
import org.hibernate.criterion.Projections;
import java.util.*;
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>();
List <Object> books2 = new ArrayList<Object>();
Object books3 = null;
Book book1 = null;
try {
factory = HibernateUtil.getSessionFactory();
session = (Session) factory.openSession();
// The line below works and I can see the values in
debug mode. I added the
// where clause to limit the resultset, but it works
without it too
books2 = (List <Object>) session.createQuery("select id,
title, author, available from Book where id = 1 ").list();
// The line below prints the total rows returned
from the query. With the
// where clause, it prints the value 1 since only
one row matches that criteria
System.out.println("Query Size = " + books2.size());
// Below are the different things that I tried in
order to get the value from
// books2. Nothing works.
Object[] firstResult = (Object[]) books2.get(0);
// int id = ((Integer) firstResult[0]).intValue();
String title = ((String) firstResult[1]).toString();
System.out.println(id);
System.out.println(title);
// Criteria crit = session.createCriteria(Book.class);
// System.out.println("Query Size = " + books2.size());
// for (int i = 0; i < books2.size(); i++) {
// Book book = (Book) books2.getInt(0);
// }
// for (int i = 0; i < books2.size(); i++) {
// books3 = books2.get(i);
// System.out.println(books3);
// }
// for (int i = 0; i < books2.size(); i++) {
// System.out.println(books2);
// book1 = null;
// book1 = (Book) books2.get(i);
// System.out.println(book1.getTitle());
// }
bookListForm.setBooks(books2);
} finally {
session.close();
}
return mapping.findForward("showList");
}
}
==========================================================================
Thanks for the help.
I got things working with Hibernate. The code is in this link:

However, I get some problems when trying to display the result in a
jsp. I can...
read more »

Hi Everyone,

I actually got this to work finally. The last problem was solved
after
I replaced Strut nested tags with plain Struts iterate tags.

The code is in this link:

http://groups.google.com.pk/group/comp.lang.java.programmer/browse_th...

Thanks again to Joe and everyone else for all the help. :)


Moderator: Please close this thread. Thanks.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top