JSF password example
A full JSF 2 example to render a password input field via <h:inputSecret /> tag.1. Managed Bean
A simple managed bean, with a “password” property.package com.mkyong.form; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import java.io.Serializable; @ManagedBean @SessionScoped public class UserBean implements Serializable { private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2. View Page
Two pages for the demonstration.demo.xhtml – render a password field via “h:inputSecret”, button via “h:commandButton”, if the button is clicked, password value will be submitted to the “userBean.password’ property via setPassword() method, and forward to “user.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:body> <h1>JSF 2 password example</h1> <h:form> Password : <h:inputSecret value="#{userBean.password}" /> <h:commandButton value="Submit" action="user" /> </h:form> </h:body> </html>
<?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:body> <h1>JSF 2 password example</h1> Password : <h:outputText value="#{userBean.password}" /> </h:body> </html>
3. Demo
URL : http://localhost:8080/JavaServerFaces/Display “demo.xhtml” page
Comments
Post a Comment