diff --git a/README.adoc b/README.adoc index 9665fad..2defa6c 100644 --- a/README.adoc +++ b/README.adoc @@ -11,14 +11,14 @@ :page-duration: 25 minutes :page-releasedate: 2024-12-07 :page-essential: false -:page-description: Learn how to build a dynamic web application using Jakarta Faces for the user interface, Jakarta Contexts and Dependency Injection for managing beans, and Jakarta Expression Language for binding and handling data. +:page-description: Learn how to build a dynamic web application using Jakarta Faces for the user interface (UI), Jakarta Contexts and Dependency Injection (CDI) for managing beans, and Jakarta Expression Language (EL) for binding and handling data. :guide-author: Open Liberty :page-tags: ['jakarta-ee'] :page-related-guides: ['grpc-intro'] :page-permalink: /guides/{projectid} :imagesdir: /img/guide/{projectid} :page-seo-title: Building a dynamic web application using Jakarta Faces, Jakarta Contexts and Dependency Injection, and Jakarta Expression Language -:page-seo-description: A getting started tutorial on how to build a dynamic web application using Jakarta Faces for the user interface, Jakarta Contexts and Dependency Injection for bean management, and Jakarta Expression Language for data binding and handling in Java. +:page-seo-description: A getting started tutorial on how to build a dynamic web application using Jakarta Faces for the user interface (UI), Jakarta Contexts and Dependency Injection (CDI) for bean management, and Jakarta Expression Language (EL) for data binding and handling in Java. :common-includes: https://raw.githubusercontent.com/OpenLiberty/guides-common/prod :source-highlighter: prettify = Building a dynamic web application using Jakarta Faces @@ -65,7 +65,7 @@ After you see the following message, your Liberty instance is ready. The defaultServer server is ready to run a smarter planet. ---- -Check out the web application at http://localhost:9080/index.xhtml. Click the image:refresh.png[refresh icon, 18, 18] refresh button, located next to the table title, to update and display the latest system load data in the table. +Check out the web application at the http://localhost:9080/index.xhtml URL. Click the image:refresh.png[refresh icon, 18, 18] refresh button, located next to the table title, to update and display the latest system load data in the table. After you are finished checking out the application, stop the Liberty instance by pressing `CTRL+C` in the command-line session where you ran Liberty. Alternatively, you can run the `liberty:stop` goal from the `finish` directory in another shell session: @@ -120,7 +120,7 @@ include::finish/src/main/webapp/WEB-INF/includes/footer.xhtml[] In the [hotspot file=0]`index.xhtml` file, the [hotspot=xmlns file=0]`xmlns` attributes define the XML namespaces for various Jakarta Faces tag libraries. These namespaces allow the page to use Jakarta Faces tags for templating, creating UI components, and enabling core functionality like form submissions and data binding. For more information on the various tag libraries and their roles in Jakarta Faces, refer to the https://jakarta.ee/learn/docs/jakartaee-tutorial/current/web/faces-facelets/faces-facelets.html#_tag_libraries_supported_by_facelets[Jakarta Faces Tag Libraries Documentation^]. -The [hotspot file=0]`index.xhtml` file combines standard HTML elements with Jakarta Faces components, enabling both static layout and dynamic functionality. Jakarta Faces tags, like [hotspot=outputStylesheet file=0]`` and [hotspot=uiIncludeFooter file=0]``, manage UI components, resource inclusion, and data binding, offering additional features beyond standard HTML; while standard elements like `
` and `
` structure the page's layout. The [hotspot=outputStylesheet file=0]`` includes a CSS file to style the page. The [hotspot=uiIncludeFooter file=0]`` tag incorporates reusable components, such as the provided [hotspot file=1]`footer.xhtml` file, to streamline maintenance and reuse across multiple pages. +The [hotspot file=0]`index.xhtml` file combines standard HTML elements with Jakarta Faces components, providing both static layout and dynamic functionality. Standard HTML elements, like `
` and `
`, structure the page's layout. Jakarta Faces tags manage UI components, resource inclusion, and data binding, offering additional features beyond standard HTML. For example, the [hotspot=outputStylesheet file=0]`` tag loads a CSS file for styling, and the [hotspot=uiIncludeFooter file=0]`` tag incorporates reusable components, such as the provided [hotspot file=1]`footer.xhtml` file, to streamline maintenance and reuse across multiple pages. At this point, the page defines a table that has no data entries. We'll add dynamic content in the following steps. @@ -165,13 +165,13 @@ To provide system load data to your web application, you'll create a CDI-managed [role="code_command hotspot file=0", subs="quotes"] ---- #Create the SystemLoadBean class.# -`src/main/java/io/openliberty/guides/application/SystemLoadBean.java` +`src/main/java/io/openliberty/guides/bean/SystemLoadBean.java` ---- SystemLoadBean.java [source, java, linenums, role='code_column hide_tags=copyright'] ---- -include::finish/src/main/java/io/openliberty/guides/application/SystemLoadBean.java[] +include::finish/src/main/java/io/openliberty/guides/bean/SystemLoadBean.java[] ---- Annotate the [hotspot file=0]`SystemLoadBean` class with a [hotspot=namedAnnotation file=0]`@Named` annotation to make it accessible in the Jakarta Faces pages under the name [hotspot=namedAnnotation file=0]`systemLoadBean`. Because the [hotspot=applicationScopedAnnotation file=0]`SystemLoadBean` bean is a CDI managed bean, a scope is necessary. An application scope is used in this example. To learn more about CDI, see the https://openliberty.io/guides/cdi-intro.html[Injecting dependencies into microservices^] guide. @@ -191,7 +191,7 @@ Now that you have the backend logic implemented with CDI, you'll update the Jaka [role="code_command hotspot file=0", subs="quotes"] ---- -#Update the index.xhtml file.# +#Replace the index.xhtml file.# `src/main/webapp/index.xhtml` ---- @@ -205,12 +205,12 @@ include::finish/src/main/webapp/index.xhtml[] SystemLoadBean.java [source, java, linenums, role='code_column hide_tags=copyright'] ---- -include::finish/src/main/java/io/openliberty/guides/application/SystemLoadBean.java[] +include::finish/src/main/java/io/openliberty/guides/bean/SystemLoadBean.java[] ---- The [hotspot file=0]`index.html` uses a [hotspot=commandButton file=0]`` tag to create the refresh button, where the action [hotspot=commandButtonAction file=0]`#{systemLoadBean.fetchSystemLoad}` invokes the [hotspot=fetchSystemLoadMethod file=1]`fetchSystemLoad()` method using Jakarta Expression Language when the button is clicked. This EL expression references the [hotspot=namedAnnotation file=1]`systemLoadBean` managed bean, triggering the method to update the system load data. The [hotspot=ajaxTag file=0]`` tag ensures that the [hotspot=systemLoadForm file=0]`systemLoadForm` component is re-rendered without a full page reload. -The [hotspot=systemLoadsTable file=0]`systemLoadsTable` is populated using the [hotspot=uiRepeat file=0]`` tag, which iterates over the list of system load data provided by the [hotspot=namedAnnotation file=1]`systemLoadBean`. The EL expression [hotspot=dataBind file=0]`#{systemLoadBean.systemLoads}` calls the [hotspot=getSystemLoads file=1]`getSystemLoads()` method from the managed bean, binding the data to the UI components. If the `systemLoadBean` hasn't been created yet, it is automatically initialized at this point. For each entry, the `time`, `cpuLoad`, and `memoryUsage` fields are displayed using [hotspot=outputText1 hotspot=outputText2 hotspot=outputText3 file=0]``, while [hotspot=convertNumber1 hotspot=convertNumber2 file=0]`` formats numeric values to two decimal places. +The [hotspot=systemLoadsTable file=0]`systemLoadsTable` is populated using the [hotspot=uiRepeat file=0]`` tag, which iterates over the list of system load data provided by the [hotspot=namedAnnotation file=1]`systemLoadBean`. The EL expression [hotspot=dataBind file=0]`#{systemLoadBean.systemLoads}` calls the [hotspot=getSystemLoads file=1]`getSystemLoads()` method from the managed bean, binding the data to the UI components. If the `systemLoadBean` hasn't been created yet, it is automatically initialized at this point. For each entry, the `time`, `cpuLoad`, and `memoryUsage` fields are displayed using [hotspot=outputText1 hotspot=outputText2 hotspot=outputText3 file=0]``. The [hotspot=convertNumber1 hotspot=convertNumber2 file=0]`` tag formats `cpuLoad` to seven decimal places and `memoryUsage` to two decimal places. // ================================================================================================= // Running the application @@ -239,13 +239,13 @@ While you can manually verify the web application by visiting http://localhost:9 [role="code_command hotspot file=0", subs="quotes"] ---- #Create the SystemLoadBeanTest class.# -`src/test/java/io/openliberty/guides/application/SystemLoadBeanTest.java` +`src/test/java/io/openliberty/guides/bean/SystemLoadBeanTest.java` ---- SystemLoadBeanTest.java [source, java, linenums, role='code_column hide_tags=copyright'] ---- -include::finish/src/test/java/io/openliberty/guides/application/SystemLoadBeanTest.java[] +include::finish/src/test/java/io/openliberty/guides/bean/SystemLoadBeanTest.java[] ---- The [hotspot=setUp file=0]`setUp()` method is annotated with the [hotspot=BeforeEach file=0]`@BeforeEach` annotation, indicating that it is run before each test case to ensure a clean state for each test execution. In this case, it creates a new instance of `SystemLoadBean` and manually calls the `init()` method to initialize the list of system load data before each test. @@ -267,8 +267,8 @@ You see the following output: ------------------------------------------------------- T E S T S ------------------------------------------------------- -Running io.openliberty.guides.application.SystemLoadBeanTest -Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.037 s -- in io.openliberty.guides.application.SystemLoadBeanTest +Running io.openliberty.guides.bean.SystemLoadBeanTest +Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.037 s -- in io.openliberty.guides.bean.SystemLoadBeanTest Results: diff --git a/finish/src/main/java/io/openliberty/guides/application/SystemLoadBean.java b/finish/src/main/java/io/openliberty/guides/bean/SystemLoadBean.java similarity index 95% rename from finish/src/main/java/io/openliberty/guides/application/SystemLoadBean.java rename to finish/src/main/java/io/openliberty/guides/bean/SystemLoadBean.java index bfd2c2f..0baa2ad 100644 --- a/finish/src/main/java/io/openliberty/guides/application/SystemLoadBean.java +++ b/finish/src/main/java/io/openliberty/guides/bean/SystemLoadBean.java @@ -9,20 +9,22 @@ * SPDX-License-Identifier: EPL-2.0 *******************************************************************************/ // end::copyright[] -package io.openliberty.guides.application; +package io.openliberty.guides.bean; import java.lang.management.ManagementFactory; import java.lang.management.MemoryMXBean; -import com.sun.management.OperatingSystemMXBean; -import java.util.Calendar; import java.util.ArrayList; +import java.util.Calendar; import java.util.List; import java.io.Serializable; import jakarta.annotation.PostConstruct; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Named; -import io.openliberty.guides.application.model.SystemLoadData; + +import com.sun.management.OperatingSystemMXBean; + +import io.openliberty.guides.bean.model.SystemLoadData; // tag::namedAnnotation[] @Named("systemLoadBean") diff --git a/finish/src/main/java/io/openliberty/guides/application/model/SystemLoadData.java b/finish/src/main/java/io/openliberty/guides/bean/model/SystemLoadData.java similarity index 95% rename from finish/src/main/java/io/openliberty/guides/application/model/SystemLoadData.java rename to finish/src/main/java/io/openliberty/guides/bean/model/SystemLoadData.java index ea42925..967c9a4 100644 --- a/finish/src/main/java/io/openliberty/guides/application/model/SystemLoadData.java +++ b/finish/src/main/java/io/openliberty/guides/bean/model/SystemLoadData.java @@ -9,7 +9,7 @@ * SPDX-License-Identifier: EPL-2.0 *******************************************************************************/ // end::copyright[] -package io.openliberty.guides.application.model; +package io.openliberty.guides.bean.model; public class SystemLoadData { private String time; diff --git a/finish/src/main/liberty/config/server.xml b/finish/src/main/liberty/config/server.xml index a102acb..7f8bdaa 100755 --- a/finish/src/main/liberty/config/server.xml +++ b/finish/src/main/liberty/config/server.xml @@ -16,12 +16,6 @@ - - - - diff --git a/finish/src/main/webapp/index.xhtml b/finish/src/main/webapp/index.xhtml index c3f4451..16e9757 100755 --- a/finish/src/main/webapp/index.xhtml +++ b/finish/src/main/webapp/index.xhtml @@ -82,7 +82,7 @@ - + diff --git a/finish/src/test/java/io/openliberty/guides/application/SystemLoadBeanTest.java b/finish/src/test/java/io/openliberty/guides/bean/SystemLoadBeanTest.java similarity index 90% rename from finish/src/test/java/io/openliberty/guides/application/SystemLoadBeanTest.java rename to finish/src/test/java/io/openliberty/guides/bean/SystemLoadBeanTest.java index a6f4514..70be5f1 100644 --- a/finish/src/test/java/io/openliberty/guides/application/SystemLoadBeanTest.java +++ b/finish/src/test/java/io/openliberty/guides/bean/SystemLoadBeanTest.java @@ -9,17 +9,16 @@ * SPDX-License-Identifier: EPL-2.0 *******************************************************************************/ // end::copyright[] -package io.openliberty.guides.application; +package io.openliberty.guides.bean; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import io.openliberty.guides.application.model.SystemLoadData; - -import org.junit.jupiter.api.BeforeEach; +import io.openliberty.guides.bean.model.SystemLoadData; public class SystemLoadBeanTest { @@ -51,8 +50,8 @@ public void testFetchSystemLoad() { int initialSize = systemLoadBean.getSystemLoads().size(); systemLoadBean.fetchSystemLoad(); int newSize = systemLoadBean.getSystemLoads().size(); - assertEquals(initialSize + 1, newSize, "System loads size should increase by 1 after fetching new data"); - + assertEquals(initialSize + 1, newSize, + "System loads size should increase by 1 after fetching new data"); } // end::testFetchSystemLoad[] diff --git a/scripts/dailyBuild.sh b/scripts/dailyBuild.sh index 3b3e215..3d80973 100644 --- a/scripts/dailyBuild.sh +++ b/scripts/dailyBuild.sh @@ -1,13 +1,19 @@ #!/bin/bash -while getopts t:d:b: flag; +while getopts t:d:j:b: flag; do case "${flag}" in t) DATE="${OPTARG}";; d) DRIVER="${OPTARG}";; - b) BUILD="${OPTARG}";; + j) JDK_LEVEL="${OPTARG}";; + *) echo "Invalid option";; esac done +if [ "$JDK_LEVEL" == "11" ]; then + echo "Test skipped because the guide does not support Java 11." + exit 0 +fi + sed -i "\#liberty-maven-plugin#ahttps://public.dhe.ibm.com/ibmdl/export/pub/software/openliberty/runtime/nightly/"$DATE"/"$DRIVER"" client/pom.xml system/pom.xml cat query/pom.xml system/pom.xml diff --git a/scripts/testApp.sh b/scripts/testApp.sh index 9ab6af0..dd19f32 100755 --- a/scripts/testApp.sh +++ b/scripts/testApp.sh @@ -10,14 +10,18 @@ set -euxo pipefail mvn -ntp -Dhttp.keepAlive=false \ -Dmaven.wagon.http.pool=false \ -Dmaven.wagon.httpconnectionManager.ttlSeconds=120 \ - -ntp -q clean package liberty:create liberty:install-feature liberty:deploy + -q clean package liberty:create liberty:install-feature liberty:deploy mvn test mvn -ntp liberty:start -# check the messages.log -# check the url +sleep 20 + +cat target/liberty/wlp/usr/servers/defaultServer/logs/messages.log || exit 1 + +status_code=$(curl -o /dev/null -s -w "%{http_code}" http://localhost:9080/index.xhtml) +[ "$status_code" -eq 200 ] || exit 1 mvn -ntp liberty:stop diff --git a/start/src/main/java/io/openliberty/guides/application/model/SystemLoadData.java b/start/src/main/java/io/openliberty/guides/bean/model/SystemLoadData.java similarity index 95% rename from start/src/main/java/io/openliberty/guides/application/model/SystemLoadData.java rename to start/src/main/java/io/openliberty/guides/bean/model/SystemLoadData.java index ea42925..967c9a4 100644 --- a/start/src/main/java/io/openliberty/guides/application/model/SystemLoadData.java +++ b/start/src/main/java/io/openliberty/guides/bean/model/SystemLoadData.java @@ -9,7 +9,7 @@ * SPDX-License-Identifier: EPL-2.0 *******************************************************************************/ // end::copyright[] -package io.openliberty.guides.application.model; +package io.openliberty.guides.bean.model; public class SystemLoadData { private String time; diff --git a/start/src/main/liberty/config/server.xml b/start/src/main/liberty/config/server.xml index a102acb..7f8bdaa 100755 --- a/start/src/main/liberty/config/server.xml +++ b/start/src/main/liberty/config/server.xml @@ -16,12 +16,6 @@ - - - - diff --git a/start/src/test/java/io/openliberty/guides/application/.gitkeep b/start/src/test/java/io/openliberty/guides/bean/.gitkeep similarity index 100% rename from start/src/test/java/io/openliberty/guides/application/.gitkeep rename to start/src/test/java/io/openliberty/guides/bean/.gitkeep