2013-03-20 101 views
0

我在我的addNew.xhtml頁面有很多字段。 我必須對客戶端的字段進行驗證。 其中一個字段是city如何驗證jsf中的字段

如果我不輸入citycity cannot be left blank),我想要驗證錯誤。

此前我正在研究grails框架,並且定製驗證非常簡單。 現在我正在使用jsf,並且無法在Internet上找到很好的示例來解決此問題。

能否請你幫我在執行這一驗證

<!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:ui="http://java.sun.com/jsf/facelets" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:a4j="http://richfaces.org/a4j"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <link type="text/css" rel="stylesheet" href="../css/global.css" /> 

    </head> 
     <body> 
<div class="windowContents"> 
    <a4j:form style="width: 700px; height: 500px" ajaxSubmit="true" 
     id="addNewRecord"> 

      <a4j:repeat value="#{addAction.editedtable}" 
       var="address"> 

       <br /> 
       <br /> 

        <table border="0" style="font-size: 12px; width: 100%;"> 
         <tr> 
          <td><h:outputText value="File ID:" /></td> 
          <td><h:outputText value="#{address.fileId}" /></td> 

          <td><h:outputText value="Insured Name" />:</td> 
          <td><h:outputText value="#{dataEntryAction.insuredName}" /></td> 
         </tr> 
         <tr> 
          <td><h:outputText value="House No" /><span class="red">*</span>:</td> 
          <td><h:inputText value="#{address.houseNumber}" /></td> 

          <td><h:outputText value="Street" /><span class="red">*</span>:</td> 
          <td><h:inputText value="#{address.street}" /></td> 
         </tr> 
         <tr> 
          <td><h:outputText value="City" />:</td> 
          <td><h:inputText id ="city" value="#{address.city}" required="false" requiredMessage="City is required" /></td> 
              <h:message for="city" /> 


         </tr> 

        </table> 
      </a4j:repeat> 
      <br /> 
      <h:panelGroup rendered="true"> 
       <a4j:commandButton value="save" image="../images/buttons/save.gif" 
         render="@form" 
        action="#{addAction.saveEditedAndPrepareHistory(addAction.userComment,user, addAction.editedtable)}" 
        reRender="dataEnrtyTable,dataEntryDetails" 
        oncomplete="javascript:Richfaces.hideModalPanel('addNewRecordPanel')" 
        style="align:center;" /> 

      </h:panelGroup> 


    </a4j:form> 
</div> 

+1

關於JSF驗證器的基礎知識在[Mkyong JSF 2教程](http://www.mkyong.com/tutorials/jsf-2-0-tutorials/)中介紹(請查看轉換器和驗證部分)和/或任何其他的JSF 2教程。請參閱此處以瞭解此主題,並在有真正問題時再回來。 – 2013-03-20 05:42:31

回答

3

有在JSF不同的選擇實現自己的任務,

1.客戶端驗證。

您在您的問題中詢問客戶端驗證。

<tr> 
    <td><h:outputText value="City" />:</td> 
    <td><h:inputText value="#{address.city}" id="cityID"/></td> 
</tr> 

<h:commandLink action="#{yourBean.submitMethod}" onclick="checkCity(cityID)"> 
    <h:outputText value="Submit"></h:outputText> 
</h:commandLink> 

function checkCity(cityID) 
{ 
    var cityEntered = $("#" + cityID).val(); 
    //Do whatever validations you want 
} 

不相關的問題 還有其他的方法也(你有沒有在你的問題問)。

2.maxlength - 可以在此字段中輸入的最大字符數。

<h:inputText id="cityID" value="#{yourBean.cityID}" maxlength="10"/> 

3。服務器端驗證

假設您沒有將onclick事件放入h:commandLink點。 1

在你的Facelets頁,

<h:messages globalOnly="true" layout="table" /> 


public String submitMethod() { 
try 
{ 
    FacesContext facesContext = null; 
    facesContext=FacesContext.getCurrentInstance(); 

    ArrayList <String> errorList= new ArrayList <String>(); 
    if(this.cityID.equals(//something)) 
    { 
     errorList.add("//some message"); 
    } 
    if(errorList != null && errorList.size() > 0) 
    { 
     for(int i=0;i<errorList.size();i++) 
     { 
      msg= (String)errorList.get(i); 
      FacesMessage facemsg= new FacesMessage(FacesMessage.SEVERITY_ERROR,msg ,msg); 
      facesContext.addMessage(null, facemsg); 
     } 
    } 
} 
catch(Exception e) 
    { 
    //Do error handling 
    } 

} 

4.內置的驗證器組件

<h:inputText id="cityID" value="#{yourBean.cityID}"> 
    <f:validateLength minimum="6" maximum="15"/> 
</h:inputText> 
+0

Thanx回答。但我想知道,在函數checkCity()你添加了評論「/ /做任何你想要的驗證」。然後我必須寫在那裏執行驗證空字段。請回答,如果你知道。 :) – 2013-03-20 06:09:34

+0

我的意思是說,現在你有輸入城市的價值出現在cityEntered變量中。使用它,你可以檢查是否符合你的要求,如「if(cityEntered ==」「)'或其他一些複雜的檢查。 – 2013-03-20 06:13:09

3

嚴格回答你的問題,在JSF 1.2+,這應該這樣做:

<h:outputLabel for="city" value="City" /> 
<h:inputText id="city" value="#{address.city}" required="true" requiredMessage="City cannot be left blank." /> 
<h:message for="city" /> 

我認爲這些文章可能會讓你感興趣:

我希望它能幫助!

+0

thanx但我使用jsf版本1.2所以它不工作:( – 2013-03-20 06:19:33

+0

對不起,我錯過了這是爲JSF 2.1。它是一個正在進行的項目嗎?你不能升級?有很多的改進可能是方便。:) 此致敬意。 – rbento 2013-03-20 06:24:41

+0

對不起,我不能升級,因爲它的客戶需求,如果我們升級然後很多回歸是需要的。所以我只好照顧這個版本1.2。 – 2013-03-20 06:26:44