使用 XPage 中 bean 中的 DataObject 将文档项值加载到地图中

问题描述 投票:0回答:2

我正在尝试创建一个使用 DataObject 从文档中收集值的 bean。

代码的灵感来自于这篇post

package com.consili;
import lotus.domino.*;

import java.util.*;
import com.ibm.xsp.extlib.util.ExtLibUtil;
import java.io.*;
import com.ibm.xsp.model.DataObject;

public class ConfigBean implements DataObject, Serializable { 

    private static final long serialVersionUID = 1L;
    private static HashMap<Object,Object> config = null; 

    public ConfigBean() throws NotesException, IOException, ClassNotFoundException{ 
        config = new java.util.HashMap<Object,Object>(); 
        readFromDocument(); 
    }

    public void readFromDocument() throws NotesException, IOException, ClassNotFoundException{ 
        Database db = ExtLibUtil.getCurrentSession().getCurrentDatabase(); 
        View view = db.getView("Settings"); 
        Document doc = view.getFirstDocument();
        if(doc==null) throw new IllegalArgumentException("doc cannot be null.");
        
        if(doc!=null){
            Iterator<Item> items = doc.getItems().iterator(); 
            while(items.hasNext()){
                Item item =  items.next(); 
                setValue(item.getName(), item.getValueCustomData()); 
            }                 
            doc.recycle(); 
            view.recycle(); 
            db.recycle(); 
        }
    }

    public void saveToDocument(){ 
        //.. write the HashMap back to your document. 
    }

    @Override
    public Object getValue(Object key) {
        if (key == null) {
            throw new IllegalArgumentException("Key cannot be null.");
        } 
        return config.get(key); 
    }

    @Override
    public boolean isReadOnly(Object arg0) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void setValue(Object key, Object value) {
        
        if (key == null  || value == null) {
               throw new IllegalArgumentException("Key or value cannot be null.");
        } 
        config.put(key.toString(), value); 
    }

    @Override
    public Class<ConfigBean> getType(Object id) {
          return ConfigBean.class;
    }
 }

面孔配置:

 <managed-bean>
    <managed-bean-name>Config</managed-bean-name>
    <managed-bean-class>com.consili.ConfigBean</managed-bean-class>
    <managed-bean-scope>session</managed-bean-scope>
  </managed-bean>

设置文档中的表单包含两个字段 Title 和 WebUrl 如果我向 xpage 添加一个输入框,它会抛出一个未找到类异常

javax.faces.FacesException:javax.faces.FacesException:无法创建类的实例:'com.consili.ConfigBean'.. null

 <xp:inputText id="inputText1" value="#{Config.Title}"></xp:inputText>

我注意到如果我注释掉这一行

 setValue(item.getName(), item.getValueCustomData()); 

页面可以工作并显示输入框,但无法设置地图值。

可能出了什么问题?

旁注,如果我在网络浏览器中将网页显示为预览而不是服务器,则客户端崩溃并出现以下错误

java xpages javabeans
2个回答
0
投票

我只是快速浏览了你的代码 - 看起来还不错......(或者更确切地说,我也无法发现问题)。

Tim Tripcony 最初描述了相同的功能,但存储库已被删除。然而,我发现 Harald Reisinger 在他的 GitHub 上放了一个副本:-)

也许您可以通过使用该代码来进一步了解?这就是我们使用的代码的最初灵感;-)

/约翰


0
投票

我再次查看了这一点,这行不起作用

 setValue(item.getName(), item.getValueCustomData()); 

检查类型并使用正确的项目类型设置值,如下所示:

if(item.getType() == Item.TEXT){
  setValue(item.getName(), item.getText());
}

if(item.getType() == Item.NUMBERS){
  setValue(item.getName(), item.getValueDouble());
}
© www.soinside.com 2019 - 2024. All rights reserved.