XPages:如何从会话范围bean访问应用程序范围bean



在我的会话范围管理bean中需要一个来自应用程序范围管理bean的值。不知道该怎么做。看到一个帖子在这里:https://guedebyte.wordpress.com/2012/05/19/accessing-beans-from-java-code-in-xpages-learned-by-reading-the-sourcecode-of-the-extensionlibrary/但我得到一堆错误…我还发现:JSF 2.0从另一个bean访问应用程序范围bean,所以我想也许我需要重新定义我的应用程序bean??完全无能…

我怎样才能做到呢?

下面是应用程序作用域bean的代码:

public class AppConfig implements Serializable { 
    private static final long serialVersionUID = 2768250939591274442L;
    public AppConfig() {
        initDefaults();
        initFromConfigDoc();
    }
    // Control the number of entries displayed in the widgets
    private int nbWidgetFavorites = 0;
    private int nbWidgetMostPopular = 0;
    private int nbWidgetToolbox = 0;
    // Control the number of entries to display in the What's new view
    private int nbWhatsNew = 0;
    private String showDetailsWhatsNew = "no";
    //controls various search options
    private int nbSearchResults = 0;
    private int nbMaxSearchResults = 0;
    //the home page to use for each language
    private String homePageUNID_FR = "";
    private String homePageUNID_EN = "";
    //application email address to use (webmaster)
    //  DEV ADDRESS
    private String appEmailAddress = "DEVTEAMTEST/DEV@DEVELOPMENTCORP";
    //  UAT ADDRESS
    //  PROD ADDRESS
    //path to the stats DB
    private String statsDB = "";
    //application message, if needed
    private String systemMessageFR = "";
    private String systemMessageEN = "";
    //default lang (defined here as session bean will read from the App bean first to
    //  see if there's a value stored there)
    private String defaultLang = "";
    //default prov
    private String defaultProv = "";
    // show Province drop down?
    private String showProv = "no";
    //various text for "share this link" emails
    private String senderEmail = "";
    private String senderName = "";
    private String appURL = "";
    private String emailText = "";
    private String clickLinkText = "";
    private String emailFooter = "";
    private String messageIntro = "";
    private String allowRatingModification = "";


    /*****************************************************************************/
    private void initDefaults() {
        // Control the number of entries displayed in the widgets
        nbWidgetFavorites = 10;
        nbWidgetMostPopular = 10;
        nbWidgetToolbox = 10;
        nbWhatsNew = 15;
        showDetailsWhatsNew = "no";
        nbSearchResults = 25;
        nbMaxSearchResults = 100;
        homePageUNID_FR = "";
        homePageUNID_EN = "";
        appEmailAddress = "DEVTEAMTEST/DEV@DEVELOPMENTCORP";
        statsDB = "belair\xBiblioStats.nsf";
        systemMessageFR = "";
        systemMessageEN = "";
        defaultLang = "FR";
        defaultProv = "QC";
        showProv = "no";
        allowRatingModification = "1";
    }
    /*****************************************************************************/
    public boolean persistToConfigDoc() {
        //write content of sessionScope vars to config doc
        try {
            Database db = ExtLibUtil.getCurrentSession().getCurrentDatabase();
            View view = db.getView("AppConfig");
            Document doc = view.getFirstDocument();
            if(doc == null) {
                doc = db.createDocument();
                doc.replaceItemValue("form", "AppConfig");
            }
            doc.replaceItemValue("nbWidgetFavorites", this.nbWidgetFavorites);
            doc.replaceItemValue("nbWidgetMostPopular", this.nbWidgetMostPopular);
            doc.replaceItemValue("nbWidgetToolbox", this.nbWidgetToolbox);
            doc.replaceItemValue("nbWidgetToolbox", this.nbWidgetToolbox);
            doc.replaceItemValue("nbWhatsNew", this.nbWhatsNew);
            doc.replaceItemValue("showDetailsWhatsNew", this.showDetailsWhatsNew);
            doc.replaceItemValue("nbSearchResults", this.nbSearchResults);
            doc.replaceItemValue("nbMaxSearchResults", this.nbMaxSearchResults);
            doc.replaceItemValue("homePageUNID_FR", this.homePageUNID_FR);
            doc.replaceItemValue("homePageUNID_EN", this.homePageUNID_EN);
            doc.replaceItemValue("appEmailAddress", this.appEmailAddress);
            doc.replaceItemValue("statsDB", this.statsDB);
            doc.replaceItemValue("systemMessageFR", this.systemMessageFR);
            doc.replaceItemValue("systemMessageEN", this.systemMessageEN);
            doc.replaceItemValue("defaultLang", this.defaultLang);
            doc.replaceItemValue("defaultProv", this.defaultProv);
            doc.replaceItemValue("showProv", this.showProv);
            doc.replaceItemValue("senderEmail", this.senderEmail);
            doc.replaceItemValue("senderName", this.senderName);
            doc.replaceItemValue("appURL", this.appURL);
            doc.replaceItemValue("emailText", this.emailText);
            doc.replaceItemValue("clickLinkText", this.clickLinkText);
            doc.replaceItemValue("emailFooter", this.emailFooter);
            doc.replaceItemValue("messageIntro", this.messageIntro);
            doc.replaceItemValue("allowRatingModification", this.allowRatingModification);
            doc.save();
            return true;
        } catch (NotesException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }
    /*****************************************************************************/
    public void initFromConfigDoc() {
        try {
            Database db = ExtLibUtil.getCurrentSession().getCurrentDatabase();
            View view = db.getView("AppConfig");
            Document doc = view.getFirstDocument();
            if(doc != null) {
                //load default values
                if(doc.hasItem("nbWidgetFavorites")) {
                    int tmp = doc.getItemValueInteger("nbWidgetFavorites"); 
                    if(tmp > 0) {
                        this.nbWidgetFavorites = tmp;
                    }
                }
                if(doc.hasItem("nbWidgetMostPopular")) {
                    int tmp = doc.getItemValueInteger("nbWidgetMostPopular"); 
                    if(tmp > 0) {
                        this.nbWidgetMostPopular = tmp;
                    }
                }
                if(doc.hasItem("nbWidgetToolbox")) {
                    int tmp = doc.getItemValueInteger("nbWidgetToolbox"); 
                    if(tmp > 0) {
                        this.nbWidgetToolbox = tmp;
                    }
                }
                if(doc.hasItem("nbWhatsNew")) {
                    int tmp = doc.getItemValueInteger("nbWhatsNew"); 
                    if(tmp > 0) {
                        this.nbWhatsNew = tmp;
                    }
                }
                if(doc.hasItem("showDetailsWhatsNew")) {
                    String tmp = doc.getItemValueString("showDetailsWhatsNew"); 
                    this.showDetailsWhatsNew = tmp;
                }
                if(doc.hasItem("nbSearchResults")) {
                    int tmp = doc.getItemValueInteger("nbSearchResults"); 
                    if(tmp > 0) {
                        this.nbSearchResults = tmp;
                    }
                }
                if(doc.hasItem("nbMaxSearchResults")) {
                    int tmp = doc.getItemValueInteger("nbMaxSearchResults"); 
                    if(tmp > 0) {
                        this.nbMaxSearchResults = tmp;
                    }
                }
                if(doc.hasItem("homePageUNID_FR")) {
                    String tmp = doc.getItemValueString("homePageUNID_FR"); 
                    if(!"".equals(tmp)) {
                        this.homePageUNID_FR = tmp;
                    }
                }
                if(doc.hasItem("homePageUNID_EN")) {
                    String tmp = doc.getItemValueString("homePageUNID_EN"); 
                    if(!"".equals(tmp)) {
                        this.homePageUNID_EN = tmp;
                    }
                }
                if(doc.hasItem("appEmailAddress")) {
                    String tmp = doc.getItemValueString("appEmailAddress"); 
                    if(!"".equals(tmp)) {
                        this.appEmailAddress = tmp;
                    }
                }
                if(doc.hasItem("statsDB")) {
                    String tmp = doc.getItemValueString("statsDB"); 
                    if(!"".equals(tmp)) {
                        this.statsDB = tmp;
                    }
                }
                if(doc.hasItem("systemMessageFR")) {
                    String tmp = doc.getItemValueString("systemMessageFR"); 
                    if(!"".equals(tmp)) {
                        this.systemMessageFR = tmp;
                    }
                }
                if(doc.hasItem("systemMessageEN")) {
                    String tmp = doc.getItemValueString("systemMessageEN"); 
                    if(!"".equals(tmp)) {
                        this.systemMessageEN = tmp;
                    }
                }
                if(doc.hasItem("defaultLang")) {
                    String tmp = doc.getItemValueString("defaultLang"); 
                    if(!"".equals(tmp)) {
                        this.defaultLang = tmp;
                    }
                }
                if(doc.hasItem("defaultProv")) {
                    String tmp = doc.getItemValueString("defaultProv"); 
                    if(!"".equals(tmp)) {
                        this.defaultProv = tmp;
                    }
                }
                if(doc.hasItem("showProv")) {
                    String tmp = doc.getItemValueString("showProv"); 
                    if(!"".equals(tmp)) {
                        this.showProv = tmp;
                    }
                }
                if(doc.hasItem("senderEmail")) {
                    String tmp = doc.getItemValueString("senderEmail"); 
                    if(!"".equals(tmp)) {
                        this.senderEmail = tmp;
                    }
                }
                if(doc.hasItem("senderName")) {
                    String tmp = doc.getItemValueString("senderName"); 
                    if(!"".equals(tmp)) {
                        this.senderName = tmp;
                    }
                }
                if(doc.hasItem("appURL")) {
                    String tmp = doc.getItemValueString("appURL"); 
                    if(!"".equals(tmp)) {
                        this.appURL = tmp;
                    }
                }
                if(doc.hasItem("emailText")) {
                    String tmp = doc.getItemValueString("emailText"); 
                    if(!"".equals(tmp)) {
                        this.emailText = tmp;
                    }
                }
                if(doc.hasItem("clickLinkText")) {
                    String tmp = doc.getItemValueString("clickLinkText"); 
                    if(!"".equals(tmp)) {
                        this.clickLinkText = tmp;
                    }
                }
                if(doc.hasItem("emailFooter")) {
                    String tmp = doc.getItemValueString("emailFooter"); 
                    if(!"".equals(tmp)) {
                        this.emailFooter = tmp;
                    }
                }
                if(doc.hasItem("messageIntro")) {
                    String tmp = doc.getItemValueString("messageIntro"); 
                    if(!"".equals(tmp)) {
                        this.messageIntro = tmp;
                    }
                }
                //allowRatingModification
                if(doc.hasItem("allowRatingModification")) {
                    String tmp = doc.getItemValueString("allowRatingModification"); 
                    if(!"".equals(tmp)) {
                        this.allowRatingModification = tmp;
                    }
                }
            }
        } catch (NotesException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /*****************************************************************************/
    public int getNbWidgetFavorites() {
        return nbWidgetFavorites;
    }
    public void setNbWidgetFavorites(int nbWidgetFavorites) {
        this.nbWidgetFavorites = nbWidgetFavorites;
    }
    public int getNbWidgetMostPopular() {
        return nbWidgetMostPopular;
    }
    public void setNbWidgetMostPopular(int nbWidgetMostPopular) {
        this.nbWidgetMostPopular = nbWidgetMostPopular;
    }
    public int getNbWidgetToolbox() {
        return nbWidgetToolbox;
    }
    public void setNbWidgetToolbox(int nbWidgetToolbox) {
        this.nbWidgetToolbox = nbWidgetToolbox;
    }
    public void setNbWhatsNew(int nbWhatsNew) {
        this.nbWhatsNew = nbWhatsNew;
    }
    public int getNbWhatsNew() {
        return nbWhatsNew;
    }
    public void setShowDetailsWhatsNew(String showDetailsWhatsNew) {
        this.showDetailsWhatsNew = showDetailsWhatsNew;
    }
    public String getShowDetailsWhatsNew() {
        return showDetailsWhatsNew;
    }
    public int getNbSearchResults() {
        return nbSearchResults;
    }
    public void setNbSearchResults(int nbSearchResults) {
        this.nbSearchResults = nbSearchResults;
    }
    public int getNbMaxSearchResults() {
        return nbMaxSearchResults;
    }
    public void setNbMaxSearchResults(int nbMaxSearchResults) {
        this.nbMaxSearchResults = nbMaxSearchResults;
    }
    public String getHomePageUNID_FR() {
        return homePageUNID_FR;
    }
    public void setHomePageUNID_FR(String homePageUNID_FR) {
        this.homePageUNID_FR = homePageUNID_FR;
    }
    public String getHomePageUNID_EN() {
        return homePageUNID_EN;
    }
    public void setHomePageUNID_EN(String homePageUNID_EN) {
        this.homePageUNID_EN = homePageUNID_EN;
    }
    public String getAppEmailAddress() {
        return appEmailAddress;
    }
    public void setAppEmailAddress(String appEmailAddress) {
        this.appEmailAddress = appEmailAddress;
    }
    public String getSystemMessageFR() {
        return systemMessageFR;
    }
    public void setSystemMessageFR(String systemMessageFR) {
        this.systemMessageFR = systemMessageFR;
    }
    public String getSystemMessageEN() {
        return systemMessageEN;
    }
    public void setSystemMessageEN(String systemMessageEN) {
        this.systemMessageEN = systemMessageEN;
    }
    public void setStatsDB(String statsDB) {
        this.statsDB = statsDB;
    }
    public String getStatsDB() {
        return statsDB;
    }
    public void setDefaultLang(String defaultLang) {
        this.defaultLang = defaultLang;
    }
    public String getDefaultProv() {
        return defaultProv;
    }
    public void setDefaultProv(String defaultPRov) {
        this.defaultProv = defaultPRov;
    }
    public void setShowProv(String showProv) {
        this.showProv = showProv;
    }
    public String getShowProv() {
        return showProv;
    }
    public String getDefaultLang() {
        return defaultLang;
    }
    public String getMessageIntro() {
        return messageIntro;
    }
    public void setMessageIntro(String messageIntro) {
        this.messageIntro = messageIntro;
    }
    public String getSenderEmail() {
        return senderEmail;
    }
    public void setSenderEmail(String senderEmail) {
        this.senderEmail = senderEmail;
    }
    public String getSenderName() {
        return senderName;
    }
    public void setSenderName(String senderName) {
        this.senderName = senderName;
    }
    public String getAppURL() {
        return appURL;
    }
    public void setAppURL(String appURL) {
        this.appURL = appURL;
    }
    public String getEmailText() {
        return emailText;
    }
    public void setEmailText(String emailText) {
        this.emailText = emailText;
    }
    public String getClickLinkText() {
        return clickLinkText;
    }
    public void setClickLinkText(String clickLinkText) {
        this.clickLinkText = clickLinkText;
    }
    public String getEmailFooter() {
        return emailFooter;
    }
    public void setEmailFooter(String emailFooter) {
        this.emailFooter = emailFooter;
    }
    //allowRatingModification
    public String getAllowRatingModification() {
        return allowRatingModification;
    }
    public void setAllowRatingModification(String allowRatingModification) {
        this.allowRatingModification = allowRatingModification;
    }
}

变量解析器遍历所有隐式变量(例如session, database)以及范围变量(例如applicationScope.myVar)。您的bean也可以通过变量解析器从SSJS访问。

所以你可以用:

ExtLibUtil.resolveVariable(FacesContext.getCurrentInstance(), "myAppScopeBean");

这不是对你问题的直接回答…

另一种方法是在applicationScope中设置您想要的值,然后从bean以这种方式访问它。直接访问整个bean是一个不同的答案。

你使用这段代码来获取你的applicationScope的句柄。

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
Map<String, Object> applicationScope = externalContext.getApplicationMap();

然后你有这样的代码来使用它:

String agentLogDB = (String) applicationScope.get("LOGDB");

相关内容

  • 没有找到相关文章

最新更新