代码之家  ›  专栏  ›  技术社区  ›  Malin

如何通过托管Bean在类中设置属性?

  •  0
  • Malin  · 技术社区  · 6 年前

    我想通过faces配置文件在java类中设置一个属性:

    <managed-bean>
        <managed-bean-name>utilsBean</managed-bean-name>
        <managed-bean-class>org.acme.bank.app.UtilsBean</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
        <managed-property>
          <property-name>filePath</property-name>
          <value>#{javascript:@ReplaceSubstring(@LeftBack(database.getFilePath(),"\\"),"\\","/")+"/"}</value>
        </managed-property>
      </managed-bean>
    

    在我的UtilsBean类上,我有一个as属性:

    public String filePath;
    

    public String getFilePath() {
            return filePath;
        }
    
        public void setFilePath(String filePath) {
            this.filePath = filePath;
        }
    

    但是当我在构造函数中输出值时,我得到一个空值。

    public UtilsBean() throws Exception {
            super();        
            Database database = ExtLibUtil.getCurrentDatabase();
            System.out.println("database.getFilePath() = " + database.getFilePath());//returns filepath of current nsf
            System.out.println("this filepath = " + this.filePath);//returns null
    
    
            try {
            ...
        }
    }
    

    在我看来,该属性不是通过faces配置设置的,还是我做得不对?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Paul Stephen Withers    6 年前

    通过在XPages中学习Java,我得出结论,延迟加载更好。与SSJS相比,这也是一个很大的优势,因为在Java中它更容易实现。比如:

    private String filePath; public void getFilePath() { if (null == filePath) { setFilePath(); } return filePath; } public void setFilePath() { filePath = getFilePathVariableInSomeWay(); }

    这意味着只在使用设置代码时调用一次,而不是在实例化对象时。

    它还避免了调用性能较差的SSJS。这还意味着您可以调试设置代码。这还意味着您正在使用特定于语言的编辑器以及相关的编译验证来生成设置代码。XML无法验证SSJS。Java编辑器可以确保没有任何编译时错误。

    我不确定使用faces配置计算托管bean属性的好处,说实话,我从来没有使用过它们。但我可以看到在Java类本身中使用方法的一些强大优势,无论是在构造函数中还是在getter中。