jakaruta.doc_020
最終投稿日:2026年07月26日
<!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();
}
}
}
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タグ』
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>