Skip to content

Commit

Permalink
Address dev content review (#8)
Browse files Browse the repository at this point in the history
* addressed dev content review

* Update SystemLoadBean.java

* Bump org.apache.maven.plugins:maven-surefire-plugin in /start (#9)

Bumps [org.apache.maven.plugins:maven-surefire-plugin](https://github.com/apache/maven-surefire) from 3.5.0 to 3.5.1.
- [Release notes](https://github.com/apache/maven-surefire/releases)
- [Commits](apache/maven-surefire@surefire-3.5.0...surefire-3.5.1)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-surefire-plugin
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump org.apache.maven.plugins:maven-surefire-plugin in /finish (#10)

Bumps [org.apache.maven.plugins:maven-surefire-plugin](https://github.com/apache/maven-surefire) from 3.5.0 to 3.5.1.
- [Release notes](https://github.com/apache/maven-surefire/releases)
- [Commits](apache/maven-surefire@surefire-3.5.0...surefire-3.5.1)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-surefire-plugin
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Addressed #8 (comment)

* addressed #8 (comment)

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
shin19991207 and dependabot[bot] authored Nov 6, 2024
1 parent 54996a3 commit 95652cb
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 74 deletions.
30 changes: 15 additions & 15 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, Jakarta Contexts and Dependency Injection, and Jakarta Expression Language.
: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 offer additional features beyond standard HTML, such as managing UI components, including resources, and binding data. 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,12 +191,12 @@ 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`
----

index.xhtml
[source, xhtml, linenums, role='code_column']
[source, xhtml, linenums, role='code_column hide_tags=copyright']
----
include::finish/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 file=0]`index.xhtml` 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
2 changes: 1 addition & 1 deletion finish/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.0</version>
<version>3.5.1</version>
</plugin>
</plugins>
</build>
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 Expand Up @@ -55,17 +57,17 @@ public void init() {
// tag::fetchSystemLoadMethod[]
public void fetchSystemLoad() {
String time = Calendar.getInstance().getTime().toString();

double cpuLoad = OS.getCpuLoad() * 100;

long heapMax = MEM.getHeapMemoryUsage().getMax();
long heapUsed = MEM.getHeapMemoryUsage().getUsed();
double memoryUsage = heapUsed * 100.0 / heapMax;

SystemLoadData data = new SystemLoadData(time, cpuLoad, memoryUsage);

systemLoads.add(data);
}
}
// end::fetchSystemLoadMethod[]

// tag::getSystemLoads[]
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
45 changes: 35 additions & 10 deletions finish/src/main/webapp/WEB-INF/includes/footer.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,23 @@
</p>
</div>
<div id="footer_round_links_container">
<a id="footer_github_link" class="footer_round_btn" href="https://github.com/OpenLiberty/" target="_blank" rel="noopener" aria-label="Github"></a>
<a id="footer_stackoverflow_link" class="footer_round_btn" href="https://stackoverflow.com/questions/tagged/open-liberty" target="_blank" rel="noopener" aria-label="Open Liberty questions on Stack Overflow"></a>
<a id="footer_groupsio_link" class="footer_round_btn" href="https://groups.io/g/openliberty" target="_blank" rel="noopener" aria-label="Open Liberty Groups.io"></a>
<a id="footer_gitter_link" class="footer_round_btn" href="https://app.gitter.im/#/room/#OpenLiberty_development:gitter.im" target="_blank" rel="noopener" aria-label="Open Liberty Gitter"></a>
<a id="footer_github_link" class="footer_round_btn"
href="https://github.com/OpenLiberty/"
target="_blank" rel="noopener" aria-label="Github">
</a>
<a id="footer_stackoverflow_link" class="footer_round_btn"
href="https://stackoverflow.com/questions/tagged/open-liberty"
target="_blank" rel="noopener"
aria-label="Open Liberty questions on Stack Overflow">
</a>
<a id="footer_groupsio_link" class="footer_round_btn"
href="https://groups.io/g/openliberty"
target="_blank" rel="noopener" aria-label="Open Liberty Groups.io">
</a>
<a id="footer_gitter_link" class="footer_round_btn"
href="https://app.gitter.im/#/room/#OpenLiberty_development:gitter.im"
target="_blank" rel="noopener" aria-label="Open Liberty Gitter">
</a>
</div>
</div>
</div>
Expand All @@ -41,26 +54,38 @@
<div id="footer_bottom_left_section">
<p id="footer_copyright">&#169; IBM Corp 2024</p>
<p class="vertical_bar">|</p>
<a id="footer_privacy_policy_link" href="https://www.ibm.com/privacy/" target="_blank" rel="noopener" class="left_footer_link">
<a id="footer_privacy_policy_link"
href="https://www.ibm.com/privacy/"
target="_blank" rel="noopener" class="left_footer_link">
<h:outputText value="Privacy policy" />
</a>
<p class="vertical_bar">|</p>
<a id="footer_license_link" href="https://github.com/OpenLiberty/open-liberty/blob/release/LICENSE" target="_blank" rel="noopener" class="left_footer_link">
<a id="footer_license_link"
href="https://github.com/OpenLiberty/open-liberty/blob/release/LICENSE"
target="_blank" rel="noopener" class="left_footer_link">
<h:outputText value="License" />
</a>
<p class="vertical_bar">|</p>
<a id="footer_logos_link" href="https://github.com/OpenLiberty/logos" target="_blank" rel="noopener" class="left_footer_link">
<a id="footer_logos_link"
href="https://github.com/OpenLiberty/logos"
target="_blank" rel="noopener" class="left_footer_link">
<h:outputText value="Logos" />
</a>
</div>
<div id="footer_bottom_right_section">
<a id="footer_docs_link" href="/docs" aria-label="Open Liberty Docs" class="right_footer_link blue_link_light_background">
<a id="footer_docs_link"
href="/docs" aria-label="Open Liberty Docs"
class="right_footer_link blue_link_light_background">
<h:outputText value="Docs" />
</a>
<a id="footer_blog_link" href="/blog" aria-label="Open Liberty Blog" class="right_footer_link blue_link_light_background">
<a id="footer_blog_link"
href="/blog" aria-label="Open Liberty Blog"
class="right_footer_link blue_link_light_background">
<h:outputText value="Blog" />
</a>
<a id="footer_support_link" href="/support" aria-label="Open Liberty Support" class="right_footer_link blue_link_light_background">
<a id="footer_support_link"
href="/support" aria-label="Open Liberty Support"
class="right_footer_link blue_link_light_background">
<h:outputText value="Support" />
</a>
</div>
Expand Down
6 changes: 4 additions & 2 deletions finish/src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
<h2>System Loads</h2>
<!-- tag::commandButton[] -->
<!-- tag::commandButtonAction[] -->
<h:commandButton id="refreshButton" value="" action="#{systemLoadBean.fetchSystemLoad}" styleClass="refreshButton" title="Refresh system load data">
<h:commandButton id="refreshButton" styleClass="refreshButton" value=""
title="Refresh system load data"
action="#{systemLoadBean.fetchSystemLoad}" >
<!-- end::commandButtonAction[] -->
<!-- tag::ajaxTag[] -->
<f:ajax render="systemLoadForm" />
Expand Down Expand Up @@ -82,7 +84,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
Loading

0 comments on commit 95652cb

Please sign in to comment.