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

How to use xpath on clientside

Here I am explaining how to use xpath on clientside

<html>
<head>
<script type="text/javascript">
readXmlUsingXPath();
function readXmlUsingXPath(){
var xmlDoc = null;
// code for IE

if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

xmlDoc.async=false;

xmlDoc.load("data.xml");

var xmlNodeList = xmlDoc.selectNodes("//Book[number(Pages)<30]");

for(var idx = 0; idx < xmlNodeList.length; idx++ ){

document.write("Title: " + xmlNodeList.item(idx).selectSingleNode("Title").text );

document.write(" Pages: " + xmlNodeList.item(idx).selectSingleNode("Pages").text);

document.write(" ISBN: " + xmlNodeList.item(idx).selectSingleNode("ISBN").text );

document.write("<br />")

}

}

// code for Mozilla, Firefox, Opera, etc.

else if (document.implementation && document.implementation.createDocument)

{

var requester = new XMLHttpRequest();

var xml = getXMLDocument("data.xml");

var xmlNodeList1 = xml.selectNodes("//Book[number(Pages)<30]");

for(var idx = 0; idx < xmlNodeList1.length; idx++ ){

document.write("Title: " + xmlNodeList1.item(idx).selectSingleNode("Title").text );

document.write(" Pages: " + xmlNodeList1.item(idx).selectSingleNode("Pages").text);

document.write(" ISBN: " + xmlNodeList1.item(idx).selectSingleNode("ISBN").text );

document.write("<br />")

}

}

}

function getXMLDocument(name)
{
requester.open("GET", name, false);
requester.send(null);
return requester.responseXML;
}


</script>
</head>

</html>

How to add xsl to xml using javascript

Here I am explaining how to add xsl to xml using javascript.

<html>
<head>
<script type="text/javascript">

// code for IE
if (window.ActiveXObject)
{
var xmlDoc;
var xml1;
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("students.xml");

xml1=new ActiveXObject("Microsoft.XMLDOM");
xml1.async=false;
xml1.load("studentmarks.xsl");

}

// code for Mozilla, Firefox, Opera, etc.

else if (document.implementation && document.implementation.createDocument)
{
var requester = new XMLHttpRequest();
var processor = new XSLTProcessor();
var serializer = new XMLSerializer();

var xml = getXMLDocument("students.xml");
var xsl1 = getXMLDocument("studentmarks.xsl");

processor.importStylesheet(xsl1);

var newXML = processor.transformToDocument(xml);
addXMLResult("Test", newXML);

}

else
{
alert('Your browser cannot handle this script');
}

function getXMLDocument(name)
{
requester.open("GET", name, false);
requester.send(null);
return requester.responseXML;
}

function addXMLResult(name, result)
{
var str;
if (result)
str = serializer.serializeToString(result);
document.writeln(str);
}

</script>
</head>

</html>