[英]Validation/conversion for two component based composite in JSF
我正在开发一个JSF Web应用程序,我需要使用周期性作为数据结构。 这里有我使用的Java类:
public class Periodicity implements Serializable {
private Integer value = 0;
private PeriodicityType type;
//Getter and setters
}
public enum PeriodicityType {
DAY, WEEK, MONTH, YEAR
}
这样我就可以为我的任务指定不同的周期,这可以将值与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>
基本上我有一个spinner
元素和一个selectOneMenu
,以便为周期选择一个值和一个类型。 还有机会不选择任何类型, 在这种情况下输入数值必须为零 。 或者,更好的是, 如果没有选择周期 , 则必须将输入转换为null / null元组 。
正如我在一些网站上读到的那样,有机会在验证方法中同时验证多个JSF组件:
UIInput confirmComponent =(UIInput)component.getAttributes()。get(“confirm”);
问题是,如何调整它以便在没有固定id的复合组件中使用?
一种方法是创建扩展UIInput
的后备组件,并在UIInput#getSubmittedValue()
。 子编号只能由findComponent()
直接使用。
<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();
}
}
与具体问题无关 , <h:panelGrid id="#{cc.id}">
确实不对。 如果你真的需要,给它一个固定的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.