jsf - 如何在 JSF 复合组件中强制执行 cc:attribute 的类型?
问题描述
我似乎没有找到任何明确的参考,该type
属性对cc:attribute
JSF 2.2 复合组件的影响。我想确保传递的 EL 表达式的类型匹配type
,否则抛出异常。Afaik 的规范type
应该这样做,因为否则我认为拥有该属性没有任何意义,但我知道 JSF 中有一些(起初)奇怪的转换,例如在null
and之间""
,因此可能不会引发异常。
如果没有抛出异常,那么在运行时(或者如果可能的话,在构建时)抛出异常的最优雅的方法是什么?
JSF2:将 cc:attribute 限制为 List 中的给定对象类型解释了检查列表的泛型类型的问题,但是attributeValue.class.name
对于这种情况,解决方案(检查看起来很老套的类型)不一定是必要的我想检查/断言原始类型。
我也对 JSF 2.3 的解决方案感兴趣。
解决方案
显然 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>
如果我再次遇到导致我怀疑是否有支票的情况,我会编辑问题。
推荐阅读
- javascript - 如何在 React-Kotlin 中导入节点模块?
- android - 在 Android 设备上捕获未处理/原始麦克风数据
- python - 如何获取 Python 3.x 中录制的音频的频率和幅度?
- c# - 将一个 Web 部件嵌套在另一个 Web 部件中
- fortran - Fortran:将硬编码值传递给接受 real(16) 的函数不起作用。我怎样才能解决这个问题?
- asp.net - 将 Angular (6) 客户端错误发送到 Web API 以进行日志记录
- php - 如何添加 smtp 主机以使用此 phpmail 脚本发送邮件
- dom - 带有嵌入式调查的 ServiceNow 电子邮件通知
- php - 从带有帖子的下拉选择中显示
- mysql - 如何在 MySQL 中检查 JSON 数组中的任何类似元素