File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Struts2 that acts as a ServletContextListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 that acts as a ServletContextListener" Watch "Struts2 that acts as a ServletContextListener" New topic
Author

Struts2 that acts as a ServletContextListener

Paul Michael Serrano
Ranch Hand

Joined: Nov 22, 2004
Posts: 59
Good Day,

I would like to ask if there is a class in Struts2.1.6 that acts like a ServletContextListener? The reason why I'm trying to do this is I do have a values that would be fetched in the DB and I want it available in my entire application.

Another thing, if there is really a class for my question, how would I put it in my page(application scope)?

Thanks,
Paul
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Normally you'd just implement a ServletContextListener.
Paul Michael Serrano
Ranch Hand

Joined: Nov 22, 2004
Posts: 59
Could I have a class that extends ActionSupport and at the same time implements a ServletContextListener and ApplicationAware interfaces?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Of course, but that doesn't necessarily mean it'll work the way you think it will, but you don't really state what you're trying to accomplish, so it's hard to say. If you're trying to get the benefits of Spring integration, for example, it probably won't work--the container instantiates servlet context listeners, not Spring.

If you're trying to use Spring-oriented stuff there's an event interface you can implement then check for an application context startup message.

What are you trying to do?
Paul Michael Serrano
Ranch Hand

Joined: Nov 22, 2004
Posts: 59
We are currently working on Struts2 framework only with version 2.1.6, we are not integrating it with Spring.

All I want to do, is to have a visibility of my ArrayList in my whole application. So I'm trying to do it like this...


I also put this snippet of code in my JSP, just to check if I'm getting it in my page. But unfortunately application.languageList is always null eventhough I've commented the DBconnection and force to put values in my ArrayList.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why does this need to be an action?
Paul Michael Serrano
Ranch Hand

Joined: Nov 22, 2004
Posts: 59
I don't know if I'm right. I tried extending ActionSupport just to be sure that I could have a value in my ApplicationAware. But still, I'm failing to get the value that I've set here....

testMap.put("languageList", languageList);
this.setApplication(testMap);


Thanks,
-Paul
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're not saving anything to the application map, you're creating a *new* map and putting the value in that. Then you call the action's setter, and overwrite the reference to the actual application map.

I still don't understand why this needs to be an action if you're not doing anything related to Struts 2.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts2 that acts as a ServletContextListener