Here is my AvailLableEvents Thread Code:
package eventmanagement.util;
import java.util.List;
import eventmanagers.Events;
public class AvailableEvents extends Thread{
private List<Events>availableEventsList=null;
public List<Events> getAvailablEventsList() {
return availableEventsList;
}
public void setAvailableEventsList(List<Events> availableEventsList) {
this.availableEventsList = availableEventsList;
}
@Override
public void run() {
while(true){
this.availableEventsList=Events.getAvailableEvents();
//System.out.println("Executing");
try {
Thread.sleep(1000*60*30);//sleep for 30 minutes
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
I created a ServletContextListener Which starts the AvailableEvents Thread:
package eventmanagement.util;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class EventManagementServletContextListener implements
ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent event) {
AvailableEvents currentevents=new AvailableEvents();
currentevents.start();
event.getServletContext().setAttribute("EVENTS", currentevents);
}
}
Here is Listener's Config in web.xml:
<listener>
<listener-class>
eventmanagement.util.EventManagementServletContextListener
</listener-class>
</listener>
Struts tags for accessing the ListavailableEvents in my JSP:
<s:set name="curEventsThread" value="#application['EVENTS']"></s:set>
<s:set name="curEventsList" value="#curEventsThread.availableEventsList"/>
<s:iterator value="#curEventsList">
<s:property value="eventtitle"/>
</s:iterator>
eventtitle is instance variable of class Events
I'm able to get desired values by using this code in any Subclass of ActionSupport:
AvailableEvents ae=(AvailableEvents)ServletActionContext.getServletContext().getAttribute("EVENTS");
List<Events>ce=ae.getAvailablEventsList();
for(Events e:ce){
System.out.println(e.getEventtitle());
}
How to do this in Struts tags Thanks in Advance for any help you may provide.
Finally I got Results using struts tags in all my JSP:
<s:set name="availEvents" value="#application['EVENTS']"/>
<s:set name="mylist" value="#availEvents.getAvailableEventsList()"/>
<s:iterator value="mylist">
<s:property value="eventtitle"/><br/>
</s:iterator>