You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Hartmut Bernecker <h....@dhw.de> on 2001/07/25 17:00:21 UTC

application scope problem

Please Help!

In my Struts-Application I create at a certain point (init Method of
myClass extends ActionServlet) a Bean for the Servlet Context.

ServletContext context = getServletContext();
Info info = (Info)context.getAttribute("host");
if (info == null)
{
   info = new Info();
   info.setBase("test");
   context.setAttribute("host", info);
}

Later in a JSP page I have that:

<jsp:useBean id="info" scope="application" class="myPackages.Info"/>
<p>Context Bean: <bean:write name="info" scope="application"
property="base"/></p>

But I don't reach the value of the property because every time when I
forward to that JSP-Page the Bean (Info) is created again and it's
properties initialized with null.

Any idea?
What is wrong???

Hartmut Bernecker

RE: application scope problem

Posted by DHarty <ha...@db.erau.edu>.
Hopefully someone will be able to elaborate on this, but I don't believe you
can't use
	<jsp:useBean id="info" scope="application" class="myPackages.Info"/>
interchangablby with struts bean tags.

You should be able to use:

	<bean:define id="info" scope="application" name="host" />

then, to display:

	<bean:write name="info" property="base"/>

Also, make sure that you are putting the bean "host" into the "application"
scope in you java code.
hth

D

> -----Original Message-----
> From: Hartmut Bernecker [mailto:h.bernecker@dhw.de]
> Sent: Wednesday, July 25, 2001 11:00 AM
> To: struts-user@jakarta.apache.org
> Subject: application scope problem
>
>
> Please Help!
>
> In my Struts-Application I create at a certain point (init Method of
> myClass extends ActionServlet) a Bean for the Servlet Context.
>
> ServletContext context = getServletContext();
> Info info = (Info)context.getAttribute("host");
> if (info == null)
> {
>    info = new Info();
>    info.setBase("test");
>    context.setAttribute("host", info);
> }
>
> Later in a JSP page I have that:
>
> <jsp:useBean id="info" scope="application" class="myPackages.Info"/>
> <p>Context Bean: <bean:write name="info" scope="application"
> property="base"/></p>
>
> But I don't reach the value of the property because every time when I
> forward to that JSP-Page the Bean (Info) is created again and it's
> properties initialized with null.
>
> Any idea?
> What is wrong???
>
> Hartmut Bernecker


Re: application scope problem

Posted by David Winterfeldt <dw...@yahoo.com>.
I'm not sure exactly how the jsp:useBean looks for the
class you specify, but since you are initializing the
class from the ActionServlet it would be safe to
assume it is there so you don't really need the
jsp:useBean.  So you should be able to do just this.

<p>Context Bean: <bean:write name="host"
scope="application" property="base"/></p>

If you weren't sure if the bean existed, you could
check for it with the logic:present tag.

David

--- Hartmut Bernecker <h....@dhw.de> wrote:
> Please Help!
> 
> In my Struts-Application I create at a certain point
> (init Method of
> myClass extends ActionServlet) a Bean for the
> Servlet Context.
> 
> ServletContext context = getServletContext();
> Info info = (Info)context.getAttribute("host");
> if (info == null)
> {
>    info = new Info();
>    info.setBase("test");
>    context.setAttribute("host", info);
> }
> 
> Later in a JSP page I have that:
> 
> <jsp:useBean id="info" scope="application"
> class="myPackages.Info"/>
> <p>Context Bean: <bean:write name="info"
> scope="application"
> property="base"/></p>
> 
> But I don't reach the value of the property because
> every time when I
> forward to that JSP-Page the Bean (Info) is created
> again and it's
> properties initialized with null.
> 
> Any idea?
> What is wrong???
> 
> Hartmut Bernecker


__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/