Developing our first JSF application

We will create a simple Maven web application to demonstrate how to use JSF with Kotlin. The application will have a JSF page that simply displays a greeting message. Perform the following steps to develop the JSF application:

  1. In IntelliJ IDEA, go to New, and then click on Project.
  2. Select maven-archetype-webapp and create the web app project. 
  3. This will create the project structure, as follows:
  1. Next, we need to add the following Maven dependencies to the project:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-api}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>${com.sun.faces.jsf-api}</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>${com.sun.faces.jsf-impl}</version>
</dependency>
  1. Create a managed bean called Person.kt. This is shown in the following code:
@ManagedBean(name = "person")
class Person {
val name: String
get() = "Mathur"
}
  1. Create a jsf file with an xhtml extension:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view>
<h:outputLabel value="JSF-App example"/>
</f:view>

<body>
<h3>
Welcome #{person.getName()}
</h3>
</body>
</html>
  1. Mention the index.xhtml page in the deployment descriptor to load it following the startup of the app:
<welcome-file-list>
<welcome-file>index.jsf</welcome-file>
</welcome-file-list>
  1. Now, build the application and deploy it on the Tomcat server. The output is as follows:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset