如何在XPage中的自定义控件中处理设计错误?

时间:2013-01-18 23:18:34

标签: error-handling el xpages

在自定义控件中,有一个重复控件引用文件夹(而不是视图)中的列。文件夹的默认设计可以与自定义控件一起及时更改。因此,可能会发生自定义控件的代码比文件夹的设计更新,因此设计不匹配并且XPage错误输出。

我特别想要的是自定义控件处理与缺少视图/文件夹列或类似设计错误相关的错误。该错误将在某处报告,通知用户他/她可以激活可以修复该情况的内容。

我知道如何捕获JavaScript错误,遗憾的是所有列值都在表达式语言中。当然,我可以重新编码,但我想知道是否有更好的方法。

简而言之:我怎样才能捕获表达式语言错误?

2 个答案:

答案 0 :(得分:3)

您可以通过添加自己的EL和自己的VariableResolver来捕获PropertyResolver语言错误。为此,您必须创建两个Java类:

  1. 变量解析器

    package ch.hasselba.xpages.demo;
    
    import javax.faces.application.FacesMessage;
    import javax.faces.context.FacesContext;
    import javax.faces.el.EvaluationException;
    import javax.faces.el.VariableResolver;
    
    public class ELErrVariableResolver extends VariableResolver {
    
      private final VariableResolver delegate;
    
      public ELErrVariableResolver(VariableResolver resolver) {
        delegate = resolver;
      }
    
      @Override
      public Object resolveVariable(FacesContext context, String name) throws EvaluationException {
          Object variable = null;
          try{
              variable = delegate.resolveVariable(context, name);
          }catch( EvaluationException ee ){
              addResolveErrMessage( context, name );
          }
    
         return variable;
      }
    
      public void addResolveErrMessage( FacesContext context , String name ){
          FacesMessage msg = new FacesMessage();
          msg.setSummary( "BAD EL! Variable '" + name + "' not found." );
          msg.setSeverity( FacesMessage.SEVERITY_FATAL );
          context.addMessage("BAD EL!", msg);
      }
    }
    
  2. 属性解析器

    package ch.hasselba.xpages.demo;
    
    import javax.faces.application.FacesMessage;
    import javax.faces.context.FacesContext;
    import javax.faces.el.EvaluationException;
    import javax.faces.el.PropertyNotFoundException;
    import javax.faces.el.PropertyResolver;
    
    public class ELErrPropertyResolver extends PropertyResolver{
    
        private final PropertyResolver delegate;
    
          public ELErrPropertyResolver(PropertyResolver resolver) {
            delegate = resolver;
          }
    
    
        @Override
        public Class getType(Object paramObject1, Object paramObject2)
                throws EvaluationException, PropertyNotFoundException {
            Class c = null;
            try{
                c = delegate.getType(paramObject1, paramObject2);
            }catch(Exception e){
                addResolveErrMessage( FacesContext.getCurrentInstance(), paramObject1.toString() + "." + paramObject2.toString() );
            }
            return c;
        }
    
        @Override
        public Class getType(Object paramObject, int paramInt)
                throws EvaluationException, PropertyNotFoundException {
            Class c = null;
            try{
                c = delegate.getType(paramObject, paramInt);
            }catch(Exception e){
                addResolveErrMessage( FacesContext.getCurrentInstance(), paramObject.toString() + "." + paramInt );
            }
            return c;
        }
    
        @Override
        public Object getValue(Object paramObject1, Object paramObject2)
                throws EvaluationException, PropertyNotFoundException {
            Object c = null;
    
            try{
                c = delegate.getValue(paramObject1, paramObject2);
            }catch(Exception e){
                addResolveErrMessage( FacesContext.getCurrentInstance(), paramObject1.toString() + "."  + paramObject2.toString() );
            }
            return c;
        }
    
        @Override
        public Object getValue(Object paramObject, int paramInt)
                throws EvaluationException, PropertyNotFoundException {
            Object c = null;
            try{
                c = delegate.getValue(paramObject, paramInt);
            }catch(Exception e){
                addResolveErrMessage( FacesContext.getCurrentInstance(), paramObject.toString() + "." + paramInt );
            }
            return c;
        }
    
        @Override
        public boolean isReadOnly(Object paramObject1, Object paramObject2)
                throws EvaluationException, PropertyNotFoundException {
            boolean c = false;
            try{
                c = delegate.isReadOnly(paramObject1, paramObject2);
            }catch(Exception e){
                addResolveErrMessage( FacesContext.getCurrentInstance(), paramObject1.toString() + "." + paramObject2.toString() );
            }
            return c;
        }
    
        @Override
        public boolean isReadOnly(Object paramObject, int paramInt)
                throws EvaluationException, PropertyNotFoundException {
            boolean c = false;
            try{
                c = delegate.isReadOnly(paramObject, paramInt);
            }catch(Exception e){
                addResolveErrMessage( FacesContext.getCurrentInstance(), paramObject.toString() + "." + paramInt );
            }
            return c;
        }
    
        @Override
        public void setValue(Object paramObject1, Object paramObject2,
                Object paramObject3) throws EvaluationException,
                PropertyNotFoundException {
            try{
                delegate.setValue(paramObject1, paramObject2, paramObject3);
            }catch(Exception e){
                addResolveErrMessage( FacesContext.getCurrentInstance(), paramObject1.toString() + "." + paramObject2.toString() );
            }
    
        }
    
        @Override
        public void setValue(Object paramObject1, int paramInt, Object paramObject2)
                throws EvaluationException, PropertyNotFoundException {
    
            try{
                delegate.setValue(paramObject1, paramInt, paramObject2);
            }catch(Exception e){
                addResolveErrMessage( FacesContext.getCurrentInstance(), paramObject1.toString() + "." + paramInt );
            }
    
        }
    
        public void addResolveErrMessage( FacesContext context , String name ){
              FacesMessage msg = new FacesMessage();
              msg.setSummary( "BAD EL! Property '" + name + "' not found." );
              msg.setSeverity( FacesMessage.SEVERITY_FATAL );
              context.addMessage("BAD EL!", msg);
          }
    }
    
  3. 将新的解析器添加到faces-config.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <faces-config>
       <application>
            <variable-resolver>ch.hasselba.xpages.demo.ELErrVariableResolver
            </variable-resolver>
            <property-resolver>ch.hasselba.xpages.demo.ELErrPropertyResolver
            </property-resolver>
        </application>
    </faces-config>
    
  4. 在您的CC上添加xp:messages组件以显示您的消息(或更改类中的错误例程以添加您想要的任何内容。

答案 1 :(得分:2)

在自定义控件中,您可以检查要使用的文件夹/视图是否正确。 Aka有正确的设计。这可以在beforepageload事件中完成。当设计检查报告错误时,应将其写入日志文件,并向用户显示“nice”消息。

可以使用openntf上的各种日志记录项目来完成错误记录,例如xlogger 当您的代码报告错误时,您可以将名为“displayRepeat”的范围值设置为false。应根据此displayRepeat值呈现重复控件(和其他控件)。

向用户显示不错的错误消息。将错误控件放在控件的顶部并添加以下代码:

facesContext.addMessage( null, 
new javax.faces.application.FacesMessage( "your error message" ) );