Configure the Action, ActionForm and Forward in the Struts Configuration File

Configure the Action, ActionForm and Forward in the Struts Configuration File

Previous Home Next

 

In this section, We will learn how to configure the subclass of Action & ActionForm and Forward the request to next view page. 

 
All Action subclasses and subclasses of ActionForm must be configure into struts-config.xml. The struts framework provides some rules and tags to configure it. Forward must be configure with in <action>tags. Which are show following example:

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.r4r.struts.LoginForm"/>
</form-beans>
<global-exceptions />
<global-forwards>
<forward name="login" path="/login.do"/>
</global-forwards>
<action-mappings>
<action path="/login" type="com.r4r.struts.LoginAction" 
name="loginForm" scope="request/session" validate="true/false/yes/no">
<forward name="success" path="/success.jsp"/>
<forward name="error" path="/error.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.r4r.struts.ApplicationResources"/>
</struts-config>

Path attributes:-The value of path attribute of <action>tag must be same as <html:form>action attribute. eg.<html:form action="”/login”"><action path="”/login”">.The input attribute of <action>tag consist the name of Input JSP View.

<action-mappings>
<action path="/login">
<forward name="success" path="/success.jsp"/>
<forward name="error" path="/error.jsp"/>
</action>
</action-mappings>

Scope attributes:-The scope attribute is used to define scope for request or session .

<action-mappings>
<action path="/login" scope="request/session">
<forward name="success" path="/success.jsp"/>
<forward name="error" path="/error.jsp"/>
</action>
</action-mappings>

Type attributes:-The type attribute is fully qualified name of Action subclass.

<action-mappings>
<action path="/login" type="com.r4r.struts.LoginAction" scope="request/session">
<forward name="success" path="/success.jsp"/>
<forward name="error" path="/error.jsp"/>
</action>
</action-mappings>

Validate attribute:-The validate attribute is for enable/disable the for check form validation.

<action-mappings>
<action path="/login" type="com.r4r.struts.LoginAction" scope="request/session" validate="true/false/yes/no">
<forward name="success" path="/success.jsp"/>
<forward name="error" path="/error.jsp"/>
</action>
</action-mappings>

Name attributes:- The name attribute is used to tell controller for lookup the name of action form to store input data. The value of name attribute must be same as appropriate name attribute. We can configure more than one Action within tag. To configure Forward struts provides tag. This tag has two attributes name and path.The value of name attribute is the key which will matched with key returned by appropriate action subclass and this value is used to forward to next view (to the value of path attribute).We can configure more than one forward with in tag. Struts provides tag to configure the subclass of ActionForm. tag have two attributes name and type .The type attribute is fully qualified name of subclass of ActionForm and the name is through which controller lookup to store the data coming for Input JSP View. 

<action-mappings>
<action path="/login" type="com.r4r.struts.LoginAction" 
name="loginForm" scope="request/session" validate="true/false/yes/no">
<forward name="success" path="/success.jsp"/>
<forward name="error" path="/error.jsp"/>
</action>
</action-mappings>

Previous Home Next