Validar un textarea
Buenas Tardes chicos espero que se encuentren bien tengo un problema con mi aplicación debo de validar un textarea el cual cuando se encuentre vacío y le presione submit salga un mensaje en rojo diciendo que esta en blanco. Tengo los códigos pero quisiera saber en donde se encuentra el error. Lo estoy realizando en Struts 1.3. La apliccaión esta echa en netbeans y estoy utilizando glassfish.
Saludos.
AreaForm
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class AreaForm extends Action{
private String area;
private String error;
/**
* @return the area
*/
public String getArea() {
return area;
}
/**
* @param area the area to set
*/
public void setArea(String area) {
this.area = area;
}
/**
* @return the error
*/
public String getError() {
return error;
}
/**
* @param error the error to set
*/
public void setError(String error) {
this.error = error;
}
public AreaForm(){
super();
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (getArea() == null || getArea().length() < 1) {
errors.add("area", new ActionMessage("error.login.required"));
}
return errors;
}
}
AreaAction
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class AreaAction extends Action {
private static final String SUCCESS = "success";
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward(SUCCESS);
}
}
welcome.jsp
Documento: welcomeStruts.jsp
Descripción: Exito Página de la aplicación
--%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html:html xhtml="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><bean:message key="welcome.title"/></title>
</head>
<body>
<h1>Congratulations!!! Your Message have successfuly Forward</h1>
<ul>
<p>Your Message: <bean:write name="AreaForm" property="area"/></p>
</ul>
<p>Return to <a href="index.jsp"> Home</a> page</p>
</body>
</html:html>
index.jsp
Document : index.jsp
Description : First Page of Application
--%>
<!-- TagLibrary which automatic configure into web.xml and used any HTML/JSP within this project -->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html:html xhtml="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><bean:message key="welcome.title"/></title>
<html:base/>
</head>
<body>
<!-- action= parameter which call Action class, for this check your Action mapping in
struts-config.xml file -->
<html:form action="/area" method="post">
<h1><bean:message key="welcome.heading" /></h1>
<h2><bean:message key="welcome.message"/></h2>
<bean:write name="AreaForm" property="error" filter="false" />
<BR>
<!-- Add error to the page -->
<html:errors property="area"/>
<b><bean:message key="welcome.login.message" /></b>
<BR><html:textarea property="area" rows="5" cols="20" />
<BR><BR>
<html:submit value=" Froward " property="submit" />
<html:reset value=" Reset " property="reset" />
</html:form>
</body>
</html:html>
struts-config
<!-- Model(Bean) class Map here -->
<form-beans>
<!-- name="Bean class name" and type="Location of bean class" -->
<form-bean name="AreaForm" type="com.mx.inge.AreaForm"/>
</form-beans>
<global-forwards>
<forward name="welcomestruts" path="welcome.do" />
</global-forwards>
<!-- Action Class Map Here -->
<action-mappings>
<action path="/area" type="com.mx.inge.AreaAction"
name="AreaForm"
scope="request"
input="/index.jsp">
<!-- If Action class return success, then this forward call -->
<forward name="success" path="/welcomeStruts.jsp" />
<!-- If Action class return failure, then this forward call -->
<forward name="failure" path="/index.jsp" />
</action>
</action-mappings>
<!-- ApplicationResource Bundle Map here-->
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="com/myapp/struts/ApplicationResource"/>
<!-- ========================= Tiles plugin ===============================-->
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>
<!-- ========================= Validator plugin ================================= -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
web.xml
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee <a href="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
" title="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
">http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
</a> <servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
</code
application.properties
<code>
#-- ApplicationResource.properties file--
# -- standard errors --
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
# -- validator --
errors.required={0} is required
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# -- other --
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
# -- welcome --
welcome.title= R4R Tech Soft
welcome.heading=Struts Applications in Netbeans 7.1.2!
welcome.message= Struts Application, used for forward request
welcome.login.message= Write something here:
#-- UserDefine Error --
error.login.required= <font color="red">Error, Textarea can't be blank</font>
#-- End of file --
- Inicie sesión o regístrese para enviar comentarios
Sigue tu error? O se arreglo
Sigue tu error? O se arreglo con lo del XML
Que error te sale?
Ya se arreglo lo del xml.
Ya se arreglo lo delo xml. pero ahora tengo que validar un textarea segun yo ya lo tengo pero no lo hace pero podrías decirme si en jsp de index y en archivo AreaForm ves algo que yo no he visto ya que me corre la apliccaión pero no hace lo del textarea cuando esta vacio deberia de salir un mesaje indicando que esta vacio.
Crees que puedas echarme la mano con esto por favor.
Saludos.
Que hace? Nada? O manda algun
Que hace? Nada? O manda algun mensaje?
Lo que es cuando escribes en
Lo que es cuando escribes en el textarea y le das clic al boton sutmit aparece en otra pagina lo que escribiste .
Pero cuando no escribes nada dentro de el cuadro de texto y le das clic al boton submit debe de aparecer un mensaje indicando que que esta en blanco.
Eso es que hace la aplicacion.
Saludos.