找不到 EL 时显示警告
Show warning when EL not found
我正在创建一个 JSF 应用程序,如果我在我的 EL 表达式中出现拼写错误,我希望得到某种警告(最好是在控制台中)。
示例:
在我的页面上,我想显示一些本地化的文本。 faces-config.xml 中的 locale-config 已正确设置并与 var 'msgs'.
一起使用
我在我的页面上使用了这段代码:
<h:outputText value="#{msg.title_edit_customer}"/>
当我在浏览器中查看我的页面时,没有显示任何内容。
我花了一段时间才意识到我打错了字 - #{msgs....
它按预期工作了。
我可以激活某种调试输出,这样我就可以直接看到某个地方有无效的 EL 吗?
我的设置:Eclipse 4.4.2,Tomcat8,MyFaces 2.2.8
多亏了@SJuan76,我才弄明白:
- 创建您自己的
javax.el.ELResolver
,所有方法都可以 return null/false。
- 打开 Class
org.apache.myfaces.el.unified.resolver.ScopedAttributeResolver
的源代码并复制方法 facesContext(ELContext)
和 findScopedMap(FacesContext, Object)
(因为 ScopedAttributeResolver
是最终的,我们不能扩展它)。
编辑 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;
}
编辑 faces-config.xml
以注册您的解析器:
<application>
<el-resolver>com.myPackage.DebugELResolver</el-resolver>
</application>
由于有很多 Resolver,每个 Resolver 都会做一点 Resolving,所以我们的新 Resolver 应该排在最后。将以下内容添加到 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>
- 现在,每次无法解析表达式时,您都会得到一些日志输出:
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}
。
我正在创建一个 JSF 应用程序,如果我在我的 EL 表达式中出现拼写错误,我希望得到某种警告(最好是在控制台中)。
示例: 在我的页面上,我想显示一些本地化的文本。 faces-config.xml 中的 locale-config 已正确设置并与 var 'msgs'.
一起使用我在我的页面上使用了这段代码:
<h:outputText value="#{msg.title_edit_customer}"/>
当我在浏览器中查看我的页面时,没有显示任何内容。
我花了一段时间才意识到我打错了字 - #{msgs....
它按预期工作了。
我可以激活某种调试输出,这样我就可以直接看到某个地方有无效的 EL 吗?
我的设置:Eclipse 4.4.2,Tomcat8,MyFaces 2.2.8
多亏了@SJuan76,我才弄明白:
- 创建您自己的
javax.el.ELResolver
,所有方法都可以 return null/false。 - 打开 Class
org.apache.myfaces.el.unified.resolver.ScopedAttributeResolver
的源代码并复制方法facesContext(ELContext)
和findScopedMap(FacesContext, Object)
(因为ScopedAttributeResolver
是最终的,我们不能扩展它)。 编辑 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; }
编辑
faces-config.xml
以注册您的解析器:<application> <el-resolver>com.myPackage.DebugELResolver</el-resolver> </application>
由于有很多 Resolver,每个 Resolver 都会做一点 Resolving,所以我们的新 Resolver 应该排在最后。将以下内容添加到
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>
- 现在,每次无法解析表达式时,您都会得到一些日志输出:
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}
。