我在我的addNew.xhtml
頁面有很多字段。 我必須對客戶端的字段進行驗證。 其中一個字段是city
。如何驗證jsf中的字段
如果我不輸入city
(city 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>
關於JSF驗證器的基礎知識在[Mkyong JSF 2教程](http://www.mkyong.com/tutorials/jsf-2-0-tutorials/)中介紹(請查看轉換器和驗證部分)和/或任何其他的JSF 2教程。請參閱此處以瞭解此主題,並在有真正問題時再回來。 – 2013-03-20 05:42:31