Skip to content

Commit

Permalink
addressed dev content review
Browse files Browse the repository at this point in the history
  • Loading branch information
shin19991207 committed Nov 1, 2024
1 parent 54996a3 commit 6f3eed3
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 44 deletions.
26 changes: 13 additions & 13 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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]`<h:outputStylesheet>` and [hotspot=uiIncludeFooter file=0]`<ui:include>`, manage UI components, resource inclusion, and data binding, offering additional features beyond standard HTML; while standard elements like `<div>` and `<section>` structure the page's layout. The [hotspot=outputStylesheet file=0]`<h:outputStylesheet>` includes a CSS file to style the page. The [hotspot=uiIncludeFooter file=0]`<ui:include>` 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 `<div>` and `<section>`, 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]`<h:outputStylesheet>` tag loads a CSS file for styling, and the [hotspot=uiIncludeFooter file=0]`<ui:include>` 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.

Expand Down Expand Up @@ -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.
Expand All @@ -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`
----

Expand All @@ -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]`<h:commandButton>` 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]`<f:ajax>` 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]`<ui:repeat>` 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]`<h:outputText>`, while [hotspot=convertNumber1 hotspot=convertNumber2 file=0]`<f:convertNumber>` formats numeric values to two decimal places.
The [hotspot=systemLoadsTable file=0]`systemLoadsTable` is populated using the [hotspot=uiRepeat file=0]`<ui:repeat>` 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]`<h:outputText>`. The [hotspot=convertNumber1 hotspot=convertNumber2 file=0]`<f:convertNumber>` tag formats `cpuLoad` to seven decimal places and `memoryUsage` to two decimal places.

// =================================================================================================
// Running the application
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 0 additions & 6 deletions finish/src/main/liberty/config/server.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
<variable name="http.port" defaultValue="9080"/>
<variable name="https.port" defaultValue="9443"/>

<basicRegistry id="basic" realm="BasicRealm">
<!--
<user name="yourUserName" password="" />
-->
</basicRegistry>

<httpEndpoint id="defaultHttpEndpoint" host="*"
httpPort="${http.port}"
httpsPort="${https.port}" />
Expand Down
2 changes: 1 addition & 1 deletion finish/src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<!-- tag::outputText2[] -->
<h:outputText value="#{systemLoadData.cpuLoad == null ? '-' : systemLoadData.cpuLoad}">
<!-- tag::convertNumber1[] -->
<f:convertNumber pattern="#0.00" />
<f:convertNumber pattern="#0.0000000" />
<!-- end::convertNumber1[] -->
</h:outputText>
<!-- end::outputText2[] -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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[]

Expand Down
10 changes: 8 additions & 2 deletions scripts/dailyBuild.sh
Original file line number Diff line number Diff line change
@@ -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 "\#<artifactId>liberty-maven-plugin</artifactId>#a<configuration><install><runtimeUrl>https://public.dhe.ibm.com/ibmdl/export/pub/software/openliberty/runtime/nightly/"$DATE"/"$DRIVER"</runtimeUrl></install></configuration>" client/pom.xml system/pom.xml
cat query/pom.xml system/pom.xml

Expand Down
10 changes: 7 additions & 3 deletions scripts/testApp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 0 additions & 6 deletions start/src/main/liberty/config/server.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
<variable name="http.port" defaultValue="9080"/>
<variable name="https.port" defaultValue="9443"/>

<basicRegistry id="basic" realm="BasicRealm">
<!--
<user name="yourUserName" password="" />
-->
</basicRegistry>

<httpEndpoint id="defaultHttpEndpoint" host="*"
httpPort="${http.port}"
httpsPort="${https.port}" />
Expand Down

0 comments on commit 6f3eed3

Please sign in to comment.