Hi RDB,
Thanks for your answer. I think I'm doing it OK though, let me paste here the
code of my app:
import java.util.Map;
import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ApplicationAware;
import database.DBConnector;
import com.opensymphony.xwork2.ActionSupport;
public class MyAction extends ActionSupport implements ApplicationAware {
private static Logger logger =
Logger.getLogger(MyAction.class);
private Map application;
public void setApplication(Map value) {
application = value;
}
public static final String DATABASE_KEY =
"database";
public DBConnector getDatabase() {
Map application = getApplication();
if(application == null) {
logger.error("null
application context");
return
null;
}
return
application.get(DATABASE_KEY);
}
/**
* <p>Provide application context.</p>
*/
public Map getApplication() {
return application;
}
}
This is the Action and the getApplication call is giving me a null application
object, what am I doing wrong?
Thank you,
Martin
> Not knowing the whole story, I may be off target... However, your code
> below is suspect to me:
>
> public class MyAction extends ActionSupport implements ApplicationAware {
>
> public DBConnector getDatabase() {
>
> Map application = getApplication();
>
> Object db = application.get(DATABASE_KEY);
>
> ....
>
> }
>
> At the top of the class you should have a variable declared called:
>
> private Map application;
>
> You need to implement/override setApplication(...), like so:
>
> public void setApplication(Map map) {
> this.application = map;
> }
>
> When struts enters your class it will set the application variable for
> you. Then inside your method above, you can dispense with:
>
> Map application = getApplication();
>
> As Laurie suggested, make sure you have (1) correct.
>
> If I'm missing the point, ignore my email.
>
> RDB
>
>
> Martín Mauri wrote:
>> Hi Laurie, I've checked all the points that you suggested, and I
>> think that #3 could be a possibility. The thing is that the
>> database object is not null when I do the
>> context.setAttribute(DATABASE_KEY, database); But in the Action
>> that implements the ApplicationAware interface I did the following:
>> Inside the method that gets invoked I did: Map application =
>> getApplication(); and it turned out to be that the application
>> object is null. Am I missing something in the configuration files?
>> Is it required to do an interceptor to transfer this object from
>> the Application Listener to my Action class?
>>
>> I'll appreciate your help,
>>
>> Thank you. Martin
>>
>> ----- Original Message ----- From: "Laurie Harper"
>> <[EMAIL PROTECTED]> To: <[email protected]> Sent:
Thursday,
>> September 25, 2008 4:45 AM Subject: Re: ApplicationListener issue
>>
>>
>> Martín Mauri wrote:
>>> Hi all, I'm having an issue while implementing an App Listener
>>> for my App. Thing is that the ApplicationListener appears to be
>>> working fine, as from the logs I can see that the
>>> contextInitialized method is being invoked.
>>>
>>> Inside the method I do this call, same way as the Mailreader
>>> sample app does:
>>>
>>> context.setAttribute(DATABASE_KEY, database); The problem comes
>>> when from my action:
>>>
>>> public class MyAction extends ActionSupport implements
>>> ApplicationAware {
>>>
>>> public DBConnector getDatabase() {
>>>
>>> Map application = getApplication();
>>>
>>> Object db = application.get(DATABASE_KEY);
>>>
>>> ....
>>>
>>> }
>>>
>>> db is returning null, like if the object got lost somewhere...
>>>
>>> here's the fragmen of my web.xml:
>>>
>>> <listener>
>>>
>>> <listener-class>
>>>
>>> myApp.ApplicationListener
>>>
>>> </listener-class>
>>>
>>> </listener>
>>>
>>> Any ideas?
>>
>> 1) there's something wrong in your implementation of
>> ApplicationAware and/or your getApplication() method
>>
>> 2) the 'database' variable was null when you called setAttribute()
>> in your listener
>>
>> 3) you have code somewhere which is overwriting the
>> application-scope property with a null value after the listener
>> initializes it
>>
>> 4) the value of DATABASE_KEY is different in the listener and the
>> action
>>
>> Those are the most obvious places to start. In fact, I can't think
>> of anything else it could be unless there's something very funky
>> going on, in which case I doubt it can be diagnosed through a
>> mailing list...
>>
>> L.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED] For
>> additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>> __________ Información de NOD32, revisión 3469
(20080924)
>> __________
>>
>> Este mensaje ha sido analizado con NOD32 antivirus system
>> http://www.nod32.com
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED] For
>> additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>