XPages:如何从会话作用域bean访问应用程序作用域bean [英] XPages: How to acces an application scope bean from a session scope bean

查看:94
本文介绍了XPages:如何从会话作用域bean访问应用程序作用域bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要会话范围托管bean中的应用程序范围托管bean中的值.不知道如何完成这项工作.在这里看到了一个帖子: JSF 2.0从另一个Bean访问Application Scope Bean ,因此IM可能需要重新定义我的应用Bean?完全毫无头绪...

I need a value from my application scope managed bean in my session scope managed bean. Not sure how to get this done. Saw a poste here: https://guedebyte.wordpress.com/2012/05/19/accessing-beans-from-java-code-in-xpages-learned-by-reading-the-sourcecode-of-the-extensionlibrary/ But I get a bunch of errors... I also found this: JSF 2.0 Accessing Application Scope bean from another Bean so IM thinking maybe I need to redefine my application bean??? Totally clueless...

我如何做到这一点?

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

Here is the application scope bean's code:

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;
    }
}

推荐答案

VariableResolver会遍历所有隐式变量(例如sessiondatabase)以及范围变量(例如applicationScope.myVar).还可以通过VariableResolver从SSJS访问您的bean.

The VariableResolver goes through all implicit variables (e.g. session, database) as well as scoped variables (e.g. applicationScope.myVar). Your bean is also accessed from SSJS via the VariableResolver.

因此您可以使用:

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

这篇关于XPages:如何从会话作用域bean访问应用程序作用域bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆