JSF中的跨域验证h:dataTable

时间:2012-02-10 07:39:37

标签: java jsf

我有一个XHTML页面,其中有四个文本框

<h:column >  
    <f:facet name="header">  
       <h:outputText value="Start Date" />  
    </f:facet>
    <h:inputText id="startDate" value="#{sampleVO.startDate}" />
</h:column>         
<h:column >  
    <f:facet name="header">  
       <h:outputText value="End Date" />  
    </f:facet>
    <h:inputText id="endDate" value="#{sampleVO.endDate}" />
</h:column>        
<h:column >
    <f:facet name="header">  
        <h:outputText value="Start Date" />  
    </f:facet>
    <h:inputText id="startDate1" value="#{sampleVO.startDate}" />
</h:column>         
<h:column >  
    <f:facet name="header">
       <h:outputText value="End Date" />  
    </f:facet>
    <h:inputText id="endDate1" value="#{sampleVO.endDate}" />
</h:column>        

我需要在开始日期和结束日期进行验证。如果用户在id =“startDate”&amp;中输入一些开始和结束日期。 id =“endDate”让我们说开始日期:“01/01/2012”(1jan)和结束日期:01/31/2012如果用户在id =“startDate1”和id =“endDate1”id =“上输入一些日期startDate1“应始终大于id =”endDate“即日期范围不应重叠

public class SampleServiceimpl implements SampleService {
    private List<SampleVO> listOfSample;

     //this function gets called when clicked on Submit button on XHTML page
     public Void submit() {
        // how can i call datesOverLaps function to check entered
        // start date and End date not overlapping, Also how to
        // display the error message on XHTML page using FacingContext
     }

     public boolean datesOverlaps(List<SampleVO> sampleVO) {
        final Date early = sampleVO.getStartDate();
        final Date late = sampleVO.getEndDate();

        for(SampleVO existing : sampleVO) {
            if(!(early.isAfter(existing.getEnd())) ||
                     (late.isBefore(existing.getStart()))){
                return true;
            }
        }
        return false;
    }
}

不确定上面的布尔函数是否是最新的并且可以在场景中使用。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

我花了6个小时。但我解决了它。我的英语不好。

  1. 在dataTable上添加绑定属性,指向backingbean中的UIDData字段(绑定表)

     <h:dataTable value="#{narocilo.podatki}" id="tabela" var="podatek" border="0" bgcolor="#F6FAF0" style="width:100%;" cellspacing="0" cellpadding="2" headerClass="header" rowClasses="#{narocilo.rowClasses}"   columnClasses="align_left,align_right,align_center,align_right,align_right,align_center_konec" **binding="#{narocilo.podatkiData}"**>
    
  2. 在表单广告结束之前

    <h:inputHidden id="validator" **validator="#{narocilo.validateKolicinaCena}"** value="morabiti"/>
    
  3. 支持bean中的验证方法

     public void validateKolicinaCena(FacesContext aContext,UIComponent aComponent,Object aValue) {
    
    int shrani_index = fPodatkiData.getRowIndex();
    
    UIComponent column_kol = fPodatkiData.getChildren().get(2);
    UIComponent column_cen = fPodatkiData.getChildren().get(5);
    
    for(int i=0; i<fPodatkiData.getRowCount(); i++) {
    
        fPodatkiData.setRowIndex(i);
    
        UIInput kolicina_input = (UIInput) column_kol.findComponent("kolicina");
        UIInput cena_input = (UIInput) column_cen.findComponent("cena");
    
        Object kolicina = kolicina_input.getLocalValue();
        Object cena = cena_input.getLocalValue();
    
        if(kolicina == null && cena != null) {
    
            kolicina_input.setValid(false);
    
            FacesMessage sporocilo = Sporocila.getMessage("si.alkimisticus.bitea.error", "javax.faces.component.UIInput.REQUIRED", null);
            sporocilo.setSeverity(FacesMessage.SEVERITY_ERROR);
    
            aContext.addMessage(kolicina_input.getClientId(aContext), sporocilo);
    
        } else if(kolicina != null && cena == null) {
    
            cena_input.setValid(false);
    
            FacesMessage sporocilo = Sporocila.getMessage("si.alkimisticus.bitea.error", "javax.faces.component.UIInput.REQUIRED", null);
            sporocilo.setSeverity(FacesMessage.SEVERITY_ERROR);
    
            aContext.addMessage(cena_input.getClientId(aContext), sporocilo);
    
        } else {
            kolicina_input.setValid(true);
            cena_input.setValid(true);
        }
    
    
    }
    
    fPodatkiData.setRowIndex(shrani_index);}