簡體   English   中英

如何在jsf中驗證字段

[英]How to validate a field in jsf

我的addNew.xhtml頁面中有很多字段。 我必須在客戶端進行字段驗證。 領域之一是city

如果我不輸入city不能表示city cannot be left blank我想得到一個驗證錯誤。

以前,我在grails框架上工作,自定義驗證是一項非常簡單的任務。 現在,我正在研究jsf,但無法在互聯網上找到解決此問題的良好示例。

您能幫我實施此驗證嗎

   <!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>

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 服務器端驗證

可以說您還沒有為h:commandLink放置onclick事件。 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>

在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" />

我認為這些文章可能對您很有趣:

希望對您有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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