在上一篇文章Creating a ToolTip Managed bean
中我能够创建一个manged bean来收集和显示只有一个查找的工具提示文本,并将它们存储在Application Scope变量中。这很有效。
我正处于JAVA学习曲线的相当陡峭的部分,所以请原谅我。
我有另一个托管bean要求来创建HashMap应用程序范围,但这次它需要是String,Object类型。该应用程序是我有一个“主”数据库,其中包含大多数代码,自定义控件和XPage。此主数据库将指向一个或多个应用程序数据库,这些数据库将存储特定于相关应用程序的Notes文档。因此,我在Master中创建了一系列应用程序文档,这些文档指定了特定于应用程序的应用程序,帮助和规则数据库的RepID以及有关应用程序的许多其他信息。这应该允许我重用相同的自定义控件,通过传递应用程序名称来打开特定的数据库。例如,Master Design DB可能指向“采购”,“客户投诉”,“旅行请求”等。 下面的代码用于加载和存储HashMap,但是我在检索数据时遇到了问题。 编译器在public Object get(String key)方法中显示两个错误,另一个在mapValue = this.internalMap.get(key);在getAppRepID方法中我认为它主要是语法但不确定。我已经在代码中对错误进行了评论。
/**
*This Class makes the variables that define an application within Workflo!Approval
*available as an ApplicationScope variable.
*/
package ca.wfsystems.wfsAppUtils;
import lotus.domino.Base;
import lotus.domino.Session;
import lotus.domino.Database;
import lotus.domino.View;
import lotus.domino.NotesException;
import lotus.domino.ViewColumn;
import lotus.domino.ViewEntry;
import lotus.domino.ViewEntryCollection;
import lotus.domino.Name;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import com.ibm.xsp.extlib.util.ExtLibUtil;
/**
* @author Bill Fox Workflo Systems WFSystems.ca
* July 2014
* This class is provided as part of the Workflo!Approval Product
* and can be reused within this application.
* If copied to a different application please retain this attribution.
*
*/
public abstract class ApplicationUtils implements Serializable, Map<String, Object> {
private static final long serialVersionUID = 1L;
private Session s;
private Name serverName;
private String repID;
private String thisKey;
private ViewEntryCollection formVECol;
private Vector formNames;
private Database thisDB;
private Database appDB;
private View appView;
private View formView;
private ViewEntry formVE;
private ViewEntry tFormVE;
private ViewEntry ve;
private ViewEntry tVE;
private ViewEntryCollection veCol;
private final Map<String, Object> internalMap = new HashMap<String, Object>();
public ApplicationUtils() {
this.populateMap(internalMap);
}
private void populateMap(Map<String, Object> theMap) {
try{
s = ExtLibUtil.getCurrentSession();
//serverName = s.createName(s.getServerName());
thisDB = s.getCurrentDatabase();
appView = thisDB.getView("vwWFSApplications");
veCol = appView.getAllEntries();
ve = veCol.getFirstEntry();
ViewEntry tVE = null;
while (ve != null) {
rtnValue mapValue = new rtnValue();
tVE = veCol.getNextEntry(ve);
Vector colVal = ve.getColumnValues();
thisKey = colVal.get(0).toString();
mapValue.setRepID(colVal.get(2).toString());
// ...... load the rest of the values .......
theMap.put(thisKey, mapValue);
recycleObjects(ve);
ve = tVE;
}
}catch(NotesException e){
System.out.println(e.toString());
}finally{
recycleObjects(ve, veCol, appView, tVE);
}
}
public class rtnValue{
private String RepID;
private String HelpRepID;
private String RuleRepID;
private Vector FormNames;
public String getRepID() {
return RepID;
}
public void setRepID(String repID) {
RepID = repID;
}
public String getHelpRepID() {
return HelpRepID;
}
public void setHelpRepID(String helpRepID) {
HelpRepID = helpRepID;
}
public String getRuleRepID() {
return RuleRepID;
}
public void setRuleRepID(String ruleRepID) {
RuleRepID = ruleRepID;
}
public Vector getFormNames() {
return FormNames;
}
public void setFormNames(Vector formNames) {
FormNames = formNames;
}
}
public void clear() {
this.internalMap.clear();
this.populateMap(this.internalMap);
}
public boolean containsKey(Object key) {
return this.internalMap.containsKey(key);
}
public boolean containsValue(Object value) {
return this.internalMap.containsValue(value);
}
public Set<java.util.Map.Entry<String, Object>> entrySet() {
return this.internalMap.entrySet();
}
public Object get(String key) {
//error on Object get Method must return a result of type Object
try {
if (this.internalMap.containsKey(key)) {
return this.internalMap.get(key);
}
} catch (Exception e) {
System.out.println(e.toString());
rtnValue newMap = new rtnValue();
return newMap;
}
}
public boolean isEmpty() {
return this.internalMap.isEmpty();
}
public Set<String> keySet() {
return this.internalMap.keySet();
}
public Object put(String key, Object value) {
return this.internalMap.put(key, value);
}
public Object remove(Object key) {
return this.internalMap.remove(key);
}
public int size() {
return this.internalMap.size();
}
public Collection<Object> values() {
return this.internalMap.values();
}
public void putAll(Map<? extends String, ? extends Object> m) {
this.internalMap.putAll(m);
}
public String getAppRepID(String key){
/*get the Replica Id of the application database
* not sure this is the correct way to call this
*/
rtnValue mapValue = new rtnValue();
mapValue = this.internalMap.get(key);
//error on line above Type Mismatch: can not convert Object to ApplicationUtils.rtnValue
String repID = mapValue.getRepID();
}
public static void recycleObjects(Object... args) {
for (Object o : args) {
if (o != null) {
if (o instanceof Base) {
try {
((Base) o).recycle();
} catch (Throwable t) {
// who cares?
}
}
}
}
}
}
答案 0 :(得分:0)
对于get()方法,我处理这种情况的方法是创建一个正确数据类型的变量为null,在我的try / catch中设置变量,最后返回变量。所以:
Object retVal = null;
try....
return retVal;
对于另一个错误,如果右键单击错误标记,它可能会让您有机会将变量强制转换为rtnValue,所以:
mapValue = (rtnValue) this.internalMap.get(key)
如果你还没有得到它,Head First Java是一本有用的书,可以让我了解一些Java概念。同样值得从OpenNTF下载Domino Designer的FindBugs插件。它将识别错误和不良做法。只需忽略&#34; local&#34;中的错误。包!
答案 1 :(得分:0)
问题是存在一个不返回任何内容的执行路径
public Object get(String key) {
//error on Object get Method must return a result of type Object
try {
if (this.internalMap.containsKey(key)) { // false
return this.internalMap.get(key);
}
} catch (Exception e) {
System.out.println(e.toString());
rtnValue newMap = new rtnValue();
return newMap;
}
}
如果key
中没有internalMap
,则不会抛出任何内容,那么该方法不会返回任何内容。
要解决此问题,请在结尾处返回newMap
。
public Object get(String key) {
//error on Object get Method must return a result of type Object
try {
if (this.internalMap.containsKey(key)) {
return this.internalMap.get(key);
}
} catch (Exception e) {
System.out.println(e.toString());
}
rtnValue newMap = new rtnValue();
return newMap;
}
您可以内联返回以保存分配(这是编译器将要执行的操作)。我没有这样做只是为了在示例中说清楚。
但是getAppRepID
方法中仍然存在编译器错误。您期待的是rtnValue
,但您发回了Object
。你必须在那里施展。
处理此问题的适当方法是,如果您知道所有值都是给定类型,请使用正确的类型创建地图。
答案 2 :(得分:0)
您是否尝试将internalMap设为rtnValue实例的地图(如此)?