aspose file tools*
The moose likes Struts and the fly likes Cannot find bean coursesList in any scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Cannot find bean coursesList in any scope" Watch "Cannot find bean coursesList in any scope" New topic
Author

Cannot find bean coursesList in any scope

lee kris
Ranch Hand

Joined: Mar 30, 2005
Posts: 35
hi everyone,
I have a problem that has consumed some of my time for a while now and still not solved. I am getting an error " Cannot find bean coursesList in any scope" to be specific. I have checked some of the suggestions on the web and did not work for me. So can anyone please look at this and respond. Thank you.

part of my JSP:

<table width="700"
border="0" cellspacing="0" cellpadding="0">
<tr align="left">
<th><bean:message key="app.course_code" /></th>
<th><bean:message key="app.section_code" /></th>
<th><bean:message key="app.year" /></th>
<th><bean:message key="app.quarter" /></th>
<th><bean:message key="app.instructor_id" /></th>

</tr>

<logic:iterate id="courses" name="coursesList">

<tr align="left">
<td>
<bean:write name="courses" property="courseCode" />
</td>
<td>
<bean:write name="courses" property="sectionCode" />
</td>
<td>
<bean:write name="courses" property="year" />
</td>
<td>
<bean:write name="courses" property="quarter" />
</td>
<td>
<bean:write name="courses" property="instructorId" />
</td>

</tr>
</logic:iterate>


my java file:

public class CoursesListAction extends Action {

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

String target = new String("success");


ArrayList coursesList =null;

try{


coursesList = StudentDAO.getCoursesList(getDataSource(request));
request.setAttribute("coursesList",coursesList);
}

catch (Exception e)
{
System.err.println ("CoursesListAction: Error creating a coursesList or reading from the form");
e.printStackTrace();

}

return (mapping.findForward(target));
}


in my struts-config.xml I have:
...
<action path="/CoursesList"
type="com.rit.CoursesListAction"
scope="session" >

<forward name="success" path="/CoursesQuarter.jsp" redirect="false"/>
</action>

...

now when i execute this I get the error below :
javax.servlet.ServletException: Cannot find bean coursesList in any scope
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.CoursesQuarter_jsp._jspService(CoursesQuarter_jsp.java:206)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

......


can anyone please hint me where i am going wrong?
Thank you in advance.
lee
anupa oru
Ranch Hand

Joined: Jan 15, 2005
Posts: 118
Hi,
as of my knowledge I guess
<logic:iterate id="courses" name="coursesList">
you have to use name="CoursesList" since action mapping has only entry for CoursesList.
Let me know how it works.
Thanks
Anupa
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Assuming the jsp snippet is from CoursesQuarter.jsp, you are hitting the CoursesListAction to get to the jsp, and the Action is not throwing an exception, everything should be working fine.


A good workman is known by his tools.
lee kris
Ranch Hand

Joined: Mar 30, 2005
Posts: 35
Thanks guys.
I don't see why id does not work but it still does not. Anupa, I tried what you suggested and still does not work. The name of the bean should actually match the name provided at the Action (java file ) which i set as
request.setAttribute("coursesList",coursesList);
but it is not working for me yet.
Thanks
lee kris
Ranch Hand

Joined: Mar 30, 2005
Posts: 35
Mark, yes the snippet code you were referring to was from CoursesQuarter.jsp.
lee
lee kris
Ranch Hand

Joined: Mar 30, 2005
Posts: 35
hi everyone,
The problem in this thread is nice but so far I have not gotten any hint as to how to solve it. I would really appreciate if some of you struts guru can check it out and give me some suggestions.
Thank you all.
lee
Sree Jag
Ranch Hand

Joined: Oct 14, 2003
Posts: 77
Hi Lee,
After you have created the courseList collection, you have not set it in the ActionForm. Instead you put it in the request scope. the <logic:iterate> tag will check for the coursesList attribute in the ActionForm and since it is not there it is throwing an Exception.

Try setting the value in ActionForm.

Hope this helps,
Seshu


Sree Jag<br />SCJP 1.4
lee kris
Ranch Hand

Joined: Mar 30, 2005
Posts: 35
hi Seshu,

my intention was to just iterate over the collection (the ArrayList)and print everything out. I set the "coursesList" in the courseListAction class by getting all my values from the database as follows:

public class CoursesListAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
String target = new String("success");
ArrayList coursesList =null;

try{
coursesList = StudentDAO.getCoursesList(getDataSource(request));
request.setAttribute("coursesList",coursesList);
}
catch (Exception e)
{
System.err.println ("CoursesListAction: Error ...");
e.printStackTrace();
}
return (mapping.findForward(target));
}}

So I don't think I need a ActionForm to set the coursesList to be used by the logic:iterate tag. To be sure though, I just created an ActionForm (displayed below ) and it did not work as I expected it. as a side note I modified my struts-config.xml to include this form in the <form-bean> tag
[ <form-bean name="coursesListForm" type="cqas.rit.CoursesListForm"/ ]

public class CoursesListForm extends ActionForm
{
private String courseCode;
private String sectionCode;
private String year;
private String quarter;
private String instructorId;
public void setCourseCode (String courseCode)
{
this.courseCode = courseCode;
}
public void setSectionCode (String sectionCode)
{
this.sectionCode = sectionCode;
}
public void setYear (String year)
{
this.year = year;
}
public void setQuarter (String quarter)
{
this.quarter = quarter;
}
public void setInstructorId (String instructorId)
{
this.instructorId = instructorId;
}
public void reset (ActionMapping mapping, HttpServletRequest request)
{
courseCode = "";
sectionCode = "";
year = "";
quarter = "";
instructorId = "";
}
public ActionErrors validate (ActionMapping mapping, HttpServletRequest request)
{
ActionErrors errors = new ActionErrors ();
if ((courseCode == null))
{
//change the following things later
//just test to see if this solves the problem
errors.add ("Course code", new ActionError ("err..."));
}
return errors;
}

I really appreciate you help and am waiting for your response back on this.
lee
Sree Jag
Ranch Hand

Joined: Oct 14, 2003
Posts: 77
Hi Lee,


You created the ActionForm but there is not coursesList field in there. You have to do something like this in the action:

coursesListForm.setCoursesList(coursesList);

and in the ActionForm add a field like:
private ArrayList coursesList;
public void setCoursesList(ArrayList list){coursesList = list;}
public ArrayList getCoursesList( return coursesList);

Hope this works,
Seshu
lee kris
Ranch Hand

Joined: Mar 30, 2005
Posts: 35
hi Sheshu,
I tried your suggestion and made the following insertions into the codes.
In the Action:

import java.util.ArrayList;

public class CoursesListAction extends Action {

public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
String target = new String("success");
ArrayList coursesList =null;
CoursesListForm coursesListForm = (CoursesListForm) form;

try{
coursesList = StudentDAO.getCoursesList(getDataSource(request));
// the following line was added as suggested by javaranch (due to Sheshu)
coursesListForm.setCoursesList(coursesList);
request.setAttribute("coursesList",coursesList);
}
catch (Exception e)
{
System.err.println ("CoursesListAction: Error creating a coursesList or reading from the form");
e.printStackTrace();
}
return (mapping.findForward(target));
}

and in the CoursesListForm :

public class CoursesListForm extends ActionForm
{
private String courseCode;
private String sectionCode;
private String year;
private String quarter;
private String instructorId;
// the following lines were added as suggested by Sheshuu
private ArrayList coursesList;

public void setCoursesList(ArrayList list){coursesList = list;}
public ArrayList getCoursesList(){
return coursesList;
}

...
}

When I execute this, I gave me the usual error message :
"Cannot find bean coursesList in any scope"
I am not sure if in the CoursesListAction, the way I am handling the form is correct. also I removed the scope = "session" in my struts-config.xml file.

I appreciate your responses as usual.
lee
Sree Jag
Ranch Hand

Joined: Oct 14, 2003
Posts: 77
hmm...interesting...the only other thing i can think of now is the configuration in Struts-Config.xml

did u change the following:
<action path="/CoursesList"
type="com.rit.CoursesListAction"
scope="session" >

to
<action path="/CoursesList"
type="com.rit.CoursesListAction"
scope="session"
name="courseListForm" >

?
try doing that

Seshu
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

lee,

It really isn't necessary to use an ActionForm for your list. As I said earlier, there is nothing in the first code you posted to indicate any type of error.

It was not necessary for your original struts-config ActionMapping to include the scope attribute, because this would refer to which scope to place the ActionForm in - and you specified no ActionForm.

Anyway, I think I figured out your problem. According to the Java API, the setAttribute method includes this statement in the JavaDoc:
If the value passed in is null, the effect is the same as calling removeAttribute(java.lang.String).
So it is possible that your DAO object is returning null and that would explain why the logic:iterate is not doing what you'd expect.

There are a couple options for solving this. One is to surround the logic:iterate with logic resent tags to search for the list. A second option would be to change the line:
coursesList = StudentDAO.getCoursesList(getDataSource(request));
to
coursesList = new ArrayList(StudentDAO.getCoursesList(getDataSource(request)));
or
coursesList = new ArrayList();
coursesList.addAll(StudentDAO.getCoursesList(getDataSource(request)));
to be sure that you are not calling setAttribute with a null Object parameter.
Sree Jag
Ranch Hand

Joined: Oct 14, 2003
Posts: 77
Hi Marc,
that was the first thing that crossed my mind when Lee showed the exception (the DAO may be null).
But when i was trying to use <bean efine> with a null attribute it gave an different exception saying "Cannot define a null value".

Does logic:iterate have a different logic regarding this?

Seshu
lee kris
Ranch Hand

Joined: Mar 30, 2005
Posts: 35
Thank you Marc and Sheshu for your responses.
I could not follow up since my internet at home was down for the whole weekend.
I tried most of your suggestions which were really great but there are something that need further fixing before it works correctly.
Today I struggled to find out more about why this is not working and when i look at my Tomcat error list i see the following:
org.apache.struts.util.PropertyMessageResources <init> initializing,config = ,org.apache.strtus.taglib.bean.LocalStrings',returnNull = true.
So i believe the ArrayList (coursesList) is not getting initialized perhaps. to that effect i tried setting the array to some dummy values and it still did not work. Does the above error hint to other problems apart from the the ArrayList is null(which is what i think.)
Once again thank you in advance for any response that you might provide.
lee
Sb Jean-Baptiste
Greenhorn

Joined: Apr 04, 2005
Posts: 1
Have you tried defining the bean at the beginning of the page. Something like
<bean efine id="courseList" name="courseList" type="java.util.Collection" scope="request"/>

or whatever the type is. I am doing the exact same thing you are and it works for me. It is not a struts related problem as far as I can tell. If you set the attribute within the Action servlet like request.setAttribute ("courseList", courseList); you should be able to access it even within a simple servlet.

--ignore if you have already tried this.
lee kris
Ranch Hand

Joined: Mar 30, 2005
Posts: 35
hi Sb Jean-Baptiste,
Thanks for your response. I tried that too and did not work. I do have the following lines in my struts-config.xml:
<action path="/CoursesList"
type="com.rit.CoursesListAction">

<forward name="success" path="/CoursesQuarter.jsp" redirect="false"/>
</action>

and I also have the following in my .jsp file:
<logic:iterate id="courses" name="coursesList">
<tr align="left">
<td>
<bean:write name="courses" property="courseCode" />
</td>
<td>
<bean:write name="courses" property="sectionCode" />
...
</logic:iterate>

I tried you suggestion by placing the <bean define ...> tag and did not work. I am not sure if i did it right. I placed the it before the <logic:iterage > tag. Is that right. can you correct me on that?
lee
Jonathan Lehr
author
Greenhorn

Joined: Oct 07, 2003
Posts: 20
Hi Lee,

Please see my answer to the following post:

http://www.coderanch.com/t/50830/Struts/Cannot-find-bean-lookupForm-any

I'm pretty sure you've run into precisely the same problem.

Jonathan
Rommel Baldivas
Greenhorn

Joined: Jan 26, 2005
Posts: 1
Hi!

Did you tried adding the following codes in your jsp page?

<%
ArrayList coursesList = (ArrayList) request.getAttribute("coursesList");
%>

I think you still need to retrieve the data stored in the "coursesList"
by calling them on your page and showing them via "bean:write". I still don't with Arrays but it works well on me on Strings.
Paolo Lancelli
Greenhorn

Joined: Jul 27, 2006
Posts: 1
For future references, I had the same problem, after few hours of headache I noticed the missed directive:
<%@ taglib uri="/tags/struts-html" prefix="html" %>
or
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> or other forms, depending on what you declared in your struts-config.xml etc...
[ July 27, 2006: Message edited by: Paolo Lancelli ]

<a href="http://www.paololancelli.com" target="_blank" rel="nofollow">paololancelli.com</a>
Vijay Tripathi
Greenhorn

Joined: Nov 10, 2006
Posts: 2
Hi everybody,
I am having the simple struts programme which do not have even those suggestions where you people are suggesting problems.
Just have a look at it.And according to your guidance if i am forwarding it using jsp syntax like <code><%response.sendRedirect("/login.jsp")%></code>it is giving the same problem,else if i am doing like inside
Vijay Tripathi
Greenhorn

Joined: Nov 10, 2006
Posts: 2
and here is the code



[ November 11, 2006: Message edited by: Vijay Tripathi ]
[ November 11, 2006: Message edited by: Vijay Tripathi ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot find bean coursesList in any scope