我想通过 faces-config 文件在我的 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 类上,我有一个属性:
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-config 设置的,还是我这样做的方式错误?
最佳答案
根据我在 XPage 中使用 Java 的进展,我得出的结论是延迟加载更好。这也是相对于 SSJS 的一大优势,因为它在 Java 中要容易得多。所以类似:
私有(private)字符串文件路径;
公共(public)无效 getFilePath() {
if (null == 文件路径) {
设置文件路径();
}
返回文件路径;
}
公共(public)无效setFilePath(){
文件路径 = getFilePathVariableInSomeWay();
}
这意味着您只在使用设置代码时调用一次设置代码,而不是在实例化对象时调用。
它还避免调用性能较差的 SSJS。这也意味着您可以调试设置代码。这还意味着您正在使用特定于语言的编辑器以及相关的编译验证来生成设置代码。 XML 无法验证 SSJS。 Java 编辑器可以确保您没有任何编译时错误。
我不确定使用 faces-config 来计算托管 bean 属性的好处,说实话我从未使用过它们。但我可以看到在 Java 类本身中使用方法的一些强大优势,无论是在构造函数还是 getter 中。
关于java - 如何通过托管 Bean 在类中设置属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51782256/