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<?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://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
Name:<h
:inputText label
="Nombre" value
="#{empCuenta.emp.vcNick}" required
="true"/><br
/> Password:<h:inputSecret label="Contraseña" value="#{empCuenta.emp.vcClave}" required="true"/><br/>
<h:commandButton value="Insert"
action="#{empCuenta.insert()}"/>
</h:form>
</h:body>
</html>
archivo edit.xhtml
Código Java:
Ver original<?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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<f:event type="preRenderView" listener="#{empCuenta.initUpdate}" />
Name:<h
:inputText label
="Nombre" value
="#{empCuenta.emp.vcNick}" required
="true"/><br
/> Password:<h:inputSecret label="Contraseña" value="#{empCuenta.emp.vcClave}" required="true"/><br/>
<h:commandButton value="Actualizar"
action="#{empCuenta.actualizar}">
</h:commandButton>
</h:form>
</h:body>
</html>
archivo index.xhtml
Código Java:
Ver original<?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"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:t="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<center><h1><font color="red" > <h:link outcome="create" value="Crear una nueva cuenta"/></font> </h1></center>
<h:form>
<center>
<h:dataTable value="#{empCuenta.lst}"
var="list" style="border-collapse: collapse" cellpadding="7px" border="1">
<h:column>
<f:facet name="header">ID</f:facet>
#{list.idCuenta}
</h:column>
<h:column>
<f:facet name="header">Nombre</f:facet>
#{list.vcNick}
</h:column>
<h:column>
<f:facet name="header">Actions</f:facet>
<h:commandLink value="Remove"
action="#{empCuenta.remove(list)}"
onclick="return confirm('¿Estas seguro de eliminar este registro?')" >
<f:ajax render="@form" execute="@form" />
</h:commandLink>
-
<h:commandLink value="Editar" action="#{empCuenta.edit(list)}"/>
</h:column>
</h:dataTable>
</center>
</h:form>
</h:body>
</html>
archivo CuentaController.java
Código Java:
Ver originalpackage controller;
import model.*;
import java.util.*;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import entity.*;
import java.io.Serializable;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
@ManagedBean(name = "empCuenta")
@ViewScoped
public List<Cuenta> lst = new ArrayList<Cuenta>();
Cuenta_dao dao;
private Cuenta emp = new Cuenta();
public CuentaController() {
dao = new Cuenta_dao();
lst = dao.getAll();
}
public void initUpdate() {
if (!FacesContext.getCurrentInstance().isPostback()) {
emp = (Cuenta) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("idCuenta");
}
}
/**
* GETTER AND SETTERS *
*/
public List<Cuenta> getLst() {
return lst;
//Cuenta_dao dao = new Cuenta_dao();
//return dao.getAll();
}
public void setLst(List<Cuenta> lst) {
this.lst = lst;
}
public Cuenta getEmp() {
return emp;
}
public void setEmp(Cuenta emp) {
this.emp = emp;
}
public void remove(Cuenta em) {
Cuenta_dao dao = new Cuenta_dao();
dao.remove(em);
lst = dao.getAll();
}
Cuenta_dao dao = new Cuenta_dao();
dao.create(emp);
return "index";
}
public String edit
(Cuenta em
) { FacesContext.getCurrentInstance().getExternalContext().getFlash().put("idCuenta", em);
return "edit?faces-redirect=true;";
}
Cuenta_dao dao = new Cuenta_dao();
dao.edit(emp);
return "index";
}
}
archivo Cuenta_dao.java
Código Java:
Ver original/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package model;
import java.util.*;
import entity.*;
import org.hibernate.*;
public class Cuenta_dao {
public List<Cuenta> getAll()
{
Session s=HibernateUtil.getSessionFactory().getCurrentSession();
List<Cuenta> lst = new ArrayList<>();
try
{
s.beginTransaction();
lst =s.createCriteria(Cuenta.class).list();
s.getTransaction().commit();
}
e.printStackTrace();
s.getTransaction().rollback();
}
return lst;
}
public void create(Cuenta em)
{
Session s=HibernateUtil.getSessionFactory().getCurrentSession();
try
{
s.beginTransaction();
s.save(em);
s.getTransaction().commit();
}
e.printStackTrace();
s.getTransaction().rollback();
}
}
public void edit(Cuenta em)
{
Session s=HibernateUtil.getSessionFactory().getCurrentSession();
try
{
s.beginTransaction();
s.update(em);
s.getTransaction().commit();
}
e.printStackTrace();
s.getTransaction().rollback();
}
}
public void remove(Cuenta em)
{
Session s=HibernateUtil.getSessionFactory().getCurrentSession();
try
{
s.beginTransaction();
s.delete(em);
s.getTransaction().commit();
}
e.printStackTrace();
s.getTransaction().rollback();
}
}
}
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 ..