Struts 2 is an MVC open source web application used for developing Java EE web applications.
The working of Struts 2 can be best illustrated with the help of a flow diagram.
From the flow diagram above we can see
- When the web application is deployed in a web server like tomcat or JBOSS, and the web server is started, the container reads the web.xml deployment descriptor for each deployed web application.
- In that file, you typically define the StrutsPrepareAndExecuteFilter which is mapped to a URL, typically /* meaning it will handle all incoming requests.
- And if the request pattern matches that in the web.xml, it calls this Filter dispatcher (StrutsPrepareAndExecuteFilter). In struts 2 the FilterDispatcher is the Front Controller,
- Once the web.xml configuration is loaded into memory on startup by the servlet FilterDispatcher, it triggers the loading and processing of the other configuration files also like the struts.xml, struts-default.xml and struts-plugin.xml. The servlet uses a Dispatcher class internally to manage this configuration loading process, building an internal map of actions, results, interceptors and other settings.
- Once the web server is started and the initialization of classes and loading of configuration into memory is completed, the filter intercepts incoming HTTP requests that match its URL pattern. These filters or interceptors handle operations like validations, file uploads, parameter binding, exception handling. These are pieces of code which get executed before and after the Action class is invoked.
- For each relevant request, it uses the corresponding pre loaded struts.xml file to determine which specific Action class to invoke.
Example - for the getquote example it will check in <CATALINA_HOME>/webapps/getquote/WEB-INF/web.xml file.
Sample code in the web.xml is below. In the below configuration, it calls the filterdispatcher (org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter) for all patterns passed in the url.
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping><struts>
<constant name="struts.ui.theme" value="simple" />
<package name="default" extends="struts-default">
<action name="lookup" class="src.LookupAction">
<result name="success">/quote.jsp</result>
<result name="failure">/quote.jsp</result>
</action>
</package>
</struts>
- Before this Action class is executed, the request is passed through the interceptors.
- The execute method of the Action class is executed. Here it can call functions and methods which implement the business logic and connect to the database.
- The data from this processed logic is then sent back to the Action class or the controller.
- The controller then identifies the view which will render this processed data.
- This processed data is sent through the interceptors before the response is displayed on the appropriate view.
References
God's Word for the day
Inappropriate speechOne may be prevented from sinning by poverty;
so when he rests he feels no remorse.
One may lose his life through shame,
or lose it because of human respect.
Another out of shame makes promises to a friend,
and so makes an enemy for nothing
Sirach 20:21-23
Gospel teachings of Jesus
Jesus and Beelzebul
Then they brought to him a demoniac who was blind and mute;
And he cured hime, so that the one who had been mute could speak and see.
All the crowds were amazed and said, "Can this be the Son of David?"
But when the pharisees heard it they said, "It is only by Beelzebul,
the ruler of the demons, that this fellow casts out demons."
He knew what they were thinking and said to them
"Every kingdom divided against itself is laid waste, and no city or house
divided against itself will stand. If satan casts out satan,
he is divided against himself; how then will his kingdom stand?
If I cast out demons by beelzebul, by whom do your exorcists cast them out?
Therefore they will be your judges. But if it is by the spirit of God that I
cast out demons, then the kingdom of God has come to you.
Or how can one enter a strong man's house and plunder his property,
without first tying up the strong man? Then indeed the house can be plundered.
Whoever is not with me is against me, and whoever does not gather with me scatters.
Therefore I tell you, people will be forgiven for every sin and blasphemy,
but blasphemy against the Spirit will not be forgiven. Whoever speaks a word
against the Son of Man will be forgiven, but whoever speaks against the Holy Spirit
will not be forgiven, either in this age or in the age to come.
Mathew 12:22-32

No comments:
Post a Comment