2013-03-20 100 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