Password Example with JSF 2.0
If you didn’t notice from the previous example, we started a mini example series for the JSF Tag Library, so in the next couple examples, we are going to deal with several simple, but quite useful JSF tags. Today, we ‘ll deal with a password field. While on JSF, we can use the following tag, in order to render an HTML input of a password field: <h:inputSecret>
To get the meaning, imagine that the fore-mentioned xhtml’s tag is equal to HTML’s <input type="password">
. So, let’s get into the full example.
1. Managed Bean
Here is our simple Managed Bean, which handles the password.
UserBean.java
package com.javacodegeeks.enterprise.jsf.password; import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean @SessionScoped public class UserBean implements Serializable{ private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2. Our Pages
As in the previous example, we need two separate pages; Let’s have a look at them:
index.xhtml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>JSF Password Example</title> </h:head> <h:body> <h1>JSF 2.0 Password Example</h1> <h:form> Password : <h:inputSecret value="#{userBean.password}" /> <h:commandButton value="Submit" action="response" /> </h:form> </h:body> </html>
response.xhtml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>JSF Password Example</title> </h:head> <h:body> <h1>JSF 2.0 Password Example - Response Page</h1> The password is : <h:outputText value="#{userBean.password}" /> </h:body> </html>
3. Demo
Let’s take a quick demo, by trying to access the following URL: http://localhost:8080/PasswordJSF
And after clicking the button, our response page:
This was an example of TextBox in JSF 2.0. You can also download the source code for this example: PaswordJSF