Validation JSF中基于两个组件的复合材料的验证/转换
我正在开发一个JSFWeb应用程序,需要使用周期作为数据结构。下面是我使用的Java类:Validation JSF中基于两个组件的复合材料的验证/转换,validation,jsf,jsf-2,composite-component,Validation,Jsf,Jsf 2,Composite Component,我正在开发一个JSFWeb应用程序,需要使用周期作为数据结构。下面是我使用的Java类: 公共类周期性实现可序列化{ 私有整数值=0; 私有周期类型; //二传手 } 公共枚举周期类型{ 日、周、月、年 } 这样我就可以为我的任务指定不同的周期,它可以将值与PeriodicityType组合起来 我还创建了一个名为periodicityInput.xhtml的输入复合元素,我可以使用它以可重用的方式以不同的形式提供该数据类型: <!DOCTYPE html PUBLIC "-//W3C/
公共类周期性实现可序列化{
私有整数值=0;
私有周期类型;
//二传手
}
公共枚举周期类型{
日、周、月、年
}
这样我就可以为我的任务指定不同的周期,它可以将值与PeriodicityType
组合起来
我还创建了一个名为periodicityInput.xhtml的输入复合元素,我可以使用它以可重用的方式以不同的形式提供该数据类型:
<!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:composite="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:body>
<composite:interface>
<composite:attribute name="value" required="true" />
<composite:attribute name="disabled" required="false" default="false" />
</composite:interface>
<composite:implementation>
<h:panelGrid columns="2" id="#{cc.id}">
<p:spinner value="#{cc.attrs.value.value}" min="1"
id="value_spinner" disabled="#{cc.attrs.disabled}" />
<p:selectOneMenu value="#{cc.attrs.value.type}" style="width:200px"
disabled="#{cc.attrs.disabled}">
<f:selectItem noSelectionOption="true"
itemLabel="#{windowsMessages.NOT_ASSIGNED}" />
<f:selectItems value="#{viewUtils.periodicityTypes}" />
</p:selectOneMenu>
</h:panelGrid>
</composite:implementation>
</h:body>
</html>
基本上,我有一个微调器
元素和一个选择菜单
,以便为周期选择值和类型。也有可能不选择任何类型,在这种情况下,输入数值必须为零。或者,更好的是,如果没有选择周期,则必须将输入转换为null/null元组
正如我在一些文章中所读到的,通过验证方法中的id,可以一次验证多个JSF组件:
UIInput confirmComponent=(UIInput)component.getAttributes().get(“确认”)
问题是,如何调整它以便在没有固定id的复合组件中使用?一种方法是创建一个扩展
UIInput
的支持组件,并在UIInput#getSubmittedValue()
中完成这项工作。子输入仅可通过findComponent()
直接使用
另见:
与具体问题无关,
确实不对。如果你真的需要的话,给它一个固定的ID
<cc:interface componentType="inputPeriodicity">
...
</cc:interface>
<cc:implementation>
...
<p:spinner id="value_spinner" ... />
<p:selectOneMenu id="type_menu" ... />
...
</cc:implementation>
@FacesComponent("inputPeriodicity")
public class InputPeriodicity extends UIInput implements NamingContainer {
@Override
public String getFamily() {
return UINamingContainer.COMPONENT_FAMILY;
}
@Override
public Object getSubmittedValue() {
UIInput typeMenu = (UIInput) findComponent("type_menu");
String type = (String) typeMenu.getSubmittedValue();
if (type == null || type.isEmpty()) {
UIInput valueSpinner = (UIInput) findComponent("value_spinner");
valueSpinner.setSubmittedValue("0"); // Don't use int/Integer here!
}
return super.getSubmittedValue();
}
}