Java 添加到托管Bean最佳实践
这是对()的扩展。我编写了一个类AppProperties,它定义了类中的各种项:Java 添加到托管Bean最佳实践,java,xpages,Java,Xpages,这是对()的扩展。我编写了一个类AppProperties,它定义了类中的各种项: public class AppProperties implements Serializable { private static final long serialVersionUID = 1L; //contents of AppProperties Object private String appRepID; private String he
public class AppProperties implements Serializable {
private static final long serialVersionUID = 1L;
//contents of AppProperties Object
private String appRepID;
private String helpRepID;
private String ruleRepID;
private String filePath;
private Vector formNames;
//end content
private Session s;
private String serverName;
public String getAppRepID() {
return appRepID;
}
public void setAppRepID(String appRepID) {
this.appRepID = appRepID;
}
//rest if getters and setters
}
在我的bean中,我有以下内容:
import ca.wfsystems.core.AppProperties;
private final Map<String, AppProperties> internalMap = new HashMap<String, AppProperties>();
public ApplicationMap() {
this.buildMap(internalMap);
}
private void buildMap(Map<String, AppProperties> theMap) {
try{
AppProperties ap = null;
Session s = ExtLibUtil.getCurrentSession();
vwApps = s.getCurrentDatabase().getView("vwWFSApplications");
veCol = vwApps.getAllEntries();
ve = veCol.getFirstEntry();
tVE = null;
while (ve != null){
Vector colVal = ve.getColumnValues();
String tAppRepID = colVal.get(2).toString();
ap.setAppRepID(colVal.get(2).toString());
ap.setHelpRepID(colVal.get(3).toString());
ap.setRuleRepID(colVal.get(4).toString());
theMap.put(colVal.get(0).toString(), ap);
}
}catch(Exception e){
System.out.println(e.toString());
}finally{
Utils.recycleObjects(s,vwApps);
}
}
导入ca.wfsystems.core.AppProperties;
私有最终映射internalMap=新HashMap();
公共应用程序映射(){
这个.buildMap(internalMap);
}
私有void构建映射(映射主题映射){
试一试{
AppProperties ap=null;
会话s=ExtLibUtil.getCurrentSession();
vwApps=s.getCurrentDatabase().getView(“vwWFSApplications”);
veCol=vwApps.getAllEntries();
ve=veCol.getFirstEntry();
tVE=null;
while(ve!=null){
Vector colVal=ve.getColumnValues();
字符串tAppRepID=colVal.get(2.toString();
ap.setappred(colVal.get(2.toString());
ap.setHelpRepID(colVal.get(3.toString());
ap.setRuleRepID(colVal.get(4.toString());
theMap.put(colVal.get(0.toString(),ap);
}
}捕获(例外e){
System.out.println(例如toString());
}最后{
Utils.回收对象(s、vwapp);
}
}
除了ap.setAppRedId(colVal(2).toString())之外,一切似乎都正常。存在一个编译器错误,“Null指针访问变量ap此时只能为Null”。setAppRedId和setHelpRepID的代码相同,并且setHelpRepID或setRuleRepID都没有编译器错误。我不确定问题是否出在设置AppProperties ap=null中。我尝试创建AppProperties ap=new AppProperties,但它不喜欢这样。我想我真的很快就要成功了,但是
感谢所有在我爬上爪哇斜坡时对我非常耐心的人 这肯定是“AppProperties ap=null”行。当您说您尝试了“AppProperties ap=new AppProperties”时,您是否在末尾包含“()”(即“AppProperties ap=new AppProperties()
”)?看起来您的AppProperties bean具有默认的无参数构造函数,因此应该可以工作。具体地说,根据您的代码猜测,我希望您将该行移动到while循环打开之后
您还有一个无限循环在等待:您从未在while循环中将条目设置为下一个条目。如果您不使用OpenNTF Domino API,我建议您使用以下习惯用法:
ViewEntry entry = veCol.getFirstEntry();
while(entry != null) {
Vector<?> colVal = ve.getColumnValues();
...
entry.recycle(colVal);
ViewEntry tempEntry = entry;
entry = veCol.getNextEntry();
tempEntry.recycle();
}
ViewEntry=veCol.getFirstEntry();
while(条目!=null){
Vector colVal=ve.getColumnValues();
...
回收(colVal);
ViewEntry tempEntry=entry;
entry=veCol.getNextEntry();
tempEntry.recycle();
}
编译器错误是正确的,您的变量ap只能在该点为空。
请按照
AppProperties ap = null;
到
如果ap没有初始化为对象,它仍然是空的
您也会在setHelpRepID或setRuleRepID上看到编译器错误,但它不会麻烦您,因为第一条语句已经有问题了。您可以通过注释掉setappepid行来尝试这一点,您应该在下一行看到相同的错误
在AppProperties类中创建公共构造函数
public AppProperties() {};
然后尝试改变
AppProperties ap = null;
到
感谢Cameron,我需要在AppProperties类中使用空构造函数,以便创建AppProperties的新实例。现在它编译正确,所以现在我可以开始测试它了。再次感谢
AppProperties ap = null;
AppProperties ap = new AppProperties();