Jsf-validatelength-tag
提供:Dev Guides
JSF-f:validateLength
f:validateLengthタグは、特定の範囲の文字列値の長さを検証するために使用されます。
JSFタグ
<f:validateLength minimum = "5" maximum = "8"/>
タグ属性
S.No | Attribute & Description |
---|---|
1 |
minimum 最小文字数の文字列 |
2 |
maximum 最大文字数の文字列 |
応用例
上記のタグをテストするテストJSFアプリケーションを作成しましょう。
Step | Description |
---|---|
1 | Create a project with a name helloworld under a package com.finddevguides.test as explained in the JSF - First Application chapter. |
2 | Modify home.xhtml as explained below. Keep the rest of the files unchanged. |
3 | Create result.xhtml in the webapps directory as explained below. |
4 | Create UserData.java as a managed bean under package com.finddevguides.test as explained below. |
5 | Compile and run the application to make sure the business logic is working as per the requirements. |
6 | Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver. |
7 | Launch your web application using appropriate URL as explained below in the last step. |
UserData.java
package com.finddevguides.test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name = "userData", eager = true)
@SessionScoped
public class UserData implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
home.xhtml
<?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:head>
<title>JSF tutorial</title>
</h:head>
<h:body>
<h2>h:validateLength Example</h2>
<h:form>
<h:inputText id = "nameInput" value = "#{userData.name}"
label = "name" >
<f:validateLength minimum = "5" maximum = "8"/>
</h:inputText>
<h:commandButton value = "submit" action = "result"/>
<h:message for = "nameInput" style = "color:red"/>
</h:form>
</h:body>
</html>
result.xhtml
<?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:f = "http://java.sun.com/jsf/core"
xmlns:h = "http://java.sun.com/jsf/html">
<h:head>
<title>JSF Tutorial!</title>
</h:head>
<h:body>
<h2>Result</h2>
<hr/>
Name: #{userData.name}
</h:body>
</html>
すべての変更を完了したら、JSF-最初のアプリケーションの章で行ったようにアプリケーションをコンパイルして実行します。 すべてがアプリケーションで問題ない場合、次の結果が生成されます。
無効な値を入力してください。 出力は次のようになります。
有効な値を入力してください。 出力は次のようになります。