STRUTS
·
Struts is a framework released by apache group. This framework build based
on MVC2 architecture. A framework is a piece of s/w developed by experts. The
framework resolve the commonly occurred problems throughout the multiple
projects.
·
There are so many frameworks are available in
market. Those are struts, spring and JSF ... etc. These frameworks simplify the
development of web based applications.
·
Most of the frameworks build based on the MVC2 architecture.
If we use struts framework we can say we are using MVC2 architecture.
·
Struts s/w is released in two versions 1.X and
2.X
·
we can download struts s/w from "www.apache.org".
once we download s/w we need to extract the zip file.
Procedure to work with first
struts based application:
1.
After downloading the s/w we can find war file
whose name is 'struts-blank.XXX.war'
2.
Create a folder with name which is same as
project name
3.
copy struts-blank.war file into created folder
4.
extract contents from war file
5.
delete war file
6.
deploy project into server
·
When we download struts s/w by default struts
guys supply the couple of jar files and xml files. The most important jar file
is "struts-config.xml". couple of xml files are struts-config.xml,
validation.xml, web.xml.
·
The followings are supplies with the struts-core.jar
file
1.
A servlet with the name
"org.apache.struts.action.ActionServlet"
2.
3.
Set of error classes. Action, ActionMapping,
ActionErrors, ActionMessage and ActionMessages
4.
Set of tag lib directories are html, bean and
logic
·
Struts guys has released web.xml file, in this
file they have configured all the struts configuration file
<web-app>
<servlet>
<servlet-name>struts</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>
<load-on-start-up>2</load-on-start-up>
</servlet>
<servlet-mapping>
<servlet-name>struts</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
What will happen when we
deploy above project into the server?
Step
1: When we deploy struts based
application into the server, server checks for the web.xml file. Now server
reads the contents from the and stores data into the JVM's memory.
step
2: When ever server find the <load-on-start-up> tag
server load the servlet into the JVM's memory. Here servlet is ActionServlet.
step3:
Now server calls the init() method of ActionServlet. As part of init() they
have provided code for getting init parameter objects, then server loads the
struts-config.xml file into the JVM's memory.
·
If any request comes to the struts based
application server handover request to the ActionServlet service method
·
Predefined Actions
1.
Forward Action
2.
Include Action
3.
Dispatch Action
4.
Lookup Dispatch Action
5.
Switch Action
All the predefined action classes are available in the
strits-extras.jar file
Forward Action:
We have developed one.jsp and deployed it. To access this
client need to send request directly to the jsp file, because of this we are
violating the rules of MVC2 architecture.
To resolve the above problem we have to develop one action
class and need to send request to the action class and action class invoke the
jsp file.
public class DispatchActionExampleAction extends DispatchAction
{
public
ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest
request, HttpServletResponse response)
{
System.out.println("save Product");
return
mapping.findForward("jsp");
}
}
The following is the
configuration for jsp to access page through the controller
<struts-config>
<form-beans></form-beans>
<global-exceptions> </global-exceptions>
<global-forwards></global-forwards>
<action-mappings>
<action type="com.ibm.struts.actions.ForwardAction"
path="/Forward"
>
<forward nam=" jsp" path="/forward.jsp"/>
</action>
</action-mappings>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/org/apache/struts/validator/
validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
Disadvantage for this approach is if we have thousands of
jsp's in our project we have to develop thousands of controller classes to the
jsp. Instead of following this approach struts guys has provided the forward
action class
Following is the configuration for the forward action class
<struts-config>
<form-beans></form-beans>
<global-exceptions> </global-exceptions>
<global-forwards></global-forwards>
<action-mappings>
<action type="org.apache.struts.actions.ForwardAction"
path="/Forward"
parameter="/ForwardAction.jsp"></action>
</action-mappings>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/org/apache/struts/validator/
validator-rules.xml,/WEB- INF/validation.xml"/>
</plug-in>
</struts-config>
Include Action:
This is includes the output of calling Servlet and called
Servlet and sent the output to cliend.
If we are really using the struts application, no need to
use this action class in out project because we have action chaining.
If projects were developed on servlet and need to convert
them to struts at that time we go for include action.
<?xml version="1.0" encoding="ISO-8859-1"
?>
<!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-beans>
<global-exceptions> </global-exceptions>
<global-forwards></global-forwards>
<action-mappings>
<action type="org.apache.struts.actions.IncludeAction"
path="/Inclue"
parameter="/IncludeAction.jsp"/>
</action-mappings>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/org/apache/struts/validator/validator-
rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
Dispatch Action:
·
In a project we are developing multiple forms to
perform different tasks. The following are the some of the forms
·
To
implement the above form we need to develop three action classes.
According to SUN it is not recommended to use small java programs into the
project. So here we create only one action class which handles all the actions.
Here we have dispatch action class. This class contain our own execute methods.
Action Class:
package com.ibm.struts.actions;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.apache.struts.actions.DispatchAction;
public class DispatchActionExampleAction extends
DispatchAction
{
public
ActionForward saveProduct(ActionMapping mapping, ActionForm form, HttpServletRequest
request, HttpServletResponse response)
{
System.out.println("save Product");
return
null;
}
public
ActionForward retrieveProduct(ActionMapping mapping, ActionForm form, HttpServletRequest
request, HttpServletResponse response)
{
System.out.println("Retrieve
Product");
return
null;
}
}
·
We have to configure above class in the struts
configuration file. To deal with dispatch action class we need to provide the
extra attribute "parameter". This is called as dispatch
handler
<struts-config>
<form-beans></form-beans>
<global-exceptions> </global-exceptions>
<global-forwards></global-forwards>
<action-mappings>
<action type="com.ibm.struts.actions.DispatchActionExampleAction"
path="/Dispatch"
parameter="mname"></action>
</action-mappings>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/org/apache/struts/validator/validator-rules.xml,
/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
·
To execute dispatch action class we need to send
url like
http://localhost:8089/PredefinedActions.do?mname=saveProduct
·
When we send the above request to the server,
server remove the ".do" from the url and checks in the struts configuration
file, from the file server get the type value and create the object to
the action class. Then server call the execute method of the struts action
class and server is unable to find the execute method, then server call this
method in super class and execute it in super class as part of this method
sever get the handler parameter and call the that method. So server executes
this method.
How to create our own url?
<script>
function sendSaveRequest() {
alert("/Dispatch.do?mname=saveProduct");
document.forms[0].action = "/Dispatch.do?mname=saveProduct";
document.form[0].submit();
}
function sendRetrieveRequest() {
alert("/Dispatch.do?mname=retrieveProduct");
document.forms[0].action = "/Dispatch.do?mname=retrieveProduct";
document.forms[0].submit();
}
</script>
<form>
<input type="submit" value="Retrieve"
onsubmit="sendRetrieveRequest" />
<input type="submit" value="Save"
onsubmit="sendSaveRequest" />
</form>
No comments:
Post a Comment