具有f:attribute转换错误的JSF Composite:attribute [英] JSF composite:attribute with f:attribute conversion error

查看:176
本文介绍了具有f:attribute转换错误的JSF Composite:attribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现JSF组件,需要有条件地添加一些属性.此问题类似于先前的 JSF:具有f:attribute的p:dataTable导致参数类型不匹配";错误,但错误信息却完全不同,所以我提出了一个新问题.

I'm implementing a JSF component and need to conditionally add some attributes. This question is similar to a previous JSF: p:dataTable with f:attribute results in "argument type mismatch" error, but with a completely different error message, so I raised a new question.

<composite:interface>
    <composite:attribute name="filter" required="false" default="false"
                         type="java.lang.Boolean"/>
    <composite:attribute name="rows" required="false" default="15"
                         type="java.lang.Integer"/>
    ...
</composite:interface>

<composite:implementation>
  <p:dataTable ivar="p" value="#{cc.attrs.dm}">
    <c:if test="#{cc.attrs.filter}">
      <f:attribute name="paginator" value="#{true}"/>
      <f:attribute name="rows" value="#{cc.attrs.rows}"/>
    </c:if>
    ...
  <p:dataTable>
</composite:implementation>

这将导致错误java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer.即使我手动设置了此设置,也会出现错误:

This results in an error java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer. Even if I manually set this, I get errors:

<f:attribute name="rows" value="15"/>    ... argument type mismatch
<f:attribute name="rows" value="#{15}"/> ... java.lang.Long cannot be cast
                                             to java.lang.Integer

如果我直接添加属性,则不会出现异常,并且将显示正确的行数:

If I add the attribute directly, there is no exception and the correct number of rows is diplayed:

<p:dataTable var="p" value="#{cc.attrs.dm}" rows="#{cc.attrs.rows}">

推荐答案

这确实是一个不幸的极端情况,在EL和复合组件属性中都有数字.对此没有解决方案.当在<f:attribute>中使用类型信息时,该类型信息在#{cc.attrs}中不可用,因此被视为String. #{15}也不能在EL中表示为整数,当类型信息不存在时,所有数字始终隐式地视为Long.可以通过使用标记文件而不是复合组件来防止ClassCastException.

This is indeed an unfortunate corner case with numbers in EL and composite component attributes. There's no solution for this. The type information is not available in #{cc.attrs} when used in <f:attribute> and thus treated as String. The #{15} cannot be represented as an integer in EL either, all numbers are always implicitly treated as Long when the type information is absent. The ClassCastException can be prevented by using a tag file instead of a composite component.

您最好的选择是检查实际的rows属性本身.

Your best bet is doing the check in the actual rows attribute itself.

<p:dataTable ... rows="#{cc.attrs.filter ? cc.attrs.rows : null}">

这篇关于具有f:attribute转换错误的JSF Composite:attribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆