LoginSignup
0

More than 1 year has passed since last update.

JSFのサンプル

Last updated at Posted at 2022-04-17

構成

  • JsfTest
    • src
      • main
        • java
          • bean
            • IndexBean.java
            • InputBean.java
        • webapp
          • index.xhtml
          • next.xhtml
            • WEB-INF
              • faces-config.xml
              • web.xml
    • pom.xml

pom.xml

	<dependency>
		<groupId>com.sun.faces</groupId>
		<artifactId>jsf-api</artifactId>
		<version>2.2.20</version>
	</dependency>
	<dependency>
		<groupId>com.sun.faces</groupId>
		<artifactId>jsf-impl</artifactId>
		<version>2.2.20</version>
	</dependency>

faces-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_3.xsd"
    version="2.3">
</faces-config>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
  <display-name>JsfTest</display-name>
  <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>*.xhtml</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
  </welcome-file-list>
</web-app>

IndexBean.java

package bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class IndexBean{

	private String label;
	
	public IndexBean(){
		this.label = "ラベル";
	}
	
	public String getLabel() {
		return label;
	}
	
	public void setLabel(String label) {
		this.label = label;
	}
}

InputBean.java

package bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean
@RequestScoped
public class InputBean {
	private String text;
	
	public String getText() {
		return text;
	}
	
	public void setText(String text) {
		this.text = text;
	}
	
	public String send() {
		return "next.xhtml";
	}
}

index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:i="http://xmlns.jcp.org/jsf/component"
      xmlns:jsf="http://xmlns.jcp.org/jsf"
      xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>トップページ</title>
</h:head>
<h:body>
	#{indexBean.label}
	<br/>
	<h:outputText value="#{indexBean.label}" />
	<h:form id="frm">
		<input type="text" jsf:value="#{inputBean.text}" />
		<h:commandButton value="送信" action="#{inputBean.send()}" />
		<input type="submit" value="送信" jsf:action="#{inputBean.send()}" />
	</h:form>
	</h:body>
</html>

next.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>送信後のページ</title>
</h:head>
	<h:body>
		<h:outputText value="#{inputBean.text}" />
       	<h:outputText value="条件分岐" rendered="#{inputBean.text ne ''}"/>
		<ui:fragment rendered="#{inputBean.text ne ''}">
			<input type="button" value="ボタン" />
		</ui:fragment>
	</h:body>
</html>

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0