Valido para la version 1.2.7
the Validator plug- in
To enable the validator plug-in open the file struts-config.xml and make sure that following line is present in the file.
| <!-- 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> |
Creating Message Resources
Message resources are used by the Validator Framework to generate the validation error messages. In our application we need to define the messages for name, Address and E-mail address. Open the Struts\strutstutorial\web\WEB-INF\MessageResources.properties file and add the following lines:
AddressForm.name=Name
AddressForm.address=Address
AddressForm.emailAddress=E-mail address
Developing Validation rules
In this application we are adding only one validation that the fields on the form should not be blank. Add the following code in the validation.xml.
| <!-- Address form Validation--> <form name="AddressForm"> <field property="name" depends="required"> <arg key="AddressForm.name"/> </field> <field property="address" depends="required"> <arg key="AddressForm.address"/> </field> <field property="emailAddress" depends="required"> <arg key="AddressForm.emailAddress"/> </field> </form> |
en el codigo del html
<html:form action="/AddressJavascriptValidation" method="post" onsubmit="return validateAddressForm(this);">
<!-- Begin Validator Javascript Function-->
<html:javascript formName="AddressForm"/>
<!-- End of Validator Javascript Function-->
struts-config
scope="request"
validate="true"
en el validator.xml
<form name="logonForm">
<field
property="username"
depends="required">
<arg key="logonForm.username"/>
</field>
<field
property="password"
depends="required,mask">
<arg key="logonForm.password"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9a-zA-Z]*$</var-value>
</var>
</field>
Necesitamos la libreria, y sus prerequisitos.
Libreria:
http://displaytag.sourceforge.net/10/download.html
Necesitamos los prerequisitos que son estas librerias:
http://jakarta.apache.org/commons/collections
http://jakarta.apache.org/commons/lang
http://jakarta.apache.org/commons/logging
<%@page import="java.sql.*"%>
<%@page import="javax.sql.*"%>
<%@page import="java.util.*"%>
<%@page import="javax.naming.*"%>
<%@page import="javax.servlet.*"%>
<%@page import="org.apache.commons.beanutils.*"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>
<jsp:useBean id="db" scope="request" class="SQLBean.DbBean" />
<jsp:setProperty name="db" property="*" />
ResultSet rs = null ;
ResultSetMetaData rsmd = null ;
int numColumns ;
int i;
%>
<%
String password=request.getParameter("password");
<head>
<title>Bienvenido</title>
</head>
<body>
<p> Hola y Bienvenido, <%=login%> <%=password%> <p>
<p align="center"><font size="3" color="#000080">Sus datos introducidos han sido correctos </font></p>
</body>
<%
db.connect();
try {
rs = db.execSQL("select * from alta");
}catch(SQLException e) {
throw new ServletException("Your query is not working", e);
}
} */
%>
<%
//while(rs.next()) {
RowSetDynaClass resultSet = new RowSetDynaClass(rs, false);
db.close();
request.setAttribute("results", resultSet);
%>
<display:table id="result" name="requestScope.results.rows">
</display:table>
<BR>
<%
//}
%>
<BR>
</html>
