Getting Started With JSF 4.0 On WildFly 27 - Mastertheboss
Getting Started With JSF 4.0 On WildFly 27 - Mastertheboss
0 on WildFly 27 - Mastertheboss
Mastertheboss
This article contains a preview of Jakarta Faces 4.0 which is an MVC framework for building user interface with Jakarta EE 10.
To get started with Jakarta EE 10, you need to download the latest release of WildFly 27 which you can use to preview Jakarta EE 10
features. At the time of writing, the latest release is WildFly 27 Alpha 5 which contains support for Jakarta EE 10 bundles.
If you are new to JSF technology, we recommend checking some tutorials available in the JSF category.
Looking at the release notes, you can find the list of enhancements added in many of its components or attributes. In this article, we
will test the following enhancements:
Before getting started, it is worth to remind you that since Jakarta EE 9 you have to update the namespace of your packages and
application resources from “javax” to “jakarta“.
In terms of application resources, make sure your HTML pages include the following schemas:
1. <html
2. xmlns:faces="jakarta.faces"
3. xmlns:ui="jakarta.faces.facelets"
4. xmlns:f="jakarta.faces.core"
5. xmlns:h="jakarta.faces.html"
6. xmlns:pt="jakarta.faces.passthrough"
7. xmlns:cc="jakarta.faces.composite"
8. xmlns:my="jakarta.faces.component"
9. xmlns:c="jakarta.tags.core"
10. xmlns:fn="jakarta.tags.functions"
11. >
Next, to run on Jakarta Faces 4.0 must include the following schema declaration in faces-config.xml:
1. <faces-config
2. xmlns="https://jakarta.ee/xml/ns/jakartaee"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
5. https://jakarta.ee/xml/ns/jakartaee/web-facesconfig_4_0.xsd"
6. version="4.0">
Facelets is a page declaration language used to create JavaServer Faces (JSF) views with XHTML. Basically, it is a visualisation
technology, responsible for the appearance of the web page. In addition to the Facelets tag libraries, JavaServer Faces and JSTL,
https://www.mastertheboss.com/java-ee/jsf/getting-started-with-jsf-4-0-on-wildfly-27/ 1/12
2024. 04. 20. 18:34 Getting started with JSF 4.0 on WildFly 27 - Mastertheboss
Facelets also supports Expression Language (EL).
You can find a getting started article about Facelets here: Facelets tutorial: Using Templates
Before JSF 4.0 the only option to use Facelets was to add the single page views in your project. You can now create your views
programmatically by extending the Class jakarta.faces.view.facelets.Facelet.
Here is a sample View which creates an HTML form with some basic components:
https://www.mastertheboss.com/java-ee/jsf/getting-started-with-jsf-4-0-on-wildfly-27/ 2/12
2024. 04. 20. 18:34 Getting started with JSF 4.0 on WildFly 27 - Mastertheboss
68.
69. @SuppressWarnings("unchecked")
70. <T> T create(String componentType) {
71. return (T) facesContext.getApplication().createComponent(facesContext, componentType, null);
72. }
73. }
74. }
As you can see, every HTML Component has a matching Class in the jakarta.faces.component.html package. They can be created
through the ComponentBuilder factory or in the standard Java way (“new Class()”). Then, you need to add the Component to the
HtmlForm or directly to the HtmlBody.
The most interesting aspect, is that you can add actionlisteners to HTML components (such as press button) directly in Java without
any Javascript coding.
1. <dependency>
2. <groupId>jakarta.faces</groupId>
3. <artifactId>jakarta.faces-api</artifactId>
4. <version>4.0.0</version>
5. </dependency>
On the other hand, you can also build the application by adding the full Jakarta EE 10 dependency:
1. <dependency>
2. <groupId>jakarta.platform</groupId>
3. <artifactId>jakarta.jakartaee-api</artifactId>
4. <version>10.0.0</version>
5. <scope>provided</scope>
6. </dependency>
When run, request the page demo.xhtml which will render our Html form:
Jakarta Faces 4. introduces a new scope for managed beans: jakarta.faces.lifecycle.ClientWindowScoped. This is not totally new for
JSF developers as it is essentially bound to the lifetime of the existing jakarta.faces.lifecycle.ClientWindow object.
1. @Named
2. @ClientWindowScoped()
3. public class SignupBean implements Serializable {
4. //
5. }
Besides the annotation, you also need to enable CLIENT_WINDOW_MODE in your web.xml otherwise you will get a
ContextNotActiveException at runtime:
1. <context-param>
2. <param-name>jakarta.faces.CLIENT_WINDOW_MODE</param-name>
3. <param-value>url</param-value>
4. </context-param>
https://www.mastertheboss.com/java-ee/jsf/getting-started-with-jsf-4-0-on-wildfly-27/ 3/12
2024. 04. 20. 18:34 Getting started with JSF 4.0 on WildFly 27 - Mastertheboss
When you reference a @ClientWindowScoped bean in your page, the request parameter “jfwid” will be created and added to the
URL, as you navigate through the pages of your application.
The lifecycle of a @ClientWindowScoped bean will terminate as soon as you open a new browser window or tab on the same URL. It
will also terminate if you try to manipulate the value fo the “jfwid” parameter during the navigation. In either case, the JSF engine will
create a new @ClientWindowScoped bean.
There is, however, a maximum number of ClientWindow instances per HTTP session, which defaults to ten per session in the Mojarra
JSF implementation. You can change the default value through the following context parameter in the web.xml file:
1. <context-param>
2. <param-name>jakarta.faces.NUMBER_OF_CLIENT_WINDOWS</param-name>
3. <param-value>50</param-value>
4. </context-param>
Uploading files is a built-in features of JSF 2.x. To upload single or multiple files, you can use the <h:inputFile /> component and bind
the File name with a CDI Bean. When using JSF 4.0 there is an additional attribute “accept” which specifies the file format which you
can use for upload. Before that, you had to include a validator to include or exclude some file formats.
Firstly, notice the html tag which contains the new jakarta namespaces to reference the JSF components.
Next, check the inputFile component which includes the accept attribute with a set of file formats.
Finally, the Form includes a commandButton which starts the File upload.
For the sake of completeness, we will include here also the UploadBean which is referenced by the view:
1. import jakarta.enterprise.inject.Model;
2. import jakarta.faces.application.FacesMessage;
3. import jakarta.faces.context.FacesContext;
4. import jakarta.servlet.http.Part;
https://www.mastertheboss.com/java-ee/jsf/getting-started-with-jsf-4-0-on-wildfly-27/ 4/12
2024. 04. 20. 18:34 Getting started with JSF 4.0 on WildFly 27 - Mastertheboss
5.
6. import java.io.*;
7.
8. @Model
9. public class UploadBean {
10. private Part part;
11.
12. public Part getPart() {
13. return part;
14. }
15.
16. public void setPart(Part part) {
17. this.part = part;
18. }
19.
20.
21. public String uploadFile() throws IOException {
22. // Extract file name from content-disposition header of file part
23. String fileName = getFileName(part);
24. String basePath = "/tmp/";
25. File outputFilePath = new File(basePath + fileName);
26. // Copy uploaded file to destination path
27. InputStream inputStream = null;
28. OutputStream outputStream = null;
29. try {
30. inputStream = part.getInputStream();
31. outputStream = new FileOutputStream(outputFilePath);
32.
33. int read = 0;
34. final byte[] bytes = new byte[1024];
35. while ((read = inputStream.read(bytes)) != -1) {
36. outputStream.write(bytes, 0, read);
37. }
38. printMessage("Success! File upload completed!");
39. } catch (IOException e) {
40. e.printStackTrace();
41. printMessage("Error! File upload error!");
42. } finally {
43. if (outputStream != null) {
44. outputStream.close();
45. }
46. if (inputStream != null) {
47. inputStream.close();
48. }
49. }
50. return null;
51. }
52.
53. private void printMessage(String message) {
54. FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, message, null);
55. FacesContext.getCurrentInstance().addMessage(null, facesMsg);
56. }
57.
58. private String getFileName(Part part) {
59. final String partHeader = part.getHeader("content-disposition");
60. for (String content : part.getHeader("content-disposition").split(";")) {
61. if (content.trim().startsWith("filename")) {
62. return content.substring(content.indexOf('=') + 1).trim()
63. .replace("\"", "");
64. }
65. }
66. return null;
67. }
68. }
The Upload bean performs the Upload through the mediation of the jakarta.servlet.http.Part, which contains a reference to the File
name.
https://www.mastertheboss.com/java-ee/jsf/getting-started-with-jsf-4-0-on-wildfly-27/ 5/12
2024. 04. 20. 18:34 Getting started with JSF 4.0 on WildFly 27 - Mastertheboss
In order to upload multiple files, one option consists in adding a set of <h:inputFile /> components and bind each one with a Servlet
Part. A simpler approach could be to use the multiple=true attribute which is available in JSF 4.0. When this attribute is set to true, you
will be able to select multiple files in one shot from the Browse button.
Here is an example:
Then, in the backing Bean, we will reference a List of Part objects and loop through them in the upload method:
1. @Model
2. public class UploadBeanMultiple {
3. private List<Part> files;
4.
5. public List<Part> getFiles() {
6. return files;
7. }
8.
9. public void setFiles(List<Part> files) {
10. this.files = files;
11. }
12.
13. public String uploadFile() throws IOException {
14.
15. for (Part part : files) {
16. String fileName = Paths.get(part.getSubmittedFileName()).getFileName().toString();
17. // Same code here
18. }
19. return null;
20. }
21.
22. }
Conclusion
This article is a walk through some Jakarta Faces 4.0 goodies. We have discussed how to create programmatically Facelets, the new
ClientWindowScope available and how to upload single or multiple files using JSF 4.0.
References: https://balusc.omnifaces.org/2021/11/whats-new-in-faces-40.html
https://www.mastertheboss.com/java-ee/jsf/getting-started-with-jsf-4-0-on-wildfly-27/ 6/12
2024. 04. 20. 18:34 Getting started with JSF 4.0 on WildFly 27 - Mastertheboss
https://www.mastertheboss.com/java-ee/jsf/getting-started-with-jsf-4-0-on-wildfly-27/ 7/12
2024. 04. 20. 18:34 Getting started with JSF 4.0 on WildFly 27 - Mastertheboss
https://www.mastertheboss.com/java-ee/jsf/getting-started-with-jsf-4-0-on-wildfly-27/ 8/12
2024. 04. 20. 18:34 Getting started with JSF 4.0 on WildFly 27 - Mastertheboss
https://www.mastertheboss.com/java-ee/jsf/getting-started-with-jsf-4-0-on-wildfly-27/ 9/12
2024. 04. 20. 18:34 Getting started with JSF 4.0 on WildFly 27 - Mastertheboss
JSF
JPA Entity Listeners
Getting started with AtlasMap
Email Address *
https://www.mastertheboss.com/java-ee/jsf/getting-started-with-jsf-4-0-on-wildfly-27/ 10/12
2024. 04. 20. 18:34 Getting started with JSF 4.0 on WildFly 27 - Mastertheboss
Search …
Follow us on Socials
Recent Posts
Top 10 Tutorials
https://www.mastertheboss.com/java-ee/jsf/getting-started-with-jsf-4-0-on-wildfly-27/ 11/12
2024. 04. 20. 18:34 Getting started with JSF 4.0 on WildFly 27 - Mastertheboss
Solving unable to create new native thread
How to solve java.lang.OutofMemoryError gc overheadlimit exceeded/
How to initialize an array in Java
How to export/import a realm in Keycloak
Keycloak OAuth/OpenId tutorial
Keycloak with Quarkus
Privacy Policy
https://www.mastertheboss.com/java-ee/jsf/getting-started-with-jsf-4-0-on-wildfly-27/ 12/12