Java Server Faces (JSF) — Advancing Your Knowledge
In my last blog concerning JSF we had an introduction into what JSF actually is, and also learned a bit about the base structure. In this blog, we will be getting a little more advanced in our knowledge of JSF. We will also be getting into the uses of JSF and how it can help speed up and streamline your web development.
How do I use it?
To access the JSF library and the functionality that goes along with it, you must first make sure that your project has JSF framework support. In my previous blog, I go over how this can be done with a project in Netbeans IDE. You must also declare a namespace so that you can call functions. This is done in the <HTML> tag near the top of your .xhtml file as shown below in bold:
<html xmlns=”http://www.w3.org/1999/xhtml"
xmlns:h=”http://xmlns.jcp.org/jsf/html">
<head>
Once you have this declaration you may use <h: … > to create html components such as forms or textboxes, and add the JSF functionality to them. Using JSF functionality, you have greater control over what is output to the user. For example, if you are printing a string out on a web page, you can allow any html tags included in the string to be read as actual html tags, and not raw text from the string. An example of this is shown below.
If you were to just have the string normally printed out using this code snippet:
<h4><h:outputText value=”#{UserNumberBean.response}”/></h4>It would output something like this:
<p>Sorry, 3 isn’t it.</p>
<p>Guess again…</p>
While adding this simple line (in bold):
<h4><h:outputText escape=”false” value=”#{UserNumberBean.response}”/></h4>Allows the web page to interpret the <p> tags as actual html tags rather than text, which would output something like this:
Sorry, 3 isn’t it.
Guess again…
Managed Beans
Using what are called “Managed Beans” is another integral part of utilizing the full power of JSF. You would use a JSF Managed Bean in place of a Servlet that would provide java functionality to your web application. Using a Managed Bean allows you to call methods such as Getters and Setters for variables without writing multiple lines to access that variable. Below is an example.
This piece of code (in bold):
<h4><h:outputText escape=”false” value=”#{UserNumberBean.response}”/></h4>Calls the getter method in a Manged Bean called “UserNumberBean” for the String variable “response”, and prints the value out to the web page. This is extremely useful, as there is no need to write multiple lines to access the session, retrieve the variable, parse it to the correct datatype, and then print it to the page. It is all done in one small line. This is the power of JSF; allowing you to do more with less.
Conclusion
In this blog post, we went over how to access the library once you have set up your project to have JSF support. We also went over a couple of examples to illustrate some of the functionality that comes with JSF. In the next installment of the JSF saga we will be looking at JSF templates, which allow you even greater control over the styling and functionality of the web page, as well as reducing the number of lines needed to be coded.
For more information, please check out: https://netbeans.org/kb/docs/web/jsf20-intro.html