0% found this document useful (0 votes)
40 views

Struts 2 Architecture

Struts 2 is an MVC framework that implements the front controller pattern. It uses interceptors to handle cross-cutting concerns separately from actions. Actions represent interactions with the model and transfer data to and from the value stack, which is a storage area for request-scoped data. Results specify the view components that render responses. The framework is configured through XML files and annotations.

Uploaded by

Madhu Sudan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Struts 2 Architecture

Struts 2 is an MVC framework that implements the front controller pattern. It uses interceptors to handle cross-cutting concerns separately from actions. Actions represent interactions with the model and transfer data to and from the value stack, which is a storage area for request-scoped data. Results specify the view components that render responses. The framework is configured through XML files and annotations.

Uploaded by

Madhu Sudan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Struts 2 Architecture

Banu Prakash

[email protected]
Struts 2
• Struts 2 is not just a new release of the older
Struts 1 framework.
• Implements the Model-View-Controller (MVC)
design pattern
• Introduces several new architectural features
that make the framework cleaner and more
flexible such as
– interceptors for layering cross cutting concerns away
from action logic.
– a powerful expression language OGNL that
transverses the entire framework, that supports
modifiable and re-usable UI Components

[email protected]
High level overview of request
processing

[email protected]
Controller – FilterDispatcher
• Infact, the MVC variant used in Struts is
often referred to as a front controller MVC.
This basically means that the controller is
out front and is the first component to act
in the processing.
• This important object is a servlet filter that
inspects each incoming request to
determine which Struts 2 action should
handle the request
[email protected]
Model – Action
• An action is an encapsulation of the calls to business
logic into a single unit of work.
• Second, the action serves as a locus of data transfer.
• Once controller finds the appropriate action, the
controller hands over control of the request processing to
the action by invoking it.
• This invocation process, conducted by the framework,
will both prepare the necessary data and execute the
action’s business logic.
• When the action completes its work, it will forward to the
Struts 2 view component, the result.

[email protected]
View – Result
• This page is the user interface which
presents a representation of the
application’s state to the user.
• These are commonly JSP pages,Velocity
templates or some other presentation
layer technology.

[email protected]
[email protected]
Interceptors
• Interceptors are Struts 2 components that
execute both before and after the rest of the
request processing.
• Interceptor allows common, cross cutting tasks
to be defined in clean, re-usable components
that you can keep separate from your action
code.
• What kinds of work should be done in
interceptors? Logging is a good example.
– Logging is something that should be done with the
invocation of every action, but it probably shouldn't be
put in the action itself.
[email protected]
The ValueStack and OGNL
• ValueStack is simply a storage area that holds all of the
application domain data associated with the processing
of a request.

– Data is moved to the ValueStack in preparation of request


processing,
– it is manipulated there during action execution,
– and it is read from there when the results render their response
pages.

• OGNL is an expression language that allows you to


reference and manipulate the data on the ValueStack.

[email protected]
ValueStack and OGNL

[email protected]
ActionContext
• The ActionContext contains all of the data
that makes up the context in which an
action occurs.
• This includes the ValueStack , the request,
session, and application maps from the
Servlet API
• It is a ThreadLocal context

[email protected]
Web.xml

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
[email protected]
Struts 2 UI Component Tags to
Render the Form
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<s:form action=“Login">
<s:textfield name="name" label="Your name"/>
<s:password name=“password" label="Your Password"/>
<s:submit/>
</s:form>
</body>
</html>

[email protected]
struts.xml
http://localhost:8080/sampleApp/Login.action

<action name="Login" class=“com.banu.Login"> Action


<result>/Home.jsp</result>
<result name="input">/Login.jsp</result> com.banu.Login
</action>
success input
<action name="Register" class="com.banu.Register">
<result>/success.jsp</result> Result Result
<result name="input">/Registration.jsp</result>
Home.jsp Login.jsp
</action>

Action
com.banu.Register
success input
Result Result
success.jsp Registration.jsp
[email protected]
http://localhost:8080/sampleApp/Register.action
Struts 2 Actions
• Model in MVC contains the business logic and data of
the application.
• Actions Encapsulates interaction with the MVC model
public String execute() throws Exception {
boolean valid = dao.validateUser( name, password );
if(valid)
return "SUCCESS";
return “INPUT”;
}
• Provides locus for data transfer
– Struts 2 will Automatically Transfer Request Data onto the
JavaBeans Properties that your Action Class Exposes
private String name;
private String password;
Setters and Getters for name and password
[email protected]
Struts 2 Architecture
Client

HttpServletRequest
I I I
N N N

ActionMapper T T T
E E E Template

HttpServletResponse R R R JSP

FilterDispatcher C C C FreeMarker
Result
E E E Velocity,

P P P etc
ActionProxy Action
T T T
O O O
ConfigurationManager
R R R
1 2 3
struts.xml

[email protected]
Servlet Filters Interceptors Container Created User Created
Request Lifecycle
• User Sends request:
• FilterDispatcher determines the appropriate action.
• ActionProxy takes help from Configuration Files
manager, which is initialized from the struts.xml.
• ActionProxy creates an ActionInvocation, which
implements the command pattern
• ActionInvocation process invokes the Interceptors (if
configured) and then invokes the action
• ActionInvocation looks for proper result. Then the result
is executed, which involves the rendering of JSP or
templates.

[email protected]
[email protected]

You might also like