Advanced Actions in Struts - Differences - Advantages

Struts Advanced Actions - Where to use them

Advanced Actions in Struts -

1. Dispatch Action

2. Lookup Dispatch Action

3. Mapping Dispatch Action

4. Switch Action

5. Forward Action

6. Include Action

1. Dispatch Action -

1 . When you want to write more than 1 action in same action then you can use dispatch action.

2 . Action will extends DisptchAction

public class DispatchAction1 extends DispatchAction
{

public ActionForward add(ActionMapping map,ActionForm form,HttpServletRequest req,HttpServletResponse res)
{

}
public ActionForward delete(ActionMapping map,ActionForm form,HttpServletRequest req,HttpServletResponse res)
{

}

}

3 . In struts-config.xml you have to specify a parameter
<action path="/dispatchtest"
type="com.DispatchAction.DispatchAction1"
name="dispatchform"
input="/pages/DispatchTest.jsp"
parameter="method">
<forward name="add" path="/pages/add.jsp"/>
<forward name="delete" path="/pages/delete.jsp"/>
</action>

4 . In jsp you can call action like this /dispatchtest.do?method='add' or /dispatchtest.do?method='delete'

2. Lookup Dispatch Action -

1 . When you want to have more than one submit button in same form in jsp then you can use dispatch action.

2 . Action will extends LookupDispatchAction and you have to override getKeyMethodMap method like this below.

public class LookupAction extends LookupDispatchAction
{
protected Map getKeyMethodMap() {
Map map=new HashMap();
map.put("submit.add","add");
map.put("submit.delete","delete");
return map;
}

public ActionForward add(ActionMapping map,ActionForm form,HttpServletRequest req,HttpServletResponse res)
{ }
}

3 . In struts-config.xml you have to specify a parameter
<action path="/lookup"
type="com.lookupdispatch.LookupAction"
name="lookupform"
input="/pages/LookupTest.jsp"
parameter="method">
<forward name="add" path="/pages/add.jsp"/>
<forward name="delete" path="/pages/delete.jsp"/>
</action>

4. In jsp

<html:html>
<body>
<html:form action="/lookup">
<h2><u>LookUpForm</u></h2>
Number1:<html:text property="n1" value=""/><br><br>
Number2:<html:text property="n2" value=""/><br><br>

<html:submit property="method" value="add"></html:submit>
<html:submit property="method">delete</html:submit>
</html:form>
</body>
</html:html>


5.In properties file you have to specify these properties which are in getKeyMethodMap method.
submit.add=add
submit.delete=delete



3. Mapping Dispatch Action -

1.When you want to write more than 1 action in same action but you want to call that actions by different url's then you can use Mapping dispatch action.

2. Action will extends MappingDispatchAction

public class MappingAction extends MappingDispatchAction
{

public ActionForward add(ActionMapping map,ActionForm form,HttpServletRequest req,HttpServletResponse res)
{

}
public ActionForward delete(ActionMapping map,ActionForm form,HttpServletRequest req,HttpServletResponse res)
{

}
}

3. In struts-config.xml you can give one url for each action in mapping dispatch action and also you have to specify action name as parameter.

<action path="/mappingtest1"
type="com.mapping.MappingAction"
name="mapform"
input="/pages/MappingTest.jsp"
parameter="add">
<forward name="add" path="/pages/add.jsp"/>
</action>

<action path="/mappingtest2"
type="com.mapping.MappingAction"
name="mapform"
input="/pages/MappingTest.jsp"
parameter="delete">
<forward name="delete" path="/pages/delete.jsp"/>
</action>

4. In jsp

<html:html>
<head>
<script type="text/javascript">
function Addtest()
{
document.testform.action="../mappingtest1.do";
document.testform.submit();
}

function Deletetest()
{
document.testform.action="../mappingtest2.do";
document.testform.submit();
}

</script>
</head>
<body>
<form name="testform">
<h2><u>Mapping Dispatch Form</u></h2>
Number1:<html:text property="n1" value=""/><br><br>
Number2:<html:text property="n2" value=""/><br><br>

<input type="button" value="Add" onclick="Addtest()"/>
<input type="button" value="Delete" onclick="Deletetest()"/>
</form>
</body>
</html:html>

4. Include Action -

Include action works like <jsp:include> main difference between them include action works through controller.

1. In struts-config.xml -

<action path="/includetest"
type="org.apache.struts.actions.IncludeAction"
parameter="/pages/Includehello.jsp"
input="/pages/IncludeTest.jsp">
</action>

Here type is org.apache.struts.actions.IncludeAction and parameter which file you have to include that name and input is from where
you are inlcuding that file.

2.In Jsp -

<html:html>
<BODY>
<H2><u>Include Action</H2></u>
<html:link page="/includetest.do">Go to Include Page</html:link>
</html:html>

5. Forward Action -

Forward action is similar to <jsp:forward> main difference between them forward action works through controller.

1. In struts-config.xml -

<action path="/forwardtest"
type="org.apache.struts.actions.ForwardAction"
parameter="/pages/ForwardSuccess.jsp"
input="/pages/ForwardTest.jsp">
</action>

Here type is org.apache.struts.actions.ForwardAction and parameter which file you have to forward that name and input is from where
you are forwarding that file.

2.In Jsp -

<html:html>
<H2><u>Forward Action</H2></u>
<html:link page="/forwardtest.do">Click to Forward Success Page</html:link>
</html:html>

5.Switch Action -

Switch Action mainly used for switching from one config file to another config file.When you want to call action from one config file which is in other config file then you can use this action.

1. web.xml - How to put 2 config files in struts

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

<init-param>
<param-name>config/test</param-name>
<param-value>/WEB-INF/test-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>

2. In struts-config.xml -

<action path="/switch"
type="org.apache.struts.actions.SwitchAction"/>

<action path="/switchtest"
type="com.switchdispatch.Switchaction"
name="switchform"
input="/pages/SwitchTest.jsp">
<forward name="add" path="/switch.do?page=/test1.do&prefix=/test"
contextRelative="true" redirect="true"/>
</action>

Here we have to define a path for switch action like '/switch' type is "org.apache.struts.actions.SwitchAction" .
When you want to forward success page to another action which is not in struts-config.xml then path is "/switch.do?page=/test1.do&prefix=/test"
Here "/switch" is switch action path and page is where you want to forward which is in other xml and prefix is path of other xml (check in web.xml).

3.In test-config.xml

<struts-config>
<form-beans>
<form-bean name="testform1" type="com.switchdispatch.TestForm"/>
</form-beans>
<action-mappings>
<action path="/test1" name="testform1" input="/pages/test1.jsp" type="com.switchdispatch.TestAction" scope="request">
<forward name="success" path="/pages/hello.jsp" contextRelative="true" redirect="true" />
</action>
</action-mappings>
</struts-config>

4.We can call action like this "/switchtest.do" .

No comments: