Sei sulla pagina 1di 7

Struts

Struts is a open source framework which make building of the web applications
easier based on the java Servlet and JavaServer pages technologies.

Struts framework was created by Craig R. McClanahan and donated to the Apache
Software Foundation in 2000. The Project now has several committers, and many
developers are contributing to overall to the framework.

Developing web application using struts frame work is fairly complex, but it eases
things after it is setup. It encourages software development following the MVC
design pattern. Many web applications are JSP-only or Servlets-only. With JSP and
Servlets, Java code is embedded in the HTML code and the Java code calls println
methods to generate the HTML code respectively. Both approaches have their
advantages and drawbacks; Struts gathers their strengths to get the best of their
association.

Struts is based on Model-View-Controller (MVC) design paradigm, it is an


implementation of JSP Model 2 Architecture. For more of Model-View-Controller
(MVC) click here.

Consists of 8 Top-Level Packagesand approx 250 Classes and Interfaces.

Struts is a set of cooperating classes, servlets, and JSP tags that make up a reusable
MVC 2 design. This definition implies that Struts is a framework, rather than a
library, but Struts also contains an extensive tag library and utility classes that work
independently of the framework.

The overview of struts

Client browser
An HTTP request from the client browser creates an event. The Web container will
respond with an HTTP response.

Controller
The controller is responsible for intercepting and translating user input into actions to
be performed by the model. The controller is responsible for selecting the next view
based on user input and the outcome of model operations. The Controller receives
the request from the browser, and makes the decision where to send the request.
With Struts, the Controller is a command design pattern implemented as a servlet.
The struts-config.xml file configures the Controller.

Business logic
The business logic updates the state of the model and helps control the flow of the
application. With Struts this is done with an Action class as a thin wrapper to the
actual business logic.

Model
A model represents an application’s data and contains the logic for accessing and
manipulating that data. Any data that is part of the persistent state of the application
should reside in the model objects. The business objects update the application
state. ActionForm bean represents the Model state at a session or request level, and
not at a persistent level. Model services are accessed by the controller for either
querying or effecting a change in the model state. The model notifies the view when
a state change occurs in the model. The JSP file reads information from the
ActionForm bean using JSP tags.

View
The view is responsible for rendering the state of the model. The presentation
semantics are encapsulated within the view, therefore model data can be adapted for
several different kinds of clients. The view modifies itself when a change in the model
is communicated to the view. A view forwards user input to the controller. The view
is simply a JSP file. There is no flow logic, no business logic, and no model
information -- just tags. Tags are one of the things that make Struts unique
compared to other frameworks like Velocity.

MVC
The MVC (Model-View-Controller) architecture the client request is first intercepted
by a servlet referred as controller servlet. this servlet handles the initial processing
of the request and determines which JSP page to display next. Here the controller
servlet is the single point of entry, there is a clear separation of business logic,
presentation output and request processing. MVC architecture is a way of
decomposing an application into three parts:

The model maintains the state and data that the application represents.
The view allows the display of information about the model to the user.
The controller allows the user to manipulate the application.
the model, the view and the controller.

MVC was originally applied in the graphical user interaction model of input,
processing and output.

In Struts, the view is handled by JSPs and presentation components, the model is
represented by Java Beans and the controller uses Servlets to perform its action.

Model
A model represents an application’s data and contains the logic for accessing and
manipulating that data. Any data that is part of the persistent state of the application
should reside in the model objects. The business objects update the application
state. ActionForm bean represents the Model state at a session or request level, and
not at a persistent level. Model services are accessed by the controller for either
querying or effecting a change in the model state. The model notifies the view when
a state change occurs in the model. The JSP file reads information from the
ActionForm bean using JSP tags.

View
The view is responsible for rendering the state of the model. The presentation
semantics are encapsulated within the view, therefore model data can be adapted for
several different kinds of clients. The view modifies itself when a change in the model
is communicated to the view. A view forwards user input to the controller. The view
is simply a JSP or HTML file. There is no flow logic, no business logic, and no model
information -- just tags. Tags are one of the things that make Struts unique
compared to other frameworks like Velocity.

Controller
The controller is responsible for intercepting and translating user input into actions to
be performed by the model. The controller is responsible for selecting the next view
based on user input and the outcome of model operations.The Controller receives the
request from the browser, and makes the decision where to send the request. With
Struts, the Controller is a command design pattern implemented as a servlet. The
struts-config.xml file configures the Controller.

Struts Controller
The controller is responsible for intercepting and translating user input into actions to
be performed by the model. The controller is responsible for selecting the next view
based on user input and the outcome of model operations. The Controller receives
the request from the browser, invoke a business operation and coordinating the view
to return to the client.

The controller is implemented by a java servlet, this servlet is centralized point of


control for the web application. In struts framework the controller responsibilities are
implemented by several different components like

The ActionServlet Class


The RequestProcessor Class
The Action Class

The ActionServlet extends the javax.servlet.http.httpServlet class. The


ActionServlet class is not abstract and therefore can be used as a concrete controller
by your application.

The controller is implemented by the ActionServlet class. All incoming requests are
mapped to the central controller in the deployment descriptor as follows.

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
</servlet>

All request URIs with the pattern *.do are mapped to this servlet in the deployment
descriptor as follows.

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
<url-pattern>*.do</url-pattern>
A request URI that matches this pattern will have the following form.
http://www.my_site_name.com/mycontext/actionName.do

The preceding mapping is called extension mapping, however, you can also specify
path mapping where a pattern ends with /* as shown below.

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/do/*</url-pattern>
<url-pattern>*.do</url-pattern>

A request URI that matches this pattern will have the following form.
http://www.my_site_name.com/mycontext/do/action_Name

The class org.apache.struts.action.requestProcessor process the request from


the controller. You can sublass the RequestProcessor with your own version and
modify how the request is processed.

Once the controller receives a client request, it delegates the handling of the request
to a helper class. This helper knows how to execute the business operation
associated with the requested action. In the Struts framework this helper class is
descended of org.apache.struts.action.Action class. It acts as a bridge between a
client-side user action and business operation. The Action class decouples the client
request from the business model. This decoupling allows for more than one-to-one
mapping between the user request and an action. The Action class also can perform
other functions such as authorization, logging before invoking business operation.
the Struts Action class contains several methods, but most important method is the
execute() method.

public ActionForward execute(ActionMapping mapping,


ActionForm form, HttpServletRequest request,
HttpServletResponse response)
throws Exception;

The execute() method is called by the controller when a request is received from a
client. The controller creates an instance of the Action class if one doesn’t already
exist. The strut framework will create only a single instance of each Action class in
your application.

Action are mapped in the struts configuration file and this configuration is loaded into
memory at startup and made available to the framework at runtime. Each Action
element is represented in memory by an instance of the
org.apache.struts.action.ActionMapping class . The ActionMapping object contains a
path attribute that is matched against a portion of the URI of the incoming request.

<action>
path= "/somerequest"
type="com.somepackage.someAction"
scope="request"
name="someForm"
validate="true"
input="somejsp.jsp"
<forward name="Success" path="/action/xys" redirect="true"/>
<forward name="Failure" path="/somejsp.jsp" redirect="true"/>
</action>

Once this is done the controller should determine which view to return to the client.
The execute method signature in Action class has a return type
org.apache.struts.action.ActionForward class. The ActionForward class represents a
destination to which the controller may send control once an action has completed.
Instead of specifying an actual JSP page in the code, you can declaratively associate
as action forward through out the application. The action forward are specified in the
configuration file.

<action>
path= "/somerequest"
type="com.somepackage.someAction"
scope="request"
name="someForm"
validate="true"
input="somejsp.jsp"
<forward name="Success" path="/action/xys" redirect="true"/>
<forward name="Failure" path="/somejsp.jsp" redirect="true"/>
</action>

The action forward mappings also can be specified in a global section, independent of
any specific action mapping.

<global-forwards>
<forward name="Success" path="/action/somejsp.jsp" />
<forward name="Failure" path="/someotherjsp.jsp" />
</global-forwards>

Struts Model
A model represents an application’s data and contains the logic for accessing and
manipulating that data. Any data that is part of the persistent state of the application
should reside in the model objects. The business objects update the application
state. ActionForm bean represents the Model state at a session or request level, and
not at a persistent level. Model services are accessed by the controller for either
querying or effecting a change in the model state. The model notifies the view when
a state change occurs in the model.The JSP file reads information from the
ActionForm bean using JSP tags.

The Struts frame work doen't offer much in the way pf building model components.
The Enterprise JavaBeans (EJB), Java Data Objects(JDO) and JavaBeans can be use
as a model. Struts frame work doesn't limit you to one particular model
implementation.

Struts View
The view is responsible for rendering the state of the model. The presentation
semantics are encapsulated within the view, therefore model data can be adapted for
several different kinds of clients.The view modifies itself when a change in the model
is communicated to the view. A view forwards user input to the controller.The view is
simply a JSP or HTML file. There is no flow logic, no business logic, and no model
information -- just tags. Tags are one of the things that make Struts unique
compared to other frameworks like Velocity.

The view components typically employed in a struts application are


HTML
Data Transfer Objects
Struts ActionForms
JavaServer pages
Custom tags
Java resource bundles

Struts ActionForm
Struts ActionForm objects are used to pass client input data back and forth between
the user and the business layer. The framework automatically collects the input from
the request and passes this data to an Action using a form bean, which is then
passed to the business layer.

Struts Tag Library


The Struts framework provides a fairly rich set of framework components. It also
includes a set of tag libraries that are designed to interact intimately with the rest o
the framework. The customg tags provided by Struts framework are grouped into
four distinct libraries
1. HTML
2. Bean
3. Logic
4. Template

And a special library called Nested tag library.

Custom tags with in Struts HTML tag library

base - renders an HTML base element


button - renders a button input fiels
cancel - renders a cancel button
checkbox - renders a checkbox input field
errors - conditionnaly renders a set of accumulated error messages
file - renders a file select input field
form - defines an HTML form element
frame - renders an HTML frame element
hidden - renders a hidden field
html - renders an HTMl html element
image - renders an input tag of type "image"
img - renders an HTMl img tag
javascript - renderts JavaScript validation rules loaded by ValidationPlugin
link - renders an HTML anchoror hyperlink
messages - Conditionally displays a set of accumulated messages
multibox -renders multiple checkbox input fields
option - renders a select option
options - renders a collection of select options
options Collection - render a collection of select options
password -renders a password input field
radio -renders a radio button input field
reset -renders a rest button input field
rewrite - renders a URI
select -renders a select element
submit -renders a submi button
text -renders an input field of type "text"
textarea -renders an textarea input field

Potrebbero piacerti anche