What is Struts Flow?

Answer Posted / archana dandwate

Actually struts use MVC(model VIew Controller)
Archietecture.

In struts we have to define following things:
1. index.jsp(it takes input from user suppose username)
2.ActionServlet class
3.Action class
4.displayname.jsp(it display username)
---------------------
we need to change 2 file
5.struts-config.xml
6.web.xml
---------------
Lets Start with step one. we will create the view file
named index.jsp


<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<html>
<head>
<title>Sample Struts Application</title>
</head>
<body>
<html:form action="Name" name="nameForm"
type="example.NameForm">
<table width="80%" border="0">
<tr>
<td>Name:</td>
<td><html:text property="name" /></td>
</tr>
<tr>
<td><html:submit /></td>
</tr>
</table>
</html:form>
</body>
</html>
-----------------------------------------

Note:
here we write tag like <html:form> because its struts
specific form tag.
In the Form tags the attributes you can find some
attributes defined in we will go through it.

action : Represents the URL to which this form will be
submitted. This attribute is also used to find the
appropriate ActionMapping in the Struts configuration file,
which we will describe later in this section. The value
used in our example is Name, which will map to an
ActionMapping with a path attribute equal to Name.

name :Identifies the key that the ActionForm will be
referenced by. We use the value NameForm. An ActionForm is
an object that is used by Struts to represent the form data
as a JavaBean. It main purpose is to pass form data between
View and Controller components. We will discuss NameForm
later in this section.
type :Names the fully qualified class name of the form bean
to use in this request. For this example, we use thevalue
example.NameForm, which is an ActionForm object containing
data members matching the inputs of this form.

To use the HTML tags, you must first add a taglib entry in
the application’s web.xml file that references the URI /WEB-
INF/struts-html.tld. This TLD describes all of the tags in
the HTML tag library. The following snippet shows the
<taglib> element that must be added to the web.xml file:

<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
--------------------------------------------

The struts-html.tld is placed in the /WEB_INF directory.

Next Step is to create the action form

The ActionForm used in this example contains a single data
member that maps directly to the name input parameter of
the form defined in the index.jsp View. When an
<html:form/> is submitted, the Struts framework populates
the matching data members of the ActionForm with the values
entered into the <html:input/> tags. The Struts framework
does this by using JavaBean reflection. The accessors of
the ActionForm must follow the JavaBean standard naming
convention for example

The NameForm.java file is shown below
NameForm.java

package example;
//import statements
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class NameForm extends ActionForm {
private String name = null;
public String getName() {
return (name);
}
public void setName(String name) {
this.name = name;
}
public void reset(ActionMapping mapping,
HttpServletRequest request) {
this.name = null;
}
}
To deploy the NameForm to our Struts application, you need
to compile this class, move it to the /WEB-
INF/classes/example directory, and add the following line
to the <form-beans> section of the /WEB-INF/struts-
config.xml file:

<form-bean name="nameForm" type="example.NameForm"/>

This makes the Struts application aware of the NameForm and
how it should be referenced.

Now we create the out page for the sample application.
Lets name it diplayname.jsp
displayname.jsp

<html>
<head>
<title>Sample Struts Display Name</title>
</head>
<body>
<table width="80%" border="0">
<tr>
<td>Hello <%= request.getAttribute("NAME") %> !!
</td>
</tr>
</table>
</body>
</html>

Now we move to the step two of creating the application's
controller

In a Struts application, two components make up the
Controller. These two components are the
org.apache.struts.action.ActionServlet and the org.apache.
struts.action.Action classes. In most Struts applications,
there is one org. apache.struts.action.ActionServlet
implementation and can have many org.apache.
struts.action.Action implementations.

The org.apache.struts.action.ActionServlet is the
Controller component that handles client requests and
determines which org.apache.struts.action.Action will
process the received request. When assembling simple
applications, such as the one we are building, the default
ActionServlet will satisfy your application needs, and
therefore, you do not need to create a specialized
org.apache.struts.action.ActionServlet implementation.

The second component of a Struts Controller is the
org.apache.struts. action.Action class. As opposed to the
ActionServlet, the Action class must be extended for each
specialized function in your application. This class is
where your application’s specific logic begins.
NameAction.java

package example;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class NameAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String target = new String("success");
if ( form != null ) {
// Use the NameForm to get the request parameters
NameForm nameForm = (NameForm)form;
String name = nameForm.getName();
}
// if no mane supplied Set the target to failure
if ( name == null ) {
target = new String("failure");
}
else {
request.setAttribute("NAME", name);
}
return (mapping.findForward(target));
}
}

Moving to step three, to deploy the NameAction to our
Struts application, we need to compile the NameAction class
and move the class file to /WEB-INF/classes/example
directory, and add the following entry to the <action-
mappings> section of the /WEB-INF/struts-config.xml file:

<action path="/Name" type="example.NameAction"
name="nameForm" input="/index.jsp">
<forward name="success" path="/displayname.jsp"/>
<forward name="failure" path="/index.jsp"/>
</action>


For step four we modify the web.xml file. We have to to
tell the Web application about our ActionServlet. This is
accomplished by adding the following servlet definition to
the /WEB-INF/web.xml file:

<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>
<load-on-startup>1</load-on-startup>
</servlet>

Once we have told the container about the ActionServlet, we
need to tell it when the action should be executed. To do
this, we have to add a <servlet-mapping> element to
the /WEB-INF/ web.xml file:

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
----------------------------------------
important note:
You will notice in the previously listed index.jsp that our
action does not include a .do at the end of the URL. We do
not have to append the .do because it is automatically
appended if we use the <html:form /> tag. If you do not use
the <html:form /> tag, then you will need to append .do to
the action's URL. This mapping tells the Web application
that whenever a request is received with .do appended to
the URL, the servlet named action should service the
request.

Eg:
<html:form action="Name.do" name="nameForm"
type="example.NameForm">

-------------------------------------------------

Is This Answer Correct ?    26 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is token used for?

582


Which design pattern is implemented by Struts2 interceptors?

651


Explain the necessity of empty tag?

674


Explain the complete struts validator framework.

645


What are result types in struts?

663






Why was reload removed from struts (since 1.1)?

630


Explain the difference between jakarta struts and apache struts?

610


What are the ways in which duplicate form submission can occur?

588


What is the difference between shocks and struts?

584


What is the difference in using Action interface and ActionSupport class for our action classes, which one you would prefer?

669


How many action classes can be used in struts application?

605


Explain about token feature in struts?

644


What is the purpose of @expressionvalidator annotation?

685


What is the purpose of @conversionerrorfieldvalidator annotation?

712


What is the struts in java?

597