将文档设置为Java类中的全局属性,不起作用

时间:2018-10-21 19:05:29

标签: java xpages xpages-ssjs

在Xpages应用程序中,我有一个Java类,我想在其中将Document设置为全局属性,并在所有方法中重复使用。该文档代表Notes配置文档,我只想执行一次查找。不幸的是,它没有按预期工作。也许有人可以指导我进行适当的处​​理?

首先,我建立了一个托管bean:

<managed-bean>
    <managed-bean-name>emplDataMining</managed-bean-name>
    <managed-bean-class>se.bank.employeeApp.utils.EmployeeDataMining</managed-bean-class>
    <managed-bean-scope>view</managed-bean-scope>
</managed-bean>

我的课程包含几种将用于不同系统的方法。系统的所有URL都存储在Notes配置文档中,我只想加载一次并在这些方法之间重复使用

public class EmployeeDataMining implements Serializable{

    private static final long serialVersionUID = 1L;
    private Document configuration;

    //constructor class. not so very special, so I wont post it

    public void getConfiguration(){
        //setting up database and view
        //only 1 document stored in the view so I can hard-code the reference
        configuration = vw.getDocumentByKey("ConfigDocument", true);
        //... rest of code e.g. setting up httpclient, JSONobj
    }

    public void collectDataFromSystemX(CloseableHttpClient httpclient, Employee employee, JSONObject JSONobj){
        //I wont post all of my code  
        HttpPost httpPost = new HttpPost(this.configuration.getItemValueString("urlSystemX"));
        //this.configuration is null :-?
        //..rest of code
    }

    public void collectDataFromSystemY(CloseableHttpClient httpclient, Employee employee, JSONObject JSONobj){
        //I wont post all of my code  
        HttpPost httpPost = new HttpPost(this.configuration.getItemValueString("urlSystemY"));
        //this.configuration is null :-?
        //..rest of code
    }

}

我的代码是从SSJS启动的:

emplDataMining.getConfiguration(); 
emplDataMining.collectDataFromSystemX(//passing in the variables which are setup in getConfiguration method)

所以我主要担心的是,没有正确设置或在方法之间交换配置文件Document。

有人可以告诉我我忽略了什么吗?

1 个答案:

答案 0 :(得分:2)

有2个问题:

  • 在视图范围内,将为您打开的每个文档重新加载Bean。如果是配置文件,则要使用会话作用域
  • 您不能将Notes对象存储在托管Bean中(请求范围可能会起作用)。您要做的是:在bean的构造函数中,加载文档并将字段值提取到bean内部变量(字符串,列表等)中。那会给你你想要的东西