Can Not find bean in scope error when trying to display in jsp

R

Rudi

Hi Everyone,

I'm new with Struts and Hibernate. I'm running the code in Windows
using MyEclipse,
running on Tomcat bundled with MyEclipse 6.0. Also, the table Book is
in an Oracle
8.i database (connects fine), Struts version 1.1, and Hibernate
version 3.1.

I got something working that gets data from a Book Oracle table in
Hibernate and I can see the data con the console since I display it
with System.out in the Struts Action. I printed the record counts and
title of the Book records:

Query Size = 7
The title is Struts Book
The title is Java Book
The title is Java2 Book
The title is EJB Book
The title is JBoss for Beginners
The title is Using MyEclipse for cooking
The title is EJB for spending your weekends

The application consist of 2 simple pages, one jsp with a link, the
other jsp that is supposed to display the bookList. The bookList is
where I'm getting an error since it doesn't display the values. I get
an error:

SEVERE: Servlet.service() for servlet action threw exception
javax.servlet.jsp.JspException: Cannot find bean in any scope

Hibernate seems to be working fine since I get the data.

The problem seems to be with the bookList.jsp page and Struts.

I tried removing the nested writes and just having a tr and td
displaying text that says there are booklist values. That text was
successfully displayed. So my problem is the logic that iterates
through the values to try to write them to the jsp.

Can anyone please help?

The code is show below. Thanks in advance. :)

Rudi


======================================================
BookListAction.java (here's where I run the query and populate the
books collection)

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

bookListForm.reset(mapping, request);
bookListForm.setBooks(bks);

} finally {
session.close();
}

return mapping.findForward("showList");

}
}

======================================================
bookList.java (here's where I have the problem)

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

<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>&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">
<tr>
<td colspan="5">Not empty</td>
</tr>
<nested:iterate property="books">
<tr>
<%-- print out the book information --%>
<td><nested:write name="Book" property="author" /></td>
</td>

</tr>
</nested:iterate>
</logic:notEmpty>

<%-- end interate --%>

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

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

package com.mycompany.sirs;

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

}

======================================================
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;
import com.mycompany.Book;

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

}

======================================================
Book.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping
DTD//EN"
"http://hibernate.sf.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.mycompany.Book" table="ADMIN.Book">

<id name="id" column="id">
<generator class="native"/>
</id>

<property name="title" column="title"/>
<property name="author" column="author"/>
<property name="available" column="available"/>
</class>
</hibernate-mapping>

======================================================
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-
configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">admin</property>
<property name="connection.url">
jdbc:eek:racle:thin:mad:test:1521:testdb
</property>
<property name="dialect">
org.hibernate.dialect.OracleDialect
</property>
<property name="myeclipse.connection.profile">Oracle</property>
<property name="connection.password">adminpass</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<mapping resource="Book.hbm.xml" />

</session-factory>

</hibernate-configuration>
 
R

Rudi

Hi Everyone,

I'm new with Struts and Hibernate. I'm running the code in Windows
using MyEclipse,
running on Tomcat bundled with MyEclipse 6.0. Also, the table Book is
in an Oracle
8.i database (connects fine), Struts version 1.1, and Hibernate
version 3.1.

I got something working that gets data from a Book Oracle table in
Hibernate and I can see the data con the console since I display it
with System.out in the Struts Action. I printed the record counts and
title of the Book records:

Query Size = 7
The title is Struts Book
The title is Java Book
The title is Java2 Book
The title is EJB Book
The title is JBoss for Beginners
The title is Using MyEclipse for cooking
The title is EJB for spending your weekends

The application consist of 2 simple pages, one jsp with a link, the
other jsp that is supposed to display the bookList. The bookList is
where I'm getting an error since it doesn't display the values. I get
an error:

SEVERE: Servlet.service() for servlet action threw exception
javax.servlet.jsp.JspException: Cannot find bean in any scope

Hibernate seems to be working fine since I get the data.

The problem seems to be with the bookList.jsp page and Struts.

I tried removing the nested writes and just having a tr and td
displaying text that says there are booklist values. That text was
successfully displayed. So my problem is the logic that iterates
through the values to try to write them to the jsp.

Can anyone please help?

The code is show below. Thanks in advance. :)

Rudi

======================================================
BookListAction.java (here's where I run the query and populate the
books collection)

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

bookListForm.reset(mapping, request);
bookListForm.setBooks(bks);

} finally {
session.close();
}

return mapping.findForward("showList");

}

}

======================================================
bookList.java (here's where I have the problem)

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

<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">
<tr>
<td colspan="5">Not empty</td>
</tr>
<nested:iterate property="books">
<tr>
<%-- print out the book information --%>
<td><nested:write name="Book" property="author" /></td>
</td>

</tr>
</nested:iterate>
</logic:notEmpty>

<%-- end interate --%>

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

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

package com.mycompany.sirs;

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

}

======================================================
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;
import com.mycompany.Book;

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

}

======================================================
Book.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping
DTD//EN"
"http://hibernate.sf.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.mycompany.Book" table="ADMIN.Book">

<id name="id" column="id">
<generator class="native"/>
</id>

<property name="title" column="title"/>
<property name="author" column="author"/>
<property name="available" column="available"/>
</class>
</hibernate-mapping>

====================================================== hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-
configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">admin</property>
<property name="connection.url">
jdbc:eek:racle:thin:mad:test:1521:testdb
</property>
<property name="dialect">
org.hibernate.dialect.OracleDialect
</property>
<property name="myeclipse.connection.profile">Oracle</property>
<property name="connection.password">adminpass</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<mapping resource="Book.hbm.xml" />

</session-factory>

</hibernate-configuration>

======================================================
struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD
Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/
struts-config_1_1.dtd">

<struts-config>
<data-sources />
<form-beans >
<form-bean name="bookListForm"
type="com.orcww.sirs.struts.form.BookListForm" />

</form-beans>

<global-exceptions />
<global-forwards >
<forward
name="welcome"
path="/default.do"
redirect="true" />

</global-forwards>

<action-mappings >
<action forward="/jsp/index.jsp" path="/default" unknown="true" />
<action
attribute="bookListForm"
input="/jsp/bookList.jsp"
name="bookListForm"
path="/bookList"
scope="request"
type="com.orcww.sirs.struts.action.BookListAction"
validate="false">
<forward name="showList" path="/jsp/bookList.jsp" />
</action>

</action-mappings>

</struts-config>
 
R

Rudi

======================================================
struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD
Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/
struts-config_1_1.dtd">

<struts-config>
<data-sources />
<form-beans >
<form-bean name="bookListForm"
type="com.mycompany.struts.form.BookListForm" />

</form-beans>

<global-exceptions />
<global-forwards >
<forward
name="welcome"
path="/default.do"
redirect="true" />

</global-forwards>

<action-mappings >
<action forward="/jsp/index.jsp" path="/default" unknown="true" />
<action
attribute="bookListForm"
input="/jsp/bookList.jsp"
name="bookListForm"
path="/bookList"
scope="request"
type="com.orcww.sirs.struts.action.BookListAction"
validate="false">
<forward name="showList" path="/jsp/bookList.jsp" />
</action>

</action-mappings>

</struts-config>

Hope that someone can see a problem that can help with the issue.
 
R

Rudi

======================================================
struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD
Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/
struts-config_1_1.dtd">

<struts-config>
<data-sources />
<form-beans >
<form-bean name="bookListForm"
type="com.orcww.sirs.struts.form.BookListForm" />

</form-beans>

<global-exceptions />
<global-forwards >
<forward
name="welcome"
path="/default.do"
redirect="true" />

</global-forwards>

<action-mappings >
<action forward="/jsp/index.jsp" path="/default" unknown="true" />
<action
attribute="bookListForm"
input="/jsp/bookList.jsp"
name="bookListForm"
path="/bookList"
scope="request"
type="com.orcww.sirs.struts.action.BookListAction"
validate="false">
<forward name="showList" path="/jsp/bookList.jsp" />
</action>

</action-mappings>

</struts-config>


Hi Everyone,

I figured out my problem. I replaced the "nested" Strut tags in the
bookList.jsp
with plain "iterate" Strut tags and it worked.

The code inside the non-empty tags should read:

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

Thanks anyway.


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