使用EL时的日志/反馈消息

时间:2014-02-27 11:43:50

标签: jsf

我遇到的问题是,在尝试获取不存在的参数/上下文值时,我在日志或页面中没有收到任何反馈消息。

有没有办法启用详细的EL错误记录,我已在web.xml中添加了一些行:

<context-param>
    <param-name>javax.faces.FACELETS_DEVELOPMENT</param-name>
    <param-value>true</param-value>
</context-param>

但这没有用。

1 个答案:

答案 0 :(得分:1)

我希望您观察到的只是EL和VDL所需的行为。

也就是说,您可以通过引入自己的解析器代码来记录一些有限但可能有用的信息。

VariableResolver解决了顶级工件:

package logs;

import java.util.logging.*;
import javax.faces.context.FacesContext;
import javax.faces.el.*;

@SuppressWarnings("deprecated")
public class LogVariableResolver extends VariableResolver {

  private static final Logger LOG = 
                         Logger.getLogger(LogVariableResolver.class.getName());
  private final VariableResolver decorated;

  public LogVariableResolver(VariableResolver decorated) {
    this.decorated = decorated;
  }

  @Override
  public Object resolveVariable(FacesContext context, String name)
                                          throws EvaluationException {
    Object resolved = decorated.resolveVariable(context, name);
    if (resolved == null && LOG.isLoggable(Level.WARNING)) {
      LOG.warning("Unresolved: " + name);
    } else {
      LOG.info("Resolved: " + name + " " + resolved);
    }
    return resolved;
  }
}

PropertyResolver解析对象的属性:

package logs;

import java.util.logging.*;
import javax.faces.el.*;

@SuppressWarnings("deprecated")
public class LogPropertyResolver extends PropertyResolver {
  private static final Logger LOG =
                          Logger.getLogger(LogPropertyResolver.class.getName());
  private final PropertyResolver decorated;

  public LogPropertyResolver(PropertyResolver pr) {
    this.decorated = pr;
  }

  @Override
  public Object getValue(Object base, Object property)
                         throws EvaluationException, PropertyNotFoundException {
    return log(decorated.getValue(base, property), base, property);
  }

  @Override
  public Object getValue(Object base, int index)
                         throws EvaluationException, PropertyNotFoundException {
    return log(decorated.getValue(base, index), base, index);
  }

  private Object log(Object result, Object base, Object prop) {
    if (result == null && LOG.isLoggable(Level.WARNING)) {
      LOG.warning("Result null for property " + prop + " on " + base);
    }
    return result;
  }

// implement remaining methods to call delegate & return values

解析器可以在 faces-config.xml

中注册
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <application>
      <variable-resolver>logs.LogVariableResolver</variable-resolver>
      <property-resolver>logs.LogPropertyResolver</property-resolver>
    </application>
 <!-- etc -->

这些类型已弃用,但为此配置ELContext比使用它们更容易。