1

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

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

我知道如何捕获 JavaScript 错误,不幸的是所有列值都使用表达式语言。当然,我可以重新编码它们,但我想知道是否有更好的方法。

简而言之:如何捕获表达式语言错误?

4

2 回答 2

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组件以显示您的消息(或更改类中的错误例程以添加您想要的任何内容。

于 2013-01-20T15:37:08.693 回答
2

In the custom control you can check if the folder/view you are going to work with is correct. Aka has the correct design. This can be done in the beforepageload event. When the design check reports an error it should be written to a log file and a 'nice' message should be displayed to the user.

The error logging could be done with the various logging projects on openntf like xlogger When your code reports an error you can than set a scoped value called 'displayRepeat' to false. The repeat control ( and other controls ) should be rendered according to this displayRepeat value.

To display the nice error message to the user. Place a errors control on the top of your control and add the following code:

facesContext.addMessage( null, 
new javax.faces.application.FacesMessage( "your error message" ) );
于 2013-01-19T07:21:28.963 回答