【问题标题】:How to enforce the type of a cc:attribute in a JSF composite component?如何在 JSF 复合组件中强制执行 cc:attribute 的类型?
【发布时间】:2018-04-28 18:53:56
【问题描述】:

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

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

JSF2: limiting cc:attribute to a given object type within a List 解释了检查列表的泛型类型的问题,但解决方案(用attributeValue.class.name 检查类型,这似乎很hacky)对于这种情况不一定是必要的,我想检查/断言原始类型。

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

【问题讨论】:

  • 您是否实施了一些测试以确保不会引发异常?哪个代码?您使用了哪些 JSF impls 进行测试?分享吧!
  • 您是否尝试过为 cc 提供支持并在那里进行检查? stackoverflow.com/questions/6602953/…

标签: jsf jsf-2.2


【解决方案1】:

显然 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>

支持 bean:

@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>

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

【讨论】:

    猜你喜欢
    • 2023-01-25
    • 2011-10-14
    • 2020-03-18
    • 1970-01-01
    • 2020-05-12
    • 2019-04-24
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    相关资源
    最近更新 更多