Vous êtes sur la page 1sur 18

Struts 2 Architecture

Banu Prakash

banuprakashc@yahoo.co.in
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

banuprakashc@yahoo.co.in
High level overview of request
processing

banuprakashc@yahoo.co.in
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
banuprakashc@yahoo.co.in
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.

banuprakashc@yahoo.co.in
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.

banuprakashc@yahoo.co.in
banuprakashc@yahoo.co.in
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.
banuprakashc@yahoo.co.in
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.

banuprakashc@yahoo.co.in
ValueStack and OGNL

banuprakashc@yahoo.co.in
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

banuprakashc@yahoo.co.in
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>
banuprakashc@yahoo.co.in
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>

banuprakashc@yahoo.co.in
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
banuprakashc@yahoo.co.in
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
banuprakashc@yahoo.co.in
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

banuprakashc@yahoo.co.in
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.

banuprakashc@yahoo.co.in
banuprakashc@yahoo.co.in

Vous aimerez peut-être aussi