Ver Mensaje Individual
  #1 (permalink)  
Antiguo 02/04/2015, 18:19
awik
 
Fecha de Ingreso: febrero-2013
Mensajes: 78
Antigüedad: 11 años, 3 meses
Puntos: 0
Como validar inputs de una web en java fsj primefaces

Buenas tengo un crud simple

hecho en java que segui gracias a un tutorial de internet ..
El caso es que ahora solo me faltan las validaciones de 2 campos

que son el Nombre y la contraseña

en caso de Nombre , el tipo es varchar con una longitud máxima de 20 caracteres

quiero que cuando llene mas de 20 caracteres y al presionar Insertar
me salga un mensaje de error que diga

´´Solo debe ingresar menos de 20 caracteres


y Tambien como validar para que solo pueda escribir letras y no números ..



y en el campo password tambien la longitud máxima es de 20 caracteres ,
quisiera que tambien si meto mas de 20 caracteres me salga un mensaje de error
que diga ,

´´Solo debe ingresar menos de 20 caracteres ´´

y nada mas y ya llevo buscando arto y no encuentro como hacer esto tan simple en java web

estoy usando fsf con primefaces


Aqui todo lo que hay en cada archivo :



create.xhtml


Código Java:
Ver original
  1. <?xml version='1.0' encoding='UTF-8' ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml"
  4.       xmlns:h="http://xmlns.jcp.org/jsf/html">
  5.     <h:head>
  6.         <title>Facelet Title</title>
  7.     </h:head>
  8.     <h:body>
  9.         <h:form>
  10.            
  11.             Name:<h:inputText label="Nombre" value="#{empCuenta.emp.vcNick}"  required="true"/><br/>
  12.             Password:<h:inputSecret label="Contraseña" value="#{empCuenta.emp.vcClave}" required="true"/><br/>
  13.            
  14.             <h:commandButton value="Insert"
  15.                              action="#{empCuenta.insert()}"/>
  16.         </h:form>
  17.     </h:body>
  18. </html>





archivo edit.xhtml



Código Java:
Ver original
  1. <?xml version='1.0' encoding='UTF-8' ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml"
  4.       xmlns:h="http://xmlns.jcp.org/jsf/html"
  5.       xmlns:f="http://xmlns.jcp.org/jsf/core">
  6.     <h:head>
  7.         <title>Facelet Title</title>
  8.     </h:head>
  9.     <h:body>
  10.        <h:form>
  11.            <f:event type="preRenderView" listener="#{empCuenta.initUpdate}" />
  12.            Name:<h:inputText label="Nombre" value="#{empCuenta.emp.vcNick}"  required="true"/><br/>
  13.             Password:<h:inputSecret label="Contraseña" value="#{empCuenta.emp.vcClave}"  required="true"/><br/>
  14.            
  15.             <h:commandButton value="Actualizar"
  16.                              action="#{empCuenta.actualizar}">
  17.                
  18.             </h:commandButton>
  19.         </h:form>
  20.     </h:body>
  21. </html>








archivo index.xhtml
Código Java:
Ver original
  1. <?xml version='1.0' encoding='UTF-8' ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml"
  4.       xmlns:h="http://java.sun.com/jsf/html"
  5.       xmlns:f="http://java.sun.com/jsf/core"
  6.       xmlns:t="http://xmlns.jcp.org/jsf/core">
  7.      
  8.     <h:head>
  9.         <title>Facelet Title</title>
  10.     </h:head>
  11.     <h:body>
  12.        
  13.        
  14.         <center><h1><font color="red" > <h:link  outcome="create"  value="Crear una nueva cuenta"/></font> </h1></center>
  15.         <h:form>
  16.             <center>
  17.             <h:dataTable value="#{empCuenta.lst}"
  18.                          var="list"  style="border-collapse: collapse" cellpadding="7px" border="1">
  19.            
  20.            
  21.                 <h:column>
  22.                 <f:facet name="header">ID</f:facet>
  23.                 #{list.idCuenta}
  24.                 </h:column>
  25.                
  26.                
  27.                 <h:column>
  28.                 <f:facet name="header">Nombre</f:facet>
  29.                 #{list.vcNick}
  30.                 </h:column>
  31.                
  32.                
  33.            
  34.             <h:column>
  35.                 <f:facet name="header">Actions</f:facet>
  36.                 <h:commandLink value="Remove"
  37.                                action="#{empCuenta.remove(list)}"
  38.                                onclick="return confirm('¿Estas seguro de eliminar este registro?')" >  
  39.                     <f:ajax render="@form" execute="@form" />
  40.                 </h:commandLink>
  41.                
  42.                 -
  43.                 <h:commandLink value="Editar" action="#{empCuenta.edit(list)}"/>
  44.             </h:column>
  45.                    
  46.                  
  47.             </h:dataTable>
  48.                
  49.                 </center>
  50.         </h:form>
  51.     </h:body>
  52. </html>





archivo CuentaController.java



Código Java:
Ver original
  1. package controller;
  2.  
  3. import model.*;
  4. import java.util.*;
  5. import javax.faces.bean.ManagedBean;
  6. import javax.faces.bean.SessionScoped;
  7. import entity.*;
  8. import java.io.Serializable;
  9. import javax.faces.bean.ViewScoped;
  10. import javax.faces.context.FacesContext;
  11.  
  12. @ManagedBean(name = "empCuenta")
  13. @ViewScoped
  14. public class CuentaController implements Serializable {
  15.  
  16.     public List<Cuenta> lst = new ArrayList<Cuenta>();
  17.     Cuenta_dao dao;
  18.     private Cuenta emp = new Cuenta();
  19.  
  20.     public CuentaController() {
  21.         dao = new Cuenta_dao();
  22.         lst = dao.getAll();
  23.     }
  24.  
  25.     public void initUpdate() {
  26.         if (!FacesContext.getCurrentInstance().isPostback()) {
  27.             emp = (Cuenta) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("idCuenta");
  28.             System.out.println("" + emp);
  29.         }
  30.  
  31.     }
  32.  
  33.     /**
  34.      * GETTER AND SETTERS *
  35.      */
  36.     public List<Cuenta> getLst() {
  37.  
  38.         return lst;
  39.  
  40.         //Cuenta_dao dao = new Cuenta_dao();
  41.         //return dao.getAll();
  42.     }
  43.  
  44.     public void setLst(List<Cuenta> lst) {
  45.         this.lst = lst;
  46.     }
  47.  
  48.     public Cuenta getEmp() {
  49.         return emp;
  50.     }
  51.  
  52.     public void setEmp(Cuenta emp) {
  53.         this.emp = emp;
  54.     }
  55.  
  56.     public void remove(Cuenta em) {
  57.         Cuenta_dao dao = new Cuenta_dao();
  58.         dao.remove(em);
  59.         lst = dao.getAll();
  60.     }
  61.  
  62.     public String insert() {
  63.         Cuenta_dao dao = new Cuenta_dao();
  64.         dao.create(emp);
  65.         return "index";
  66.     }
  67.  
  68.     public String edit(Cuenta em) {
  69.         FacesContext.getCurrentInstance().getExternalContext().getFlash().put("idCuenta", em);
  70.         return "edit?faces-redirect=true;";
  71.      
  72.     }
  73.  
  74.     public String actualizar() {
  75.         Cuenta_dao dao = new Cuenta_dao();
  76.         dao.edit(emp);
  77.         return "index";
  78.        
  79.     }
  80.  
  81. }









archivo Cuenta_dao.java



Código Java:
Ver original
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package model;
  7.  
  8. import java.util.*;
  9. import entity.*;
  10. import org.hibernate.*;
  11.  
  12. public class Cuenta_dao {
  13.    
  14.    
  15.     public List<Cuenta> getAll()
  16.     {
  17.        
  18.         Session s=HibernateUtil.getSessionFactory().getCurrentSession();
  19.        
  20.         List<Cuenta> lst = new ArrayList<>();
  21.        
  22.         try
  23.         {
  24.             s.beginTransaction();
  25.             lst =s.createCriteria(Cuenta.class).list();
  26.             s.getTransaction().commit();
  27.            
  28.         }
  29.         catch (Exception e) {
  30.             e.printStackTrace();
  31.             s.getTransaction().rollback();
  32.         }
  33.        
  34.         return lst;
  35.     }
  36.    
  37.    
  38.     public void create(Cuenta em)
  39.     {
  40.         Session s=HibernateUtil.getSessionFactory().getCurrentSession();
  41.        
  42.          try
  43.         {
  44.             s.beginTransaction();
  45.             s.save(em);
  46.             s.getTransaction().commit();
  47.            
  48.         }
  49.         catch (Exception e) {
  50.             e.printStackTrace();
  51.             s.getTransaction().rollback();
  52.         }
  53.        
  54.        
  55.     }
  56.    
  57.    
  58.    
  59.    
  60.      public void edit(Cuenta em)
  61.     {
  62.         Session s=HibernateUtil.getSessionFactory().getCurrentSession();
  63.        
  64.          try
  65.         {
  66.             s.beginTransaction();
  67.             s.update(em);
  68.             s.getTransaction().commit();
  69.            
  70.         }
  71.         catch (Exception e) {
  72.             e.printStackTrace();
  73.             s.getTransaction().rollback();
  74.         }
  75.        
  76.        
  77.     }
  78.      
  79.      
  80.      
  81.      
  82.      
  83.      
  84.       public void remove(Cuenta em)
  85.     {
  86.         Session s=HibernateUtil.getSessionFactory().getCurrentSession();
  87.        
  88.          try
  89.         {
  90.             s.beginTransaction();
  91.             s.delete(em);
  92.             s.getTransaction().commit();
  93.            
  94.         }
  95.         catch (Exception e) {
  96.             e.printStackTrace();
  97.             s.getTransaction().rollback();
  98.         }
  99.        
  100.        
  101.     }
  102.      
  103.      
  104.      
  105.    }





En que lugar del archivo y que código tendria que agregar para hacer las validaciones que quiero ??

ojala alguien pueda ayudarme

esto sale cuando presiono insert y no ingrese nada ..


Última edición por awik; 02/04/2015 a las 18:43