Sunday, September 16, 2012

Automatic Validation in Struts


order-form.jsp

<HTML>
<HEAD><TITLE>Order</TITLE></HEAD>
<BODY BGCOLOR="#FDF5E6"><CENTER>
<%@ taglib uri="http://struts.apache.org/tags-html"  prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean"  prefix="bean" %>

<html:errors/>
<html:form action="order"
           onsubmit="return validateOrderFormBean(this);">
  <bean:message key="inputForm.firstName" />
<html:text property="firstName"/><BR>
  <html:submit value="Order Now!"/>
</html:form>
<html:javascript formName="orderFormBean"/>
</CENTER></BODY></HTML>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>DemoStruts</display-name>
  <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>
        <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
      </welcome-file-list>
</web-app>

struts-config.xml

<struts-config>
 
  <form-beans>
  
    <form-bean name="orderFormBean"
               type="omservlet.OrderFormBean"/>
  </form-beans>
  <action-mappings>
      <action path="/order"
            type="omservlet.Order"
            name="orderFormBean"
            scope="request"
            input="/order-form.jsp">
      <forward name="success"
               path="/WEB-INF/results/order-confirmation.jsp"/>
    </action>
  </action-mappings>
   <message-resources parameter="MessageResources"/>

  <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>

MessageResources.properties
# -- Custom messages for this application --
inputForm.firstName=First name
# -- Standard errors --
errors.header=<UL>
errors.prefix=<LI><B><FONT COLOR="RED">
errors.suffix=</FONT></B></LI>
errors.footer=</UL>
# -- validator --
errors.required={0} is required.
validator-rules.xml
<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">

<form-validation>

   <global>

      <validator name="required"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateRequired"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       javax.servlet.http.HttpServletRequest"
                  msg="errors.required"/>
   </global>

</form-validation>

Validation.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE form-validation PUBLIC
          "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
          "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>
  <formset>
    <form name="orderFormBean">
      <field property="firstName"
             depends="required">
        <arg0 key="inputForm.firstName"/>
      </field>
    </form>  </formset> </form-validation>

OrderFormBean.java
package omservlet;
import org.apache.struts.validator.*;

public class OrderFormBean extends ValidatorForm {

// setter and getter methods.

}
Order.java
package omservlet;
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class Order extends Action {
 public ActionForward execute(ActionMapping mapping,
                               ActionForm form,
                               HttpServletRequest request,
                               HttpServletResponse response)
      throws Exception {
    return(mapping.findForward("success"));
  }
}

order-confirmation.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD><TITLE>Thanks for Ordering</TITLE></HEAD>
<BODY BGCOLOR="#FDF5E6">
<CENTER>
<H1>Thanks for Ordering</H1>
</CENTER>
</BODY></HTML>