【问题标题】:Show warning when EL not found未找到 EL 时显示警告
【发布时间】:2015-09-19 14:15:36
【问题描述】:

我正在创建一个 JSF 应用程序,如果我在 EL-Expression 中出现拼写错误,我希望得到某种警告(最好是控制台)。

示例: 在我的页面上,我想显示一些本地化的文本。 faces-config.xml 中的 locale-config 已正确设置并与 var 'msgs' 一起使用。

我在我的页面上使用了这个代码:

<h:outputText value="#{msg.title_edit_customer}"/>

当我在浏览器中检查我的页面时,没有显示任何内容。 我花了一段时间才意识到我打错了 - #{msgs.... 它按预期工作。

我可以激活某种调试输出,这样我就可以直接看到某处存在无效的 EL 吗?

我的设置:Eclipse 4.4.2、Tomcat 8、MyFaces 2.2.8

【问题讨论】:

标签: eclipse jsf el jsf-2.2


【解决方案1】:

感谢@SJuan76,我能弄明白:

  1. 创建自己的javax.el.ELResolver,所有方法都可以返回null/false。
  2. 打开类org.apache.myfaces.el.unified.resolver.ScopedAttributeResolver的源并复制facesContext(ELContext)findScopedMap(FacesContext, Object)方法(因为ScopedAttributeResolver是最终的,我们不能扩展它)。
  3. 编辑 getValue 方法:

    @Override
    public Object getValue(ELContext context, Object base, Object property) {
        if(!context.isPropertyResolved()){
          //Douple Check false-positives
          boolean foundInScope = false;
          final Map<String, Object> scopedMap = findScopedMap(
                facesContext(context), property);
          if (scopedMap != null) {
            Object object = scopedMap.get(property);
            if (object != null) {
                foundInScope = true;
            }
          }
          if (!foundInScope) {
            log.warn(String.format("EL-Property %s couldn't be resolved",
                    property));
        }
    }
        return null;
    }
    
  4. 编辑 faces-config.xml 以注册您的解析器:

    <application> <el-resolver>com.myPackage.DebugELResolver</el-resolver> </application>

  5. 由于有许多解析器,并且每个解析器都做一点解析,所以我们的新解析器应该排在最后。将以下内容添加到web.xml

    <context-param> <param-name>org.apache.myfaces.EL_RESOLVER_COMPARATOR</param-name> <param-value>org.apache.myfaces.el.unified.CustomLastELResolverComparator</param-value> </context-param>

  6. 现在每次无法解析表达式时,您都会得到一些日志输出:

03.07.2015 07:34:21 com.myPackage.DebugELResolver [http-nio-8080-exec-2] WARN EL-Property msgx couldn't be resolved

我不知道如何获得实际的 EL 表达式(例如 #{msgx.title_edit_customer}

【讨论】:

    猜你喜欢
    • 2012-03-11
    • 2020-07-05
    • 2012-01-24
    • 2022-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    相关资源
    最近更新 更多