如何在jsf中验证字段 [英] How to validate a field in jsf
问题描述
我的addNew.xhtml
页中有很多字段.
我必须在客户端进行字段验证.
字段之一是city
.
I have many fields in my addNew.xhtml
page.
I have to do validations of the fields on client side.
One of the field is city
.
如果我不输入表示city cannot be left blank
的city
,我想得到一个验证错误.
I want to get a validation error if I don't enter city
which says city cannot be left blank
.
以前,我在grails框架上工作,自定义验证是一项非常简单的任务. 现在,我正在研究jsf,但无法在互联网上找到解决该问题的好例子.
Previously I was working on grails framework and it was a very easy task to customize validations. Now I am working on jsf and I'm having trouble finding good examples on the internet to solve this issue.
请帮助我实施此验证
<!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.2+中严格回答您的问题,这应该做到:
Strictly answering your question, in JSF 1.2+, this should do it:
<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" />
我认为这些文章可能对您很有趣:
I think that these articles may be interesting to you:
- Validator for multiple fields
- JSF conversion and validation
希望对您有帮助!
这篇关于如何在jsf中验证字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!