thx a lot,
this was my question. :-)


"Craig R. McClanahan" schrieb:

> On Wed, 21 Mar 2001, hunkpapa wrote:
>
> >
> >
> > "Craig R. McClanahan" schrieb:
> >
> > > On Fri, 4 Jan 1980, hunkpapa wrote:
> > >
> > > > Hi,
> > > > I've a problem with beans.
> > > > I can use a bean with the writeTag and this works out well.
> > > >
> > > > But how can this bean access the session ?
> > > > I want to read values I placed in the session
> > > > to use them in the getter methods of the bean.
> > > >
> > > >
> > > >
> > >
> > > If the bean itself is a String you want to write:
> > >
> > >         <bean:write name="beanName" scope="session"/>
> > >
> > > If the bean is an object whose property you want to write:
> > >
> > >         <bean:write name="customer" property="companyName"
> > >                 scope="session"/>
> > >
> > > In general, most Struts tags that accept a "name" attribute also let you
> > > specify a "scope" attribute.
> > >
> > > Craig McClanahan
> >
> > OK,
> > How is the source of the bean , if i want access the session ?
> >
> >
>
> Do you mean that you want the JavaBean itself to access the session?  If
> so, I'm sorry I misunderstood you previously.
>
> In general, a JavaBean will *not* have direct access to the session, or
> any other aspect of the servlet API.  You could pass in the current
> session as a method parameter (say, from your Action), but that would
> create a dependency that this bean would only work inside a servlet-based
> web application.
>
> You probably want to look at why your bean needs access to the session in
> the first place.  Usually, this would be because you need access to some
> other beans that are stored there.  In such a case, you might consider
> refactoring your design so that the relevant beans reference each other
> directly.
>
> Let's take one really simple example -- let's say you have a Customer
> bean, and an ArrayList of Order beans, as two different session
> attributes.  But, in order to display information about an order, you need
> a reference to the customer to get the customer's name.
>
> One strategy would be to modify your Order bean method so that it has a
> "customer" property:
>
>         public class Order {
>             ...
>             public Customer getCustomer();
>             public void setCustomer(Customer
>             ...
>         }
>
> and, when you are creating the order beans in the first place, be sure you
> call setCustomer() at the right times.
>
> If you have done this, you no longer need to access the session from an
> Order bean in order to display the customer name.  Instead, you can do
> things like this (where "orders" is your ArrayList of orders in the
> session):
>
>         <logic:iterate id="order" name="orders" scope="session">
>           Order number is <bean:write name="order" property="orderId"/>
>           and customer name is
>           <bean:write name="order" property="customer.name"/>
>         </logic:iterate>
>
> If this is still not what you are after, why don't you describe your
> problem in a little more detail.
>
> Craig

Reply via email to