JSF 2 validateRequiredの例
”
f:validateRequired
“はJSF 2.0の新しいバリデータタグで、入力フィールドが空でないことを確認するために使用されます。例えば、
<h:inputSecret id="password" value="#{user.password}"> <f:validateRequired/> </h:inputSecret>
あるいは、「必須」属性も使用できます。両方とも同じ空の値の検証を行います。
<h:inputSecret id="password" value="#{user.password}" required="true"/>
“f:validateRequired”の例
「
f:validateRequired
」タグの使用方法を示すJSF 2.0の例で、「password」フィールドが空でないことを確認します。
1.マネージドBean
ユーザー管理Bean。
package com.mkyong; import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name="user") @SessionScoped public class UserBean implements Serializable{ String password; String confPassword; //getter and setter methods }
2. JSFページ
JSF XHTMLページで、 ”
f:validateRequired
“タグを使用して、 “password”と “confirm password”の両方のフィールドが空でないことを確認してください。
<?xml version="1.0" encoding="UTF-8"?> <!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:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" > <h:body> <h1>JSF 2 validateRequired example</h1> <h:form> <h:panelGrid columns="3"> Enter your password : <h:inputSecret id="password" value="#{user.password}" size="20" required="true" label="Password"/> <h:message for="password" style="color:red"/> Enter your password again : <h:inputSecret id="confPassword" value="#{user.confPassword}" size="20" label="Confirm Password"> <f:validateRequired/> </h:inputSecret> <h:message for="confPassword" style="color:red"/> </h:panelGrid> <h:commandButton value="Submit" action="result"/> </h:form> </h:body> </html>
デモ
「パスワード」または「パスワードの確認」フィールドが空の場合は、エラーメッセージを表示します。
ソースコードをダウンロードする
ダウンロード – リンク://wp-content/uploads/2010/10/JSF-2-ValidateRequired-Example.zip[JSF-2-ValidateRequired-例.zip](9KB)
リファレンス
2 validateRequired JavaDoc]