Está en la página 1de 5

Persona

package managedbeans;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;

import javax.faces.bean.RequestScoped;

@ManagedBean

@RequestScoped

public class Persona implements Serializable {

private static final long serialVersionUID = -3887793502548313687L;

private String nombre;

private String telefono;

private int edad;

public Persona() {

// TODO Auto-generated constructor stub

public String getNombre() {

return nombre;

public void setNombre(String nombre) {

this.nombre = nombre;

}
public String getTelefono() {

return telefono;

public void setTelefono(String telefono) {

this.telefono = telefono;

public int getEdad() {

return edad;

public void setEdad(int edad) {

this.edad = edad;

Index

<!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:ui="http://java.sun.com/jsf/facelets"

xmlns:f="http://java.sun.com/jsf/core"

xmlns:h="http://java.sun.com/jsf/html">

<h:head>Menú principal</h:head>
<body>

<p><a href="f_persona.xhtml" >Formulario con validaciones</a></p>

</body>

</html>

Formulario Persona

<!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:ui="http://java.sun.com/jsf/facelets"

xmlns:f="http://java.sun.com/jsf/core"

xmlns:h="http://java.sun.com/jsf/html">

<h:head>Formulario de personas con validaciones</h:head>

<body>

<p>Componentes de validación</p>

<h:form>

<h:panelGrid columns="3">

<h:outputLabel value="Nombre :" for="nombre" ></h:outputLabel>

<h:inputText value="#{persona.nombre}" id="nombre" required="true" >

<f:validateLength minimum="3"/>

</h:inputText>

<h:message for="nombre"/>

<h:outputLabel value="Telefono :" for="telefono" ></h:outputLabel>

<h:inputText value="#{persona.telefono}" id="telefono" required="true" >

<f:validateLength minimum="9" maximum="11" />

</h:inputText>

<h:message for="telefono"/>

<h:outputLabel value="Edad :" for="edad" ></h:outputLabel>

<h:inputText value="#{persona.edad}" id="edad" required="true" >


<f:validateLongRange minimum="18" maximum="67"/>

</h:inputText>

<h:message for="edad"/>

<h:commandButton action="ver" value="Mostrar información persona" />

</h:panelGrid>

</h:form>

</body>

</html>

VER PERSONA

<!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:ui="http://java.sun.com/jsf/facelets"

xmlns:f="http://java.sun.com/jsf/core"

xmlns:h="http://java.sun.com/jsf/html">

<h:head>Mostrar informaicón de persona</h:head>

<body>

</body>

</html>

Dentro de la clase persona


public void compruebaEmail(FacesContext context,
UIComponent componente,
Object valor) {
//Lógica de validación
String email = (String) valor;
if (email.indexOf("@")==-1 || email.indexOf(".") ==-1) {
throw new ValidatorException(new FacesMessage("Email no válido"));
}
}

También podría gustarte