Xpages 和 EL:检索 HashMap 值的语法

Xpages and EL: Syntax for retrieving HashMap Values

我有一个用Java写的cacheBean。我使用 EL 成功地提取了 Vectors,但是我有一个 HashMap,当我尝试访问一个值时,我抛出了一个错误。

我的 cacheBean 是:

package com.scoular.cache;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Vector;

import org.openntf.domino.utils.Factory;
import org.openntf.domino.Database;
import org.openntf.domino.Session;
import org.openntf.domino.View;
import org.openntf.domino.ViewEntry;
import org.openntf.domino.ViewNavigator;

public class PCConfig implements Serializable {

    private static final long serialVersionUID = 1L;

    private Database thisDB;
    private Database compDirDB;
    public Database PCDataDB;

    public HashMap<Integer, String> status = new HashMap<Integer, String>();
    public static Vector<Object> geoLocations = new Vector<Object>();
    public static Vector<Object> models = new Vector<Object>();

    // @SuppressWarnings("unchecked")
    private void initConfigData() {
        try {
            getStatus();
            getGeoLocations();
            getModels();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public PCConfig() {
        // initialize application config
        System.out.println("Starting CacheBean");
        initConfigData();
        System.out.println("Ending CacheBean");
    }


    public static void setModels(Vector<Object> models) {
        PCConfig.models = models;
    }

    public void getStatus() {
        status.put(1, "In Inventory");
        status.put(2, "Being Built");
        status.put(3, "In Production");
        status.put(4, "Aquiring PC");
        status.put(5, "Decommissioning");
    }

    public Vector<Object> getGeoLocations() {

        if (PCConfig.geoLocations == null || PCConfig.geoLocations.isEmpty()) {
            try {
                Session session = Factory.getSession();
                thisDB = session.getCurrentDatabase();
                compDirDB = session.getDatabase(thisDB.getServer(), "compdir.nsf", false);
                View geoView = compDirDB.getView("xpGeoLocationsByName");
                for (ViewEntry ce : geoView.getAllEntries()) {
                    Vector<Object> rowVal = ce.getColumnValues();
                    geoLocations.addElement(rowVal.elementAt(0));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return geoLocations;
    }

    public Vector<Object> getModels() {

        if (PCConfig.models == null || PCConfig.models.isEmpty()) {
            try {
                Session session = Factory.getSession();
                thisDB = session.getCurrentDatabase();
                PCDataDB = session.getDatabase(thisDB.getServer(), "scoApps\PC\PCData.nsf", false);
                ViewNavigator vn = PCDataDB.getView("dbLookupModels").createViewNav();
                ViewEntry entry = vn.getFirstDocument();
                while (entry != null) {
                    Vector<Object> thisCat = entry.getColumnValues();
                    if (entry.isCategory()) {
                        String thisCatString = thisCat.elementAt(0).toString();
                        models.addElement(thisCatString);
                    }
                    entry = vn.getNext(entry);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return models;
    }
}

获取a值的代码是:

<xp:text escape="true" id="computedField2">
    <xp:this.value><![CDATA[#{PCConfig.status[0]}]]></xp:this.value></xp:text>

您的方法 getStatus() 必须 return HashMap。

public HashMap<Integer, String> getStatus() {
    ...
    return status;
}

此外,#{PCConfig.status[0]} 尝试读取键 0 的值。但是你的 HashMap 状态中没有键 0...

据我所知,您应该按照以下步骤操作

#{javascript:PCConfig.status.get(1)}