I want to call an ArrayList stored in applicationScope from another class
I have a class, something like this that stores a bounch of data in a public variable names AL_data, the method getAllData just store the data in AL_data
public class Application implements Serializable{
private static final long serialVersionUID = 1L;
public ArrayList<District> AL_data;
public Application(){
try {
getAllData();
} catch (NotesException e) {
e.printStackTrace();
}
}
}
And I have set the class as a managed bean in faces-config using applicationScope
<managed-bean>
<managed-bean-name>App</managed-bean-name>
<managed-bean-class>com.utils.Application</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
I also have another class that I want to use to read the applications scope
public class actions {
private static Map<String, Object> applicationScope() {
FacesContext context = FacesContext.getCurrentInstance();
return (Map<String, Object>) context.getApplication().getVariableResolver().resolveVariable(context,"applicationScope");
}
public Vector<String> getDataForCurrentUser() {
try {
// how do I access the AL_data arraylist stored in applicationscope
// ArrayList m = (ArrayList) this.applicationScope();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
and I have set this class as a manages bean using sessionScope.
<managed-bean>
<managed-bean-name>Actions</managed-bean-name>
<managed-bean-class>com.utils.actions</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
I would like to know how I can call the application scope class and access it's public properties, or how I can return my ArrayList.
thanks
Thomas
Add a public method to your application scoped bean that other Java classes can use to access the instance of that bean:
public static Application get() {
FacesContext context = FacesContext.getCurrentInstance();
return (Application) context.getApplication().getVariableResolver().resolveVariable("App");
}
You can then use that method to get to the instance of the application scoped bean from your Actions class and then access the methods and variables of that bean:
public class actions {
public Vector<String> getDataForCurrentUser() {
// Access the AL_data arraylist stored in the App application scoped bean
ArrayList<District> m = Application.get().AL_data;
}