Tuesday, January 28, 2014

JSF 2.0 Example Code

Excerpted from  http://balusc.blogspot.com/2011/01/jsf-20-tutorial-with-eclipse-and.html 


package com.example;

import java.io.Serializable;
import java.util.Date;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

public class User implements Serializable {

 private static final long serialVersionUID = 1L;
 private Long id;
 @NotNull (message = "Please enter username")
 private String username;
 @NotNull (message = "Please enter password")
 @Pattern(regexp = ".*(?=.{8,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).*", message = "Password is not strong enough")
 private String password;
 @NotNull (message = "Please enter email")
 @Pattern(regexp = "([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)", message = "Email is not in valid format")
 private String email;
 private Date birthdate;

 public Long getId() {
  return id;

 public void setId(Long id) {
  this.id = id;

 public String getUsername() {
  return username;

 public void setUsername(String username) {
  this.username = username;

 public String getPassword() {
  return password;

 public void setPassword(String password) {
  this.password = password;

 public String getEmail() {
  return email;

 public void setEmail(String email) {
  this.email = email;

 public Date getBirthdate() {
  return birthdate;

 public void setBirthdate(Date birthdate) {
  this.birthdate = birthdate;


Backing Bean:

package com.example;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

public class Register implements Serializable {

 private static final long serialVersionUID = 1L;
 private User user;

    public void init() {
        user = new User();
    public void submit() {
        FacesMessage message = new FacesMessage("Registration succesful!");
        FacesContext.getCurrentInstance().addMessage(null, message);
    public User getUser() {
        return user;


<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets">
    <title>Register User</title>
    <h:form id="form">
        <h:panelGrid columns="3">
            <h:outputLabel for="username">Username</h:outputLabel>
            <h:inputText id="username" label="Username" value="#{register.user.username}">
                <f:ajax event="blur" render="usernameMessage" />
            <h:message id="usernameMessage" for="username" />

            <h:outputLabel for="password">Password</h:outputLabel>
            <h:inputSecret id="password" label="Password" value="#{register.user.password}"  redisplay="true">
                <f:ajax event="blur" render="passwordMessage" />
            <h:message id="passwordMessage" for="password" />

            <h:outputLabel for="email">Email</h:outputLabel>
            <h:inputText id="email" label="Email" value="#{register.user.email}" >
                <f:ajax event="blur" render="emailMessage" />
            <h:message id="emailMessage" for="email" />

            <h:outputLabel for="birthdate">Birthdate (yyyy-MM-dd)</h:outputLabel>
            <h:inputText id="birthdate" converterMessage="Please enter date in yyyy-MM-dd format" label="Birthdate" value="#{register.user.birthdate}">
                <f:convertDateTime pattern="yyyy-MM-dd" />
                <f:ajax event="blur" render="birthdateMessage" />
            <h:message id="birthdateMessage" for="birthdate" />

            <h:panelGroup />
            <h:commandButton value="Register" action="#{register.submit}">
                <f:ajax execute="@form" render="@form" />
            <h:messages globalOnly="true" layout="table" />

No comments:

Using a Console Cable (USB TTL Serial Cable) with Raspberry Pi 3

A console cable provides terminal access to Raspberry Pi from say a laptop host without requiring to connect a monitor to Pi's HDMI port...