我想从另一个类调用存储在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将此类设置为Managed 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。
谢谢
托马斯
答案 0 :(得分:3)
向应用程序范围的Bean添加一个公共方法,其他Java类可以使用该方法来访问该Bean的实例:
DELIMITER $$
CREATE PROCEDURE insertar_datos (name VARCHAR(25), N INT, AP
VARCHAR(15),
AM VARCHAR(15), Nom VARCHAR(30), DNI VARCHAR(8), Direc VARCHAR(30))
BEGIN
SET @tableName = Name;
SET @NName= N;
SET @APName = AP;
SET @AMName = AM;
SET @NomName = Nom;
SET @DNIName = DNI;
SET @DirecName = Direc;
SET @q = CONCAT('
INSERT INTO `' , @tableName, '` VALUES(
`',@NName,'`,
`',@APName,'`,
`',@AMName,'`,
`',@NomName,'`,
`',@DNIName,'`,
`',@DirecName,'`
)
');
PREPARE stmt FROM @q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END $$
DELIMITER;
然后,您可以使用该方法从Actions类访问应用程序范围的bean的实例,然后访问该bean的方法和变量:
public static Application get() {
FacesContext context = FacesContext.getCurrentInstance();
return (Application) context.getApplication().getVariableResolver().resolveVariable("App");
}
答案 1 :(得分:3)
只要范围规则允许,最好的方法就是让框架处理将属性和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>
要在您的Actions bean中接受该属性的注入,请如下定义一个公共方法:
public class actions implements Serializable {
private Application app;
public void setApplication(Application app) {
this.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;
}
}