我想从另一个类调用存储在applicationScope中的ArrayList
我有一个类,这样的东西在公共变量名称AL_data中存储一大堆数据,方法getAllData只将数据存储在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();
}
}
}
我使用applicationScope在faces-config中将类设置为托管bean
<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>
我还有另一个类,我想用它来读取应用程序范围
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;
}
我使用sessionScope将此类设置为管理bean。
<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>
我想知道如何调用应用程序范围类并访问它的公共属性,或者如何返回我的ArrayList。
谢谢
托马斯
向您的应用程序作用域bean添加一个公共方法,其他Java类可以使用它来访问该bean的实例:
public static Application get() {
FacesContext context = FacesContext.getCurrentInstance();
return (Application) context.getApplication().getVariableResolver().resolveVariable("App");
}
然后,您可以使用该方法从Actions类获取应用程序作用域bean的实例,然后访问该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;
}
只要范围规则允许,最好的方法是让框架处理将属性和bean注入其他bean。
由于您需要在会话范围的bean内部使用应用程序范围的bean,因此您可以按如下方式定义注入:
<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-property>
<property-name>application</property-name>
<value>#{App}</value>
</managed-property>
</managed-bean>
要接受在您内部注入属性,操作bean定义一个公共方法,如下所示:
public class actions implements Serializable {
private Application app;
public void setApplication(Application app) {
this.app = app;
}
}
通过这种方式,您可以在类中的任何位置保存app bean,而无需在需要时一次又一次地解决它。您可能还会认为引用的应用程序bean太多而您只想要数据。那时你只需要调整faces-config.xml
和接收方法就可以得到它。
<managed-property>
<property-name>allData</property-name>
<value>#{App.allData}</value>
</managed-property>
public class actions implements Serializable {
private static final long serialVersionUID = 1L;
private ArrayList<District> allData;
public void setAllData(ArrayList<District> allData) {
this.allData = allData;
}
}
现在,两条建议。
public class Application implements Serializable {
private static final long serialVersionUID = 1L;
private ArrayList<District> allData;
//public Application(){
// try {
// getAllData();
// } catch (NotesException e) {
// e.printStackTrace();
// }
//}
public ArrayList<District> getAllData() {
if (allData == null) {
try {
allData = // your logic result
} catch (NotesException e) {
throw new FacesException(e);
}
}
return allData;
}
}