A closer look into struts-config.xml in Struts 1.x

A web application uses a deployment descriptor to initialize resources like servlets and taglibs. This deployment descriptor is formatted as an XML file and named "web.xml". Likewise, the framework uses a configuration file to initialize its own resources. These resources include ActionForms to collect input from users, ActionMappings to direct input to server-side Actions, and ActionForwards to select output pages.

Here's a simple struts-config.xml file and its different elements:

<struts-config>

<form-beans> 
  <form-bean 
       name="helloWorldForm" 
     type="com.naeemgik.common.form.HelloWorldForm"/>
</form-beans>

<action-mappings>
  <action 
       path="/helloWorld"
       input="/ByeWorld.jsp"
type="com.naeemgik.common.action.HelloWorldAction"
name="helloWorldForm"
       attribute="helloWorldForm"
       validate="true"
       scope="request"
       parameter="operation">

<forward name="success" path="/HelloWorld.jsp"/>
       <forward name="error" path="/ByeWorld.jsp"/>

  </action>
</action-mappings>

</struts-config>


In the above struts-config.xml file I have included most of the attributes of the action tag for the understanding point of view. Now let discuss each one.

The form-bean tag have HelloWorldForm which is used to hold the data. This class is extended from ActionForm.java

The acion tag attributes:

path: The action will be only and only called when the the user hit the URL of named /helloWorld

input: If vaidate is true and the form have some validation errors then the default page to display will be mentioned in this attribute.

type: Type is the Action class that will be called. It may be extended from Action.java or DispatchAction.java depending upon user scenarios. For more details may refer to Difference between Action and DispatchAction

name: The “name” attribute is actually the form-bean name.

attribute: attribute have same meaning as that of name attribute.

validate: This attribute is used whether this action needs some sort of validators or not? In case validate="false" it skips any validations.

scope: This actaully describe the action scope whether it will be request or session.

parameter: This is used only in case of DispatchAction. As dispatch Action have multiple action methods so programmer must have explicitly mention which action needs to be triggered. If you are new to Action and DispatchAction must read Difference between Action and DispatchAction

forward: This tag actually launches the corresponding JSP,page or another action depending on the forward name attribute.




2 comments:

  1. Thank you for sharing this blog. This blog will help to improve my knowledge. Struts Training in Chennai | Struts Training | Struts Training center in Chennai | Struts course in Chennai

    ReplyDelete
  2. A closer look into struts-config.xml in Struts 1.x reveals how this configuration file plays a crucial role in the framework. Developers often use Fira Code for its clean and readable font, enhancing the coding experience while working with Struts configurations.

    ReplyDelete