viernes, 19 de noviembre de 2010

STRUTS1 VALIDACION FORMULARIO

Ahora nuestro formulario debe incluir la cantidad de productos a pedir, y esta cantidad se va a validar

modificamos la vista para incluir el nuevo campo "cantidad"

....
<struts-html:form action="/recibeFormularioPedido"
>
<struts-html:text property="nombreProducto" />
<struts-html:text property="cantidad" />
<struts-html:submit value="pedir" />
</struts-html:form>
....



y modificamos la clase java que representa el formulario FormularioPedido



public class FormularioPedido extends ActionForm {

String nombreProducto;

public String getNombreProducto() {
return nombreProducto;
}

public void setNombreProducto(String nombreProducto) {
this.nombreProducto = nombreProducto;
}


Integer cantidad;

public Integer getCantidad() {
return cantidad;
}

public void setCantidad(Integer cantidad) {
this.cantidad = cantidad;
}

}



Implementamos la validacion del formulario en la clase FormularioPedido

Es decir sobreescribimos el metodo de la clase padre ActionForm
public ActionErrors validate(ActionMapping mapping, HttpServletRequest req)


package es.cea.struts1;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class FormularioPedido extends ActionForm {

String nombreProducto;

public String getNombreProducto() {
return nombreProducto;
}

public void setNombreProducto(String nombreProducto) {
this.nombreProducto = nombreProducto;
}


Integer cantidad;

public Integer getCantidad() {
return cantidad;
}

public void setCantidad(Integer cantidad) {
this.cantidad = cantidad;
}

public ActionErrors validate(ActionMapping mapping, HttpServletRequest req) {

ActionErrors ae = new ActionErrors();
Integer cantidad = null;
try {
if (req.getParameter("cantidad") != null) {
cantidad = new Integer(req.getParameter("cantidad"));
}
} catch (NumberFormatException e) {
// si no se puede convertir a Integer el dato incluido por el usuario entonces
// se incluye un actionerror con una clave de mensaje de error
// dicha clave de mensaje se interpretara segun el fichero I18N.properties
// que se colocara en el src/main/resources y se declara en el struts-config.xml

ae.add("cantidad", new ActionMessage("error.cantidad"));
}
return ae;
}

}


Incluimos en la vista la etiqueta <
html:errors property="laPropiedad"/> para la visualizacion de errores de cada componente del formulario

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
prefix="struts-html"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<h1>Formularios con Struts: envio de pedido de producto</h1>
<struts-html:form action="/recibeFormularioPedido">
<struts-html:text property="nombreProducto" />
<struts-html:text property="cantidad" />
<struts-html:submit value="pedir" />
</struts-html:form>
<struts-html:errors property="cantidad" />

</body>
</html>


Por último indicamos en el struts-config que el formulario debe ser validado indicando en la acción correspondiente los atributos validate="true" input="/URL-DE-ENTTRADA-DE-FORMULARIO"

atencion se incluye el nombre de fichero de internacionalizacion I18N (estará en el src/main/resources/I18N.properties)



<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="pedidoForm" type="es.cea.struts1.FormularioPedido"></form-bean>
</form-beans>
<action-mappings>
<action path="/helloStruts" type="es.cea.struts1.HelloWorldAction">
<forward name="ok" path="/helloWorldStruts.jsp">
</forward>
</action>
<action path="/formPedidoProducto" type="es.cea.struts1.FormularioPedidoAction" name="pedidoForm">
<forward name="formularioNuevo" path="/formPedidoProductoStruts.jsp">
</forward>
</action>
<action path="/recibeFormularioPedido" type="es.cea.struts1.RecepcionFormularioPedidoAction"
name="pedidoForm" validate="true" input="/formPedidoProductoStruts.jsp">
<forward name="pedidoRecibidoOK" path="/recepcionFormPedidoProductoStruts.jsp"/>
</action>
</action-mappings>
<message-resources parameter="I18N" null="false"></message-resources>
</struts-config>



el fichero I18N.properties

error.cantidad=debe introducir un numero para la cantidad

No hay comentarios:

Publicar un comentario