バッキングビーンの基本
バッキングビーンの基本
そしてお互いのマッピングは、JAVAのクラスに付与するアノテーションとなり、以下書式です。
@Named(value = "sampleBean")
ビュー名が『sample.xhtml』の場合は、バッキングビーン名は『SampleBean.java』となります。
そしてこのマナーを守った特典として、引数不要の『@Named』を使う事ができます。
例えばサンプルでも使用している「ビュースコープ」のライフサイクルは、画面が閉じられるか、リダイレクトされるまで内容を維持します。
※正確にはビュースコープはCDI標準の仕様ではありません(広義として記載) 以下は寿命が短い順に並べたスコープと、その定義クラスです。
・@ViewScoped import jakarta.faces.view.ViewScoped;
・@SessionScoped import jakarta.enterprise.context.SessionScoped;
・@ApplicationScoped import jakarta.enterprise.context.ApplicationScoped;
画面遷移
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="jakarta.faces.html">
<h:head>
<title>NEXT</title>
</h:head>
<h:body>
<h1>次の画面</h1>
</h:body>
</html>
package view;
import java.io.IOException;
import java.io.Serializable;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import jakarta.faces.context.ExternalContext;
import jakarta.faces.context.FacesContext;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
import lombok.Getter;
import lombok.Setter;
@Named(value = "sampleBean")
@ViewScoped
public class SampleBean implements Serializable {
private static final long serialVersionUID = 1L;
@Getter
@Setter
private String name;
public void submit() {
System.out.println("submitメソッドです。[" + this.name + "]が送信されました。");
}
public String goPost() {
return "next";
}
public String goGet() {
return String.format("next?faces-redirect=true&inpText=%s&munber=%d", this.name, 123);
}
public void goYahoo() {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
try {
String paramName = URLEncoder.encode("テスト", StandardCharsets.UTF_8.toString());
String targetUrl = "https://www.yahoo.co.jp/?q=" + paramName;
externalContext.redirect(targetUrl);
} catch (IOException e) {
e.printStackTrace();
}
}
}
何も返却しない場合はPOSTでの自画面遷移となります。
※「return null」でも良いです
遷移したい画面のファイル名(拡張子なし)を文字列で返却します。
パラメータを渡したい場合はサンプルの様に『return String.format』関数を使うと便利です。
基本的な書式は、文字列で『next?faces-redirect=true』となります。
画面間での値の受取方法
サンプルソースの場合、テキストボックスに入れた値がエクリプスのコンソールに表示されているのが確認できるハズです。
では、画面遷移時の値はどうやって受け取るのでしょうか。
以下にその方法を紹介します。
jakarta.faces.context.FacesContext fc = jakarta.faces.context.FacesContext.getCurrentInstance();
Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
System.out.println(params.get("inpText"));
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="jakarta.faces.core"
xmlns:h="jakarta.faces.html">
<f:metadata>
<f:event type="preRenderView" listener="#{nextBean.init}" />
</f:metadata>
<h:head>
<title>NEXT</title>
</h:head>
<h:body>
<h1>次の画面</h1>
</h:body>
</html>
package view;
import java.io.Serializable;
import java.util.Map;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
@Named(value = "nextBean")
@ViewScoped
public class NextBean implements Serializable {
private static final long serialVersionUID = 1L;
public void init() {
jakarta.faces.context.FacesContext fc = jakarta.faces.context.FacesContext.getCurrentInstance();
Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
System.out.println(params.get("inpText"));
}
}
POST、GET遷移共に値が取得できましたか?
もしGET遷移は取得できたけど、POST遷移は取得できない場合は以下を見直してください。
『sample.xhtmlのformタグ』
この『prependId』属性が「ない」or「true」の場合は、取得するキー名が『hoge:inpText』となります。
それでは遷移元であるバッキングビーンの「goPost」関数に以下を追記してみましょう。
public String goPost() {
Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
flash.put("pram_name", "フラッシュスコープです");
return "next";
}
public void init() {
jakarta.faces.context.FacesContext fc = jakarta.faces.context.FacesContext.getCurrentInstance();
Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
System.out.println(params.get("hoge:inpText"));
// フラッシュスコープ
Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();
System.out.println((String) flash.get("pram_name"));
}
一度だけ有効なセッションです。
かなり有効な手段となり、仮に渡したい値が100個あってもDTOでまとめて1回で完結できます。




<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="jakarta.faces.html">
<h:head>
<title>Hello World</title>
<h:outputStylesheet library="css" name="style.css" />
</h:head>
<h:body>
<h1>おはよう世界!</h1>
<h:form id="hoge" prependId="false">
<h:inputText id="inpText" value="#{sampleBean.name}" styleClass="parts_text" style="width:300px;height:25px;" />
<BR />
<h:commandButton value="自画面繊維" action="#{sampleBean.submit()}" />
<BR />
<h:commandButton value="POST繊維" action="#{sampleBean.goPost()}" />
<BR />
<h:commandButton value="GET繊維" action="#{sampleBean.goGet()}" />
<BR />
<h:commandButton value="外部繊維" action="#{sampleBean.goYahoo()}" />
</h:form>
</h:body>
</html>