problem with logic:iterate in struts

A

Arun Nair

Hi,
I have been trying to solve the following problem for over 4 days.
I can't figure out what mistake I am making. I am not able to use the
<logic:iterate> tag over a collection.

The error I get is
type Exception report

message

description The server encountered an internal error () that prevented
it from fulfilling this request.

exception

javax.servlet.ServletException: Cannot find bean years in any scope
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:867)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:800)
org.apache.jsp.pages.JobSeekerEduRegistration_jsp._jspService(JobSeekerEduRegistration_jsp.java:245)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:305)


root cause

javax.servlet.jsp.JspException: Cannot find bean years in any scope
org.apache.struts.util.RequestUtils.lookup(RequestUtils.java:938)
org.apache.struts.taglib.logic.IterateTag.doStartTag(IterateTag.java:277)
org.apache.jsp.pages.JobSeekerEduRegistration_jsp._jspService(JobSeekerEduRegistration_jsp.java:179)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:305)




I have the following class:

/*
* RegisterJobSeekerEduForm.java
*
* Created on January 2, 2000, 5:15 AM
*/
package app;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import java.util.List;
import java.util.ArrayList;


/**
*
* @author anair
*/
public class RegisterJobSeekerEduForm extends ActionForm {
private List exams = new ArrayList(7);
private List subjects = new ArrayList(7);
private List percents = new ArrayList(7);
private List grades = new ArrayList(7);
private List institutions = new ArrayList(7);
private List years = new ArrayList(7);


/** Creates a new instance of RegisterJobSeekerEduForm */
public RegisterJobSeekerEduForm() {

}
 
M

Murray

Arun Nair said:
<td colspan="0" align="center">
<logic:iterate id="year" name="years">
<bean:write name="year" property="name"/>
</logic:iterate>
</td>

You're confusing the name and property attributes. Name should be the bean
that contains the collection i.e. "jeb" and property is the property of that
bean which respresents the collection i.e. "years"


<logic:iterate id="year" name="jeb" property="years">
<bean:write name="year" property="name"/>
</logic:iterate>

However I think you're going to have other problems going by the source code
you gave

public Object getYears(int index) {
return years.get(index);
}

This isn't a bean method: getter methods don't have arguments. getYears
shouild probably just return the list, not a particular entry in the list
(I'm guessing here, since I don't know your intentions)
 
D

David Hilsee

Arun Nair said:
Hi,
I have been trying to solve the following problem for over 4 days.
I can't figure out what mistake I am making. I am not able to use the
<logic:iterate> tag over a collection.

The error I get is
type Exception report

message

description The server encountered an internal error () that prevented
it from fulfilling this request.

exception

javax.servlet.ServletException: Cannot find bean years in any scope
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextI
mpl.java:867)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImp
l.java:800)
org.apache.jsp.pages.JobSeekerEduRegistration_jsp._jspService(JobSeekerEduRe
gistration_jsp.java:245)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:3
11)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter
..java:305)


root cause

javax.servlet.jsp.JspException: Cannot find bean years in any scope
org.apache.struts.util.RequestUtils.lookup(RequestUtils.java:938)
org.apache.struts.taglib.logic.IterateTag.doStartTag(IterateTag.java:277)
org.apache.jsp.pages.JobSeekerEduRegistration_jsp._jspService(JobSeekerEduRe
gistration_jsp.java:179)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:3
11)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter
..java:305)




I have the following class:

/*
* RegisterJobSeekerEduForm.java
*
* Created on January 2, 2000, 5:15 AM
*/
package app;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import java.util.List;
import java.util.ArrayList;


/**
*
* @author anair
*/
public class RegisterJobSeekerEduForm extends ActionForm {
private List exams = new ArrayList(7);
private List subjects = new ArrayList(7);
private List percents = new ArrayList(7);
private List grades = new ArrayList(7);
private List institutions = new ArrayList(7);
private List years = new ArrayList(7);


/** Creates a new instance of RegisterJobSeekerEduForm */
public RegisterJobSeekerEduForm() {

}

.
. //snip
.


/**
* Getter for property years.
* @return Value of property years.
*/
public Object getYears(int index) {
return years.get(index);
}

/**
* Setter for property years.
* @param years New value of property years.
*/
public void setYears(int index, Object value) {
this.years.set(index, value);
}


}

I am trying to use this in my jsp like this:


<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<jsp:useBean id="jeb" class="bean.JobSeekerEduBean" scope="session">
<jsp:setProperty name="jeb" property="*"/>
</jsp:useBean>
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>Job Seeker Educational</title>
<html:base />
</head>
<body>
<html:form action="/RegisterJobSeeker">
<html:errors/>
.
.
. //snip
.
.

<td colspan="0" align="center">
<logic:iterate id="year" name="years">
<bean:write name="year" property="name"/>
</logic:iterate>
</td>


Disclaimer: I do not use struts, but I am familiar with JSPs and Servlets
and use them on a fairly regular basis.

After reading the logic:iterate section from the help provided
(http://struts.apache.org/userGuide/struts-logic.html), my understanding is
that any name attribute (which, in the above code, you have specified as
"years") should match an id attribute for a jsp:useBean element. In the
code you provided, this is not the case, and that would explain why the
exception messages say "Cannot find bean years in any scope". If you wish
to have the iterate over the bean that you have specified in your useBean
element, then you should change the name attribute of the logic:iterate
element to "jeb".

There may be additional errors that I cannot find due to my inexperience
with tag libraries and struts, but I hope that my comment provided some
insight into your problem.
 
A

Arun Nair

Hi,
I based my code on the sample shown at
http://struts.apache.org/faqs/indexedprops.html


However I have to capture a job seekers educational qualifications. I
need to capture 'year of passing, for all the degrees/diploma a job
seeker may have earned.

I have tried your suggestion but could not make it work. I have
changed the code to :
/**
* Getter for property years.
* @return Value of property years.
*/
public java.util.List getYears() {
return years;
}

/**
* Setter for property years.
* @param years New value of property years.
*/
public void setYears(java.util.List years) {
this.years = years;
}


Now i get amessage 'No getter method for property name of bean year'


All I wand to do is to display 7 fields each(for years, degree earned,
name college/school etc.) in the jsp page.


I have a class
public class JobSeekerEduBean {
private List exams = new ArrayList(7);
private List subjects = new ArrayList(7);
private List percents = new ArrayList(7);
private List grades = new ArrayList(7);
private List institutions = new ArrayList(7);
private List years = new ArrayList(7);
public void save()
...
...
...
}

which I use save the data to the database

and i have one more class

public class RegisterJobSeekerEduForm extends ActionForm {
private ArrayList exams = new ArrayList(7);
private ArrayList subjects = new ArrayList(7);
private ArrayList percents = new ArrayList(7);
private ArrayList grades = new ArrayList(7);
private ArrayList institutions = new ArrayList(7);
private ArrayList years = new ArrayList(7);
 
S

Sudsy

Arun Nair wrote:

What this will do is iterate through the elements of the Collection
specified by "years" and give each the name "year" within the loop.
Since you've specified the property in the bean:write tag, Struts
is trying to perform "year.getName()". But I can't see from your
code what you've stuffed into "years". You create an empty ArrayList
with a capacity of 7 but I don't see the code to populate it.
What you require is another class. Something like this:

public class EduYear {
private String name;

public EduYear() {
}

public EduYear( String name ) {
this.name = name;
}

public String getName() {
return( name );
}

public void setName( String name ) {
this.name = name;
}
}

Then populate your ArrayList like so:

years.add( new EduYear( "some name" ) );

I've written an article on the use of some of the more advanced
feature of Struts which you can find here:
<http://www.sudsy.net/technology/struts-advanced.html>

Cheers!
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top