[JAVA] moved! JSF + CDI (Preferences) on Tomcat

Introduction

I want to implement EE more easily! I want to try! So, I would like to run JSF + CDI on Tomcat. JSF and CDI don't usually work on Tomcat, but Tomcat9 seems to support CDI so I'll give it a try.

The sample application for operation check creates a login screen and a top screen that transitions when it succeeds. Roughly like this. サンプルアプリ The environment is as follows.

What you need to do before implementation

To use CDI, Apache OpenWebBeans To use JSF, you need Apache MyFaces.

Follow the steps below to set up the environment.

  1. Download and unzip the Apache OpenWebBeans zip
  2. Download the Apache MyFaces zip (do not unzip)
  3. Run the shell in the folder you unzipped in 1.
  4. Web.xml settings

1. Download Apache OpenWebBeans

Download from the Apache OpenWebBeans page (https://openwebbeans.apache.org/download.html). This time, I downloaded openwebbeans-distribution-2.0.18-binary.zip. After downloading, unzip it.

2. Download Apache MyFaces

Download from the Apache MyFaces page (http://myfaces.apache.org/#/core23next). This time, I downloaded myfaces-core-assembly-2.3-next-M4-bin.zip. This zip does not need to be unzipped. I will use it as it is.

3. Run the shell

There is install_owb_tomcat7_myfaces.sh in the folder where you unzipped OpenWebBeans, so execute it. The arguments are the downloaded myfaces zip file and the tomcat path.

./install_owb_tomcat7_myfaces.sh /Download path/myface-core-assembly-2.3-next-M4-bin.zip tomcat path

When executed, the required jar files will be copied to tomcat / lib and the Listener will be added to the context.xml.

4. Web.xml settings

Set Listener to enable CDI and JSF. Set it and Servlet Mapping.

web.xml


<listener>
    <listener-class>org.apache.webbeans.servlet.WebBeansConfigurationListener</listener-class>
</listener>
<listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>

<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>

You are now ready. CDI / JSF should now work on Tomcat!

Implementation

To check the operation, I will implement a sample application. First, maven settings.

maven settings

pom.xml


<!-- Servlet -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.0</version>
    <scope>provided</scope>
</dependency>

<!-- JSF -->
<dependency>
    <groupId>org.apache.myfaces.core</groupId>
    <artifactId>myfaces-api</artifactId>
    <version>2.3-next-M4</version>
    <scope>provided</scope>
</dependency>

<!-- CDI -->    
<dependency>
    <groupId>javax.enterprise</groupId>
    <artifactId>cdi-api</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
</dependency>

<!-- Lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
</dependency>

Add provided as provided for CDI and JSF compilation. Since it is in tomcat / lib at the time of operation You do not need. I also introduced lombok. (This is not required)

Creating a screen

In JSF, create a screen (xhtml) and a Backing Bean corresponding to the screen. First, create a screen. Ignore the design and implement it concisely.

index.xhtml


<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:ui="http://java.sun.com/jsf/facelets" 
	xmlns:h="http://java.sun.com/jsf/html" 
	xmlns:ph="http://xmlns.jcp.org/jsf/passthrough"
	xmlns:f="http://java.sun.com/jsf/core">

<h:head>
    <title>JSF sample</title>
</h:head>

<body>

    <h:messages escape="true" id="messages" showDetail="false" closable="true"/>
    <h:form id="form">
        <br/>
        <h:inputText value="#{index.loginId}" ph:placeholder="Username" /><br/>
        <h:inputSecret value="#{index.password}" ph:placeholder="Password" /><br/>
        <h:commandButton action="#{index.loginEvent()}" value="Login" update="@form" />
    </h:form>
</body>
</html>

Next is Backing Bean.

IndexBean.java


@Named("index")    //・ ・ ・ ①
@ViewScoped
@Getter
@Setter
public class IndexBean implements Serializable {

    private String loginId;        //・ ・ ・ ②
    private String password;

    public String loginEvent() {
        if (!loginId.isEmpty() && !password.isEmpty()) {
            return "top.xhtml?redirect=true";     //・ ・ ・ ③

        } else {
            //・ ・ ・ ④
            FacesContext context = FacesContext.getCurrentInstance();
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Authentication error", "");
            context.addMessage(null, message);
            return null;
        }
    }
}

(1) Use the Named annotation to set it so that it can be seen from the EL expression in index.xhtml. (2) Define the attributes according to the items on the screen. (3) If something is entered in both the login ID and password, the screen will change. ④ In other cases, the message is displayed on the self-screen.

Next is the Top screen of the transition destination.

top.xhtml


<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
	xmlns:ui="http://java.sun.com/jsf/facelets" 
	xmlns:h="http://java.sun.com/jsf/html" 
	xmlns:ph="http://xmlns.jcp.org/jsf/passthrough"
	xmlns:f="http://java.sun.com/jsf/core">

<h:head>
	<title>JSF sample</title>
</h:head>

<body>
	<h:form id="form">
		<div>TOP screen</div>
	</h:form>
</body>
</html>

Complete

A message was displayed and the screen transition was possible. It was easier than I expected! that's all.

reference

Apache OpenWebBeans

Recommended Posts

moved! JSF + CDI (Preferences) on Tomcat
moved! JSF + CDI on Tomcat and Bean Validation
JSP on tomcat
Tomcat v8 on CentOS7
1002 error on tomcat + webSocket
Install tomcat on Sakura's VPS
Install tomcat + eclipse on mac