You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ali Ozoren <oz...@home.com> on 2001/09/22 21:46:14 UTC

Iterating over wrapped form bean's property

Hi all,

I have a Report bean, a ReportForm and a report.jsp.
ReportForm has a property called report of type Report. Hence it is using
the underlying report as storage.
In report.jsp, I refer to forms's properties as report.aProperty,
report.anotherProperty.
Report has a units property of type Hashtable. This hashtable contains
values of type Unit.

Config-struts looks like this:

    <action    path="/report"
               type="myApp.ReportAction"
               name="reportForm"
              scope="session"
              input="/jsp/report.jsp">
      <forward name="unit"  path="/jsp/unit.jsp"/>
    </action>

Yet,

When I try to iterate using

<logic:iterate name="report" property="units" id="unit">
  <bean:write name="unit" property="owner"/>			// owner is a property of
unit
</logic:iterate>

I get

javax.servlet.ServletException: Cannot find bean unit in scope null

I tried other variations of the iterate.

Now, when I look at the example that comes with Struts, I see the user
variable of type User being added to the session in the login action, before
it gets used in the registration.jsp. If I understand Struts, in my case,
reportForm that is defined in struts-config, scope=session, should be same
as adding reportForm programmatically. But apparently it isn't. Or is it? :)
Any idea?

Thanks,
--a


RE: Iterating over wrapped form bean's property

Posted by Adam Hardy <ad...@yahoo.com>.
Ali,

It sounds like your almost there.

Try adding type="yourpackage.Unit" to the iterate tag 

assuming that Unit is the name of your unit class.

Also if want to set the properties of nested beans
using the iterate tag I have found that it appears to
work but then does nothing.  What actually happens is
that the properties are returned to the form and not
to the bean!  From what I can gather from other posts
on this list it is a known issue (confirmation
anyone?) I don't know if it will be fixed in
struts1.1.

Anyway I have a work around that involves that works
for me. I'll email it to you if you want.

Sleep sounds like a good idea about now :-)

--- Ali Ozoren <oz...@home.com> wrote:
> I think I found couple of problems, the biggest of
> which being not including
> the <%@ taglib uri="/WEB-INF/struts-logic.tld"
> prefix="logic" %> in the
> beginning. I literally slept on it to figure out. Do
> I feel silly. However,
> there are also few more problems due to nested bean
> in the form bean and
> casting Hashtable entry to a Unit, which seems to be
> the only remaining
> problem since I am now able to iterate and print
> Unit's "toString()". I'll
> post a postmortem once I solve this last piece.
> Thanks,
> --a
> 
> -----Original Message-----
> From: Adam Hardy [mailto:adam_hardy@yahoo.com]
> Sent: Saturday, September 22, 2001 7:14 PM
> To: struts-user@jakarta.apache.org
> Subject: RE: Iterating over wrapped form bean's
> property
> 
> 
> Ali,
> 
> Sorry I think I gave you a red herring.
> 
> Units is a property of a report bean?
> 
> Then try
> 
> logic:iterate name="reportForm"
>  property="report.units"
>   id="unit">
>     <bean:write name="unit" property="owner"/>
> 
> assuming that reportForm has a getReport() method
> and
> Report has a getUnits() method.
> 
> Failing that try adding just a simple test String
> property to the form and see if you can output using
> bean:write.
> 
> PS Even if you don't need it Report will need a
> public setUnits(Hashtable input)
> {
> ...
> }
> 
> to conform to the bean standard. I normally just
> write
> the method signature with empty braces.
> 
> 
> --- Ali Ozoren <oz...@home.com> wrote:
> > Adam thanks for the reply. I put my feedback down
> > below:
> >
> > -----Original Message-----
> > From: Adam Hardy [mailto:adam_hardy@yahoo.com]
> > Sent: Saturday, September 22, 2001 5:22 PM
> > To: struts-user@jakarta.apache.org
> > Subject: Re: Iterating over wrapped form bean's
> > property
> >
> >
> > Ali,
> >
> > Try <logic:iterate name="reportForm"
> > property="units"
> >  id="unit">
> >    <bean:write name="unit" property="owner"/>
> >
> > a: did so. I still get "Cannot find bean unit in
> > scope null"
> >
> > You will also need to populate the form before you
> > use
> > it. If you enter /jsp/report.jsp as the url the
> null
> > error occurs because the reportForm has not been
> > created yet (although once it has been created it
> > should work fine because reportForm is sitting in
> > the
> > session).
> >
> > a: if it is a new report it will not be populated
> up
> > front.
> >
> > If you enter /report.do as the url struts knows
> that
> > this is an action, it creates a reportForm for you
> > and
> > passes it into the perform method of ReportAction.
> >
> > The perform method then populates the reportForm
> as
> > necessary and returns an ActionForward object
> > normally
> > done by:
> > return (mapping.findForward("success"));
> >
> > Now your newly populated form is sitting in the
> > session ready for your page to use.
> >
> > a: that is true. i verified this right before the
> > end in the ReportAction as
> > follows:
> >
> >       Report r =
> > (Report)request.getSession().getAttribute("rep");
> >       ReportForm rf =
> >
>
(ReportForm)request.getSession().getAttribute("reportForm");
> >       System.out.println("action rf id=" +
> > String.valueOf(rf.getReport().getId()));
> > 	System.err.println("count=" +
> > String.valueOf(r.getUnitsAsHashtable().size()));
> >
> > Just for testing, I put this dummy "rep" variable
> of
> > type Report into the
> > session in the login action. Both "rep" and
> > "reportForm" variables are in
> > session.
> >
> > Here is another bit of info. I checked the
> generated
> > servlet code for sample
> > registration.jsp and my report.jsp
> >
> > Sample has this snippet:
> >
> >                 /* ----  logic:iterate ---- */
> >
> > org.apache.struts.taglib.logic.IterateTag
> > _jspx_th_logic_iterate_0 = new
> > org.apache.struts.taglib.logic.IterateTag();
> >
> >
>
_jspx_th_logic_iterate_0.setPageContext(pageContext);
> >
> >
>
_jspx_th_logic_iterate_0.setParent(_jspx_th_logic_equal_5);
> >
> > _jspx_th_logic_iterate_0.setId("subscription");
> >
> > _jspx_th_logic_iterate_0.setName("user");
> >
> >
>
_jspx_th_logic_iterate_0.setProperty("subscriptions");
> >
> > Whereas mine doesn't, instead it has something
> like
> > this:
> >     out.write("\r\n\r\n<logic:iterate
> > scope=\"session\" name=\"rep\"
> > id=\"aUnit\" type=\"police.Unit\">\r\n  ");
> >
> > I turned the getter method of units property from
> >
> > Hashtable getUnits()
> > to
> > Unit[] getUnits(), with the necessary changes to
> > return an array, still no
> > luck. I think I religiously copied the sample
> > registration.jsp and related
> > files but here I am.
> >
> > Thanks,
> > --a
> >
> >
> >
> >
> >
> >
> >
> >
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Get email alerts & NEW webcam video instant
> messaging with Yahoo! Messenger.
> http://im.yahoo.com
> 


__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger. http://im.yahoo.com

RE: Iterating over wrapped form bean's property

Posted by Ali Ozoren <oz...@home.com>.
I think I found couple of problems, the biggest of which being not including
the <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> in the
beginning. I literally slept on it to figure out. Do I feel silly. However,
there are also few more problems due to nested bean in the form bean and
casting Hashtable entry to a Unit, which seems to be the only remaining
problem since I am now able to iterate and print Unit's "toString()". I'll
post a postmortem once I solve this last piece.
Thanks,
--a

-----Original Message-----
From: Adam Hardy [mailto:adam_hardy@yahoo.com]
Sent: Saturday, September 22, 2001 7:14 PM
To: struts-user@jakarta.apache.org
Subject: RE: Iterating over wrapped form bean's property


Ali,

Sorry I think I gave you a red herring.

Units is a property of a report bean?

Then try

logic:iterate name="reportForm"
 property="report.units"
  id="unit">
    <bean:write name="unit" property="owner"/>

assuming that reportForm has a getReport() method and
Report has a getUnits() method.

Failing that try adding just a simple test String
property to the form and see if you can output using
bean:write.

PS Even if you don't need it Report will need a
public setUnits(Hashtable input)
{
...
}

to conform to the bean standard. I normally just write
the method signature with empty braces.


--- Ali Ozoren <oz...@home.com> wrote:
> Adam thanks for the reply. I put my feedback down
> below:
>
> -----Original Message-----
> From: Adam Hardy [mailto:adam_hardy@yahoo.com]
> Sent: Saturday, September 22, 2001 5:22 PM
> To: struts-user@jakarta.apache.org
> Subject: Re: Iterating over wrapped form bean's
> property
>
>
> Ali,
>
> Try <logic:iterate name="reportForm"
> property="units"
>  id="unit">
>    <bean:write name="unit" property="owner"/>
>
> a: did so. I still get "Cannot find bean unit in
> scope null"
>
> You will also need to populate the form before you
> use
> it. If you enter /jsp/report.jsp as the url the null
> error occurs because the reportForm has not been
> created yet (although once it has been created it
> should work fine because reportForm is sitting in
> the
> session).
>
> a: if it is a new report it will not be populated up
> front.
>
> If you enter /report.do as the url struts knows that
> this is an action, it creates a reportForm for you
> and
> passes it into the perform method of ReportAction.
>
> The perform method then populates the reportForm as
> necessary and returns an ActionForward object
> normally
> done by:
> return (mapping.findForward("success"));
>
> Now your newly populated form is sitting in the
> session ready for your page to use.
>
> a: that is true. i verified this right before the
> end in the ReportAction as
> follows:
>
>       Report r =
> (Report)request.getSession().getAttribute("rep");
>       ReportForm rf =
>
(ReportForm)request.getSession().getAttribute("reportForm");
>       System.out.println("action rf id=" +
> String.valueOf(rf.getReport().getId()));
> 	System.err.println("count=" +
> String.valueOf(r.getUnitsAsHashtable().size()));
>
> Just for testing, I put this dummy "rep" variable of
> type Report into the
> session in the login action. Both "rep" and
> "reportForm" variables are in
> session.
>
> Here is another bit of info. I checked the generated
> servlet code for sample
> registration.jsp and my report.jsp
>
> Sample has this snippet:
>
>                 /* ----  logic:iterate ---- */
>
> org.apache.struts.taglib.logic.IterateTag
> _jspx_th_logic_iterate_0 = new
> org.apache.struts.taglib.logic.IterateTag();
>
>
_jspx_th_logic_iterate_0.setPageContext(pageContext);
>
>
_jspx_th_logic_iterate_0.setParent(_jspx_th_logic_equal_5);
>
> _jspx_th_logic_iterate_0.setId("subscription");
>
> _jspx_th_logic_iterate_0.setName("user");
>
>
_jspx_th_logic_iterate_0.setProperty("subscriptions");
>
> Whereas mine doesn't, instead it has something like
> this:
>     out.write("\r\n\r\n<logic:iterate
> scope=\"session\" name=\"rep\"
> id=\"aUnit\" type=\"police.Unit\">\r\n  ");
>
> I turned the getter method of units property from
>
> Hashtable getUnits()
> to
> Unit[] getUnits(), with the necessary changes to
> return an array, still no
> luck. I think I religiously copied the sample
> registration.jsp and related
> files but here I am.
>
> Thanks,
> --a
>
>
>
>
>
>
>
>


__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger.
http://im.yahoo.com


RE: Iterating over wrapped form bean's property

Posted by Adam Hardy <ad...@yahoo.com>.
Ali,

Sorry I think I gave you a red herring.

Units is a property of a report bean?

Then try

logic:iterate name="reportForm"
 property="report.units"
  id="unit">
    <bean:write name="unit" property="owner"/>

assuming that reportForm has a getReport() method and
Report has a getUnits() method.

Failing that try adding just a simple test String
property to the form and see if you can output using
bean:write.

PS Even if you don't need it Report will need a 
public setUnits(Hashtable input)
{
...
}

to conform to the bean standard. I normally just write
the method signature with empty braces.


--- Ali Ozoren <oz...@home.com> wrote:
> Adam thanks for the reply. I put my feedback down
> below:
> 
> -----Original Message-----
> From: Adam Hardy [mailto:adam_hardy@yahoo.com]
> Sent: Saturday, September 22, 2001 5:22 PM
> To: struts-user@jakarta.apache.org
> Subject: Re: Iterating over wrapped form bean's
> property
> 
> 
> Ali,
> 
> Try <logic:iterate name="reportForm"
> property="units"
>  id="unit">
>    <bean:write name="unit" property="owner"/>
> 
> a: did so. I still get "Cannot find bean unit in
> scope null"
> 
> You will also need to populate the form before you
> use
> it. If you enter /jsp/report.jsp as the url the null
> error occurs because the reportForm has not been
> created yet (although once it has been created it
> should work fine because reportForm is sitting in
> the
> session).
> 
> a: if it is a new report it will not be populated up
> front.
> 
> If you enter /report.do as the url struts knows that
> this is an action, it creates a reportForm for you
> and
> passes it into the perform method of ReportAction.
> 
> The perform method then populates the reportForm as
> necessary and returns an ActionForward object
> normally
> done by:
> return (mapping.findForward("success"));
> 
> Now your newly populated form is sitting in the
> session ready for your page to use.
> 
> a: that is true. i verified this right before the
> end in the ReportAction as
> follows:
> 
>       Report r =
> (Report)request.getSession().getAttribute("rep");
>       ReportForm rf =
>
(ReportForm)request.getSession().getAttribute("reportForm");
>       System.out.println("action rf id=" +
> String.valueOf(rf.getReport().getId()));
> 	System.err.println("count=" +
> String.valueOf(r.getUnitsAsHashtable().size()));
> 
> Just for testing, I put this dummy "rep" variable of
> type Report into the
> session in the login action. Both "rep" and
> "reportForm" variables are in
> session.
> 
> Here is another bit of info. I checked the generated
> servlet code for sample
> registration.jsp and my report.jsp
> 
> Sample has this snippet:
> 
>                 /* ----  logic:iterate ---- */
>                
> org.apache.struts.taglib.logic.IterateTag
> _jspx_th_logic_iterate_0 = new
> org.apache.struts.taglib.logic.IterateTag();
>                
>
_jspx_th_logic_iterate_0.setPageContext(pageContext);
>                
>
_jspx_th_logic_iterate_0.setParent(_jspx_th_logic_equal_5);
>                
> _jspx_th_logic_iterate_0.setId("subscription");
>                
> _jspx_th_logic_iterate_0.setName("user");
>                
>
_jspx_th_logic_iterate_0.setProperty("subscriptions");
> 
> Whereas mine doesn't, instead it has something like
> this:
>     out.write("\r\n\r\n<logic:iterate
> scope=\"session\" name=\"rep\"
> id=\"aUnit\" type=\"police.Unit\">\r\n  ");
> 
> I turned the getter method of units property from
> 
> Hashtable getUnits()
> to
> Unit[] getUnits(), with the necessary changes to
> return an array, still no
> luck. I think I religiously copied the sample
> registration.jsp and related
> files but here I am.
> 
> Thanks,
> --a
> 
> 
> 
> 
> 
> 
> 
> 


__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger. http://im.yahoo.com

RE: Iterating over wrapped form bean's property

Posted by Ali Ozoren <oz...@home.com>.
Adam thanks for the reply. I put my feedback down below:

-----Original Message-----
From: Adam Hardy [mailto:adam_hardy@yahoo.com]
Sent: Saturday, September 22, 2001 5:22 PM
To: struts-user@jakarta.apache.org
Subject: Re: Iterating over wrapped form bean's property


Ali,

Try <logic:iterate name="reportForm" property="units"
 id="unit">
   <bean:write name="unit" property="owner"/>

a: did so. I still get "Cannot find bean unit in scope null"

You will also need to populate the form before you use
it. If you enter /jsp/report.jsp as the url the null
error occurs because the reportForm has not been
created yet (although once it has been created it
should work fine because reportForm is sitting in the
session).

a: if it is a new report it will not be populated up front.

If you enter /report.do as the url struts knows that
this is an action, it creates a reportForm for you and
passes it into the perform method of ReportAction.

The perform method then populates the reportForm as
necessary and returns an ActionForward object normally
done by:
return (mapping.findForward("success"));

Now your newly populated form is sitting in the
session ready for your page to use.

a: that is true. i verified this right before the end in the ReportAction as
follows:

      Report r = (Report)request.getSession().getAttribute("rep");
      ReportForm rf =
(ReportForm)request.getSession().getAttribute("reportForm");
      System.out.println("action rf id=" +
String.valueOf(rf.getReport().getId()));
	System.err.println("count=" +
String.valueOf(r.getUnitsAsHashtable().size()));

Just for testing, I put this dummy "rep" variable of type Report into the
session in the login action. Both "rep" and "reportForm" variables are in
session.

Here is another bit of info. I checked the generated servlet code for sample
registration.jsp and my report.jsp

Sample has this snippet:

                /* ----  logic:iterate ---- */
                org.apache.struts.taglib.logic.IterateTag
_jspx_th_logic_iterate_0 = new org.apache.struts.taglib.logic.IterateTag();
                _jspx_th_logic_iterate_0.setPageContext(pageContext);
                _jspx_th_logic_iterate_0.setParent(_jspx_th_logic_equal_5);
                _jspx_th_logic_iterate_0.setId("subscription");
                _jspx_th_logic_iterate_0.setName("user");
                _jspx_th_logic_iterate_0.setProperty("subscriptions");

Whereas mine doesn't, instead it has something like this:
    out.write("\r\n\r\n<logic:iterate scope=\"session\" name=\"rep\"
id=\"aUnit\" type=\"police.Unit\">\r\n  ");

I turned the getter method of units property from

Hashtable getUnits()
to
Unit[] getUnits(), with the necessary changes to return an array, still no
luck. I think I religiously copied the sample registration.jsp and related
files but here I am.

Thanks,
--a









Re: Iterating over wrapped form bean's property

Posted by Adam Hardy <ad...@yahoo.com>.
Ali,

Try <logic:iterate name="reportForm" property="units"
 id="unit">
   <bean:write name="unit" property="owner"/>

You will also need to populate the form before you use
it. If you enter /jsp/report.jsp as the url the null
error occurs because the reportForm has not been
created yet (although once it has been created it
should work fine because reportForm is sitting in the
session).

If you enter /report.do as the url struts knows that
this is an action, it creates a reportForm for you and
passes it into the perform method of ReportAction.

The perform method then populates the reportForm as
necessary and returns an ActionForward object normally
done by:
return (mapping.findForward("success"));

Now your newly populated form is sitting in the
session ready for your page to use.

Also if you want report.jsp to use reportForm you
should forward to report.jsp. 

Try looking at struts-config_1_0.dtd in your struts
distribution it gives full details of what the
different parts of struts-config.xml does.

Adam.

--- Ali Ozoren <oz...@home.com> wrote:
> Hi all,
> 
> I have a Report bean, a ReportForm and a report.jsp.
> ReportForm has a property called report of type
> Report. Hence it is using
> the underlying report as storage.
> In report.jsp, I refer to forms's properties as
> report.aProperty,
> report.anotherProperty.
> Report has a units property of type Hashtable. This
> hashtable contains
> values of type Unit.
> 
> Config-struts looks like this:
> 
>     <action    path="/report"
>                type="myApp.ReportAction"
>                name="reportForm"
>               scope="session"
>               input="/jsp/report.jsp">
>       <forward name="unit"  path="/jsp/unit.jsp"/>
>     </action>
> 
> Yet,
> 
> When I try to iterate using
> 
> <logic:iterate name="report" property="units"
> id="unit">
>   <bean:write name="unit" property="owner"/>			//
> owner is a property of
> unit
> </logic:iterate>
> 
> I get
> 
> javax.servlet.ServletException: Cannot find bean
> unit in scope null
> 
> I tried other variations of the iterate.
> 
> Now, when I look at the example that comes with
> Struts, I see the user
> variable of type User being added to the session in
> the login action, before
> it gets used in the registration.jsp. If I
> understand Struts, in my case,
> reportForm that is defined in struts-config,
> scope=session, should be same
> as adding reportForm programmatically. But
> apparently it isn't. Or is it? :)
> Any idea?
> 
> Thanks,
> --a
> 


__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger. http://im.yahoo.com