Struts

The Struts framework Valuators
adplus-dvertising
Previous Home Next

Valuators is basically used to perform validation in the project/application, it is configure into .xml file and used into:-

  • The ActionForm bean must extend ValidatorForm.
  • The form's JSP/HTML must include the "html:javascript /" tag for client side validation.
  • The Validator Framework uses two XML configuration files
  • validation.xml- This file are used to defines the validations applied to a form bean.
  • validator-rules.xml- This file are used to defines the standard validation routines, due to reusable purpose used in the validation.xml to define the specific validations.
  • //Validator mapping into struts-config.xml file
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts-config PUBLIC
    "-//Apache Software Foundation
    //DTD Struts Configuration 1.3//EN"
    "http://jakarta.apache.org/struts
    /dtds/struts-config_1_3.dtd">
    <struts-config>
    <form-beans>
     <!-- Model of your application or Beans map here -->
    </form-beans>
    <global-exceptions>
    </global-exceptions>
    <!-- Forward page map here-->
    <global-forwards>
    <forward name="welcome"  path="/Welcome.do"/>
    </global-forwards>
    <!-- Action class, success page, failure page, 
    and all the action related stuff map here -->
    <action-mappings>
    <action path="/Welcome" forward="/welcomeStruts.jsp"/>
    </action-mappings>
    	<!-- Validator only work on form or
    	where validity is required-->
    	<form-validation>
     <formset>
     <form name="welcomeStruts">
    <field property="username" depends="required">
    <msg name="required" key="error.username"/>
    		  </field>
    <field property="password" depends="required">
    <msg name="required" key="error.password"/>
    </field>
     </form>
      </formset>
    </form-validation>
    <controller processorClass="org.apache.struts
    .tiles.TilesRequestProcessor"/>
     <!--ApplicationResource file map here -->
    <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>
    

  • Exception Handler in struts:
  • When an Action's class execute a method that throws an exception, then an ExceptionHandler is define to handle the execute. Class ExceptionHandler is define in the package of org.apache.struts.action.ExceptionHandler and override the execute method. Now the execute method should proceed the Exception and return an ActionForward object, which configure your handler in struts-config.xml like this-

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- ExceptionHandler define here -->
    <global-exceptions>
        <exception  key="ABC.key"  
    	type="java.io.Exception"
    handler=" com.myapp.struts.ExceptionHandler"/>
    </global-exceptions>
    

    Above configuration element says that com.myapp.struts.ExceptionHandler.execute will be called when any Exception is thrown by an Action. and the key is a key into your message resources properties file that can be used to retrieve an error message. If the handler attribute is not specified so, the default handler stores the exception in the request attribute under the value of the Globals.EXCEPTION_KEY global key. A common use of ExceptionHandlers is to configure one for java.lang.Exception so it's called for any exception and log the exception to some data store.

    Struts are efficient more than other Framework.

    Struts framework is more efficient than other framework like Spring web WVC, JavaServer Faces and Hibernate etc.

    • The Struts is not only thread-safe but thread-dependent (instantiates each Action once and allows other requests to be threaded through the original object).
    • The Struts tag libraries provide general-purpose functionality.
    • The Struts components are reusable by the application.
    • ActionForm beans minimize subclass code and shorten subclass hierarchies.
    • The Struts localization strategies reduce the need for redundant JSPs.
    • The Struts is lightweight (5 core packages, 5 tag libraries).
    • The Struts is open source and well documented (code to be examined easily)
    • The Struts is neutral model and easier integrated with other framework like Struts with Hibernate, Struts with Spring web MCV, and Struts with JSF.
    Previous Home Next