首页 > 解决方案 > 如何在 JSF 复合组件中强制执行 cc:attribute 的类型?

问题描述

我似乎没有找到任何明确的参考,该type属性对cc:attributeJSF 2.2 复合组件的影响。我想确保传递的 EL 表达式的类型匹配type,否则抛出异常。Afaik 的规范type应该这样做,因为否则我认为拥有该属性没有任何意义,但我知道 JSF 中有一些(起初)奇怪的转换,例如在nulland之间"",因此可能不会引发异常。

如果没有抛出异常,那么在运行时(或者如果可能的话,在构建时)抛出异常的最优雅的方法是什么?

JSF2:将 cc:attribute 限制为 List 中的给定对象类型解释了检查列表的泛型类型的问题,但是attributeValue.class.name对于这种情况,解决方案(检查看起来很老套的类型)不一定是必要的我想检查/断言原始类型。

我也对 JSF 2.3 的解决方案感兴趣。

标签: jsfjsf-2.2

解决方案


显然 Primefaces 5.0 和 6.2 在以下情况下检查类型:

复合组件:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:cc="http://xmlns.jcp.org/jsf/composite">
    <cc:interface>
        <cc:attribute name="theValue"
                type="java.util.List"
                required="true"/>
    </cc:interface>
    <cc:implementation>
        #{cc.attrs.theValue.add("abc")}
    </cc:implementation>
</html>

支持豆:

@Named
@ViewScoped
public class BackingBean0 implements Serializable {
    private Set<String> property0 = new HashSet<>();

    public Set<String> getProperty0() {
        return property0;
    }

    public void setProperty0(Set<String> property0) {
        this.property0 = property0;
    }
}

index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:typesafety="http://xmlns.jcp.org/jsf/composite/typesafety">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <typesafety:comp theValue="#{backingBean0.property0}"/>
    </h:body>
</html>

如果我再次遇到导致我怀疑是否有支票的情况,我会编辑问题。


推荐阅读