You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Sandip Gaikwad <sa...@gmail.com> on 2007/08/21 08:11:43 UTC

: Cannot find bean: "collections" in any scope

Please help me.

Action class code

/*
 * AbstractUserRecordsAction.java
 *
 * Created on August 20, 2007, 6:04 PM
 */

package struts.action;

import hibernate.pojo.AbstractDetailsPojo;
import hibernate.pojo.AbstractLoginPojo;
import hibernate.pojo.AbstractUserInfoPojo;
import hibernate.pojo.AbstractUserRecordsPojo;
import hibernate.pojo.AdminLoginPojo;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
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.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.util.LabelValueBean;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.exception.SQLGrammarException;
import struts.plugin.HibernatePlugin;
/**
 *
 * @author WFCFD
 * @version
 */

public class AbstractUserRecordsAction extends Action {

    private ActionMessages errors;

    /* forward name="success" path="" */
    private final static String SUCCESS = "success";


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

        errors=new ActionMessages();

        List<AbstractUserInfoPojo> userinfo;
        List<AbstractDetailsPojo> details;
        Collection collections=new ArrayList();
        SessionFactory factory=null;
        Session session=null;

        try{
            factory=(SessionFactory)servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);

            session=factory.openSession();

            String SQL_QUERY="from AbstractUserInfoPojo";
            Query query=session.createQuery(SQL_QUERY);
            userinfo=(List<AbstractUserInfoPojo>)query.list();

            SQL_QUERY="from AbstractDetailsPojo";
            query=session.createQuery(SQL_QUERY);
            details=(List<AbstractDetailsPojo>)query.list();

            for(int i=0;i<userinfo.size();i++){

                AbstractUserInfoPojo ap=userinfo.get(i);
                AbstractDetailsPojo ad=details.get(i);


                long id=ap.getId();
                String title=ap.getTitle();
                String firstname=ap.getFirstname();
                String lastname=ap.getLastname();
                String country=ap.getCountry();
                String abstracttitle=ad.getAbstracttitle();

                collections.add(new LabelValueBean("id",""+id));
                collections.add(new LabelValueBean("title",title));
                collections.add(new LabelValueBean("firstname",firstname));
                collections.add(new LabelValueBean("lastname",lastname));
                collections.add(new LabelValueBean("country",country));
                collections.add(new
LabelValueBean("abstracttitle",abstracttitle));

               /* DynaActionForm frm=(DynaActionForm)form;
                frm.set("c",collections);*/

                request.setAttribute("collections",collections);
            }


            saveErrors(request,errors);

            if(!errors.isEmpty()){
                return mapping.findForward("failure");
            }



        }catch(SQLGrammarException sge){

              System.out.println("==========SQL Grammer Exception
:-=============");
              System.out.println("1> SQL Query= "+sge.getSQL());
              System.out.println("2> Error Code= "+sge.getErrorCode());
              System.out.println("3> SQL State= "+sge.getSQLState());
              SQLException sqe=sge.getSQLException();
              System.out.println("4> Message= "+sqe.getMessage());
              sge.printStackTrace();
              System.out.println("================================================");

          }
          catch (HibernateException e) {
               System.out.print("Hibernate Error");
              e.printStackTrace();

          } finally {

              session.close();

          }


          System.out.println("Record found");
          return mapping.findForward("success");
    }
}

Jsp file       (first way)
<%@ 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:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body bgcolor="#99CCFF">
<table>
     <tr bgcolor="#999999">
        <td width="41" align="center" class="darkbottom"><span
class="style16">ID</span></td>
        <td width="55" align="center" class="darkbottom"><span
class="style16">Title</span></td>
        <td width="119" align="center" class="darkbottom"><span
class="style16">First Name </span></td>
        <td width="124" align="center" class="darkbottom"><span
class="style16">Last Name</span></td>
        <td width="106" align="center" class="darkbottom"><span
class="style16">Country</span></td>
        <td width="224" align="center" class="darkbottom"><span
class="style16">Abstract Title </span></td>
        <td width="224" align="center" class="darkbottom"><span
class="style16">Commands</span></td>
        <td width="78" align="center" class="darkbottom"><span
class="style16">Download</span></td>
        </tr>
        <logic:iterate id="collection" name="collections">
            <tr bgcolor="#99CCFF">
                 <td class="faintbottomright"><bean:write
name="collection" property="id"/></td>
                <td class="faintbottomright"><bean:write
name="collection" property="title"/></td>
                <td class="faintbottomright"><bean:write
name="collection" property="firstname"/></td>
                <td class="faintbottomright"><bean:write
name="collection" property="lastname"/></td>
                <td class="faintbottomright"><bean:write
name="collection" property="country"/></td>
                <td class="faintbottomright"><bean:write
name="collection" property="abstracttitle"/></td>
                <td class="faintbottomright">&nbsp;</td>
                <td class="faintbottomright">&nbsp;</td>
            </tr>
        </logic:iterate>
</table>
</body>
</html:html>

Jsp file       (second way)
<%@ page import="java.util.Collection" %>
<%@ 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:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body bgcolor="#99CCFF">
<%! private Collection collections; %>
<% collections=(Collection)request.getAttribute("collections"); %>
<table>
     <tr bgcolor="#999999">
        <td width="41" align="center" class="darkbottom"><span
class="style16">ID</span></td>
        <td width="55" align="center" class="darkbottom"><span
class="style16">Title</span></td>
        <td width="119" align="center" class="darkbottom"><span
class="style16">First Name </span></td>
        <td width="124" align="center" class="darkbottom"><span
class="style16">Last Name</span></td>
        <td width="106" align="center" class="darkbottom"><span
class="style16">Country</span></td>
        <td width="224" align="center" class="darkbottom"><span
class="style16">Abstract Title </span></td>
        <td width="224" align="center" class="darkbottom"><span
class="style16">Commands</span></td>
        <td width="78" align="center" class="darkbottom"><span
class="style16">Download</span></td>
        </tr>
        <logic:iterate id="collection" name="collections">
            <tr bgcolor="#99CCFF">
                 <td class="faintbottomright"><bean:write
name="collection" property="id"/></td>
                <td class="faintbottomright"><bean:write
name="collection" property="title"/></td>
                <td class="faintbottomright"><bean:write
name="collection" property="firstname"/></td>
                <td class="faintbottomright"><bean:write
name="collection" property="lastname"/></td>
                <td class="faintbottomright"><bean:write
name="collection" property="country"/></td>
                <td class="faintbottomright"><bean:write
name="collection" property="abstracttitle"/></td>
                <td class="faintbottomright">&nbsp;</td>
                <td class="faintbottomright">&nbsp;</td>
            </tr>
        </logic:iterate>
</table>
</body>
</html:html>





Runtime error:-
javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot
find bean: "collections" in any scope

server log
Servlet.service() for servlet jsp threw exception
javax.servlet.jsp.JspException: Cannot find bean: "collections" in any scope
        at org.apache.struts.taglib.TagUtils.lookup(TagUtils.java:935)
        at org.apache.struts.taglib.logic.IterateTag.doStartTag(IterateTag.java:232)
        at org.apache.jsp.abstractuserrecords_jsp._jspService(abstractuserrecords_jsp.java:201)
        at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org