JSF2.1でカスタムコンバーターを作成します。
【手順】
1.「JSF2.1プロジェクトの作成方法」の手順で、「JSF21Sample021-CustomConverter」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
2.「WebContent/WEB-INF/web.xml」を以下の様に入力。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>JSF21Sample021-CustomConverter</display-name>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
</web-app>
3.「Ctrl+Shift+F」を押し、ソースコードをフォーマッティング、「Ctrl+S」でファイルを保存。
4.「Javaパッケージの作成方法」の手順で、「com.example.jsf21sample021」というパッケージを作成。
5.「Javaクラスファイルの作成方法」の手順で、「CustomConverterBean.java」というクラスファイルを作成。
6.「CustomConverterBean.java」を以下の様に入力。
package com.example.jsf21sample021;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class CustomConverterBean implements Serializable {
private static final long serialVersionUID = 3806803543305054045L;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
7.「Ctrl+Shift+F」を押し、ソースコードをフォーマッティング、「Ctrl+S」でファイルを保存。
8.「Javaクラスファイルの作成方法」の手順で、「CustomValidator.java」というクラスファイルを作成。
9.「CustomValidator.java」を以下の様に入力。
※getAsObjectは画面からBeanへの保存処理。
※getAsStringはBeanから画面への表示処理。
package com.example.jsf21sample021;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
@FacesConverter(value = "jp.blogspot.foolprogrammer.CustomConverter")
public class CustomConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String string) {
return string.toUpperCase();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object object) {
return object.toString().toLowerCase();
}
}
10.「Ctrl+Shift+F」を押し、ソースコードをフォーマッティング、「Ctrl+S」でファイルを保存。
11.「任意のファイルの作成方法」の手順で、「WebContent/」ディレクトリに「index.xhtml」というファイルを作成。
12.「index.xhtml」を以下の様に入力。
<html xmlns="http://www.w3c.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:body>
<h:form>
<h:inputText id="name" value="#{customConverterBean.name}" converter="jp.blogspot.foolprogrammer.CustomConverter" />
<h:commandButton value="送信" />
<br />
<h:outputText value="#{customConverterBean.name}" />
<br />
<h:message showSummary="true" showDetail="false" for="name" />
</h:form>
</h:body>
</html>
13.「Ctrl+Shift+F」を押し、ソースコードをフォーマッティング、「Ctrl+S」でファイルを保存。
14.「動的Webアプリケーションをサーバーに配置する方法」の手順で、作成したプロジェクトをサーバーに配置します。
15.「サーバーをデバッグモードで起動する方法」の手順で、サーバーを起動します。
16.ブラウザで以下のURLにアクセスします。
http://localhost:8080/JSF21Sample021-CustomConverter/faces/index.xhtml
17.以下の様に表示されれば成功です。
以上です。


0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。