试图计算一个在Bean中调用的方法

时间:2014-10-10 23:25:08

标签: xpages managed-bean xpages-ssjs

我有一个托管bean,它返回许多描述应用程序的不同属性。因此可以通过调用

来调用它来返回数据库的FilePath

appProps [sessionScope.ssApplication] .helpFilePath

appProps [sessionScope.ssApplication] .ruleFilePath

我试图找出一个广义的情况,我需要根据compositeData变量中的值调用文件路径,该变量可以采用4个不同的值help / rule / app / main中的任何一个。

我写了这个SSJS并且它有效,但我想知道是否有更好的方法使它工作:

var target:String = compositeData.DBSource;
switch (target){
    case "app" :
        return appProps[sessionScope.ssApplication].appFilePath;
        break;
    case "help" :
        return appProps[sessionScope.ssApplication].helpFilePath;
        break;
    case "rule" :
        return appProps[sessionScope.ssApplication].ruleFilePath;
        break;
    case "main" :
        return appProps[sessionScope.ssApplication].mainFilePath;
        break;

}

我无法通过使用compositeData.DBSource +" FilePath"来确定是否有办法计算方法。 。当我尝试这个时,我得到一个错误,该方法不存在。使用上面的SSJS代码并不是一个真正的问题,但它似乎有点多余。

1 个答案:

答案 0 :(得分:2)

您可以在托管bean中创建一个以target作为参数的新方法:

public String getFilePath(String target) {
    String returnValue = "";
    if (target.equalsIgnoreCase("app")) {
        returnValue = this.appFilePath;
    } else if (target.equalsIgnoreCase("help")) {
        returnValue = this.helpFilePath;
    } else if (target.equalsIgnoreCase("rule")) {
        returnValue = this.ruleFilePath;
    } else if (target.equalsIgnoreCase("main")) {
        returnValue = this.mainFilePath;
    }

    return returnValue;
}

然后在你的SSJS中这样称呼它:

appProps[sessionScope.ssApplication].getFilePath(compositeData.DBSource);