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> |
|