簡體   English   中英

在JSF中驗證/轉換基於兩個組件的組合

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM