ActionServlet Overview
Here's a brief digest of how ActionServlet is designed
(see Specification for detailed info):
- ActionServlet acts as 'Controller' in Model-View-Controller (MVC) programming
model. Views are usually HTML templates processed by a template engine (currently
Velocity or WebMacro).
- Web applications are built of (reusable) components. Component classes can be of any type
(JavaBeans, EJBs, Corba objects etc.).
- With ActionServlet, application flow is configured by a XML file (called
ActionConfig) with a simple syntax, which provides a way to:
- select which method of which component to be invoked by HTTP request:
- by matching action URL (using regular expressions) or
- by matching HTTP parameters 'form' and 'action',
- pass parameters to a component method upon request -
parameters for action method can be taken from:
- Context object,
- HTTP request (usual ?param=value style),
- URL (like http://server/shop/addCart/banana),
- other component (by <input-variable> element),
- direct value (<input-variable> element),
- react to action method invocation, because every method:
- returns a value (or void) - then <on-return> element is used,
- throws an exception - then <on-exception> element is used.
Example: Excerpt of ActionConfig:
<component name="Authenticator" class="my.package.Authenticator" persistence="session">
<action name="/login" method="login(String userName, String password)">
<input-variable name="password" value="defaultValue" if="!$password"/>
<output-variable name="loginOK"
component="Authenticator" value="isLoggedIn()"/>
<on-return value="void" show-template="SuccessfulLogin.wm"/>
<on-exception class="LoginException" show-template="Login.wm"/>
</action>
</component>
This config means that upon a request, where URL is formed like http://my.server.com/servlet/login:
- values of userName and password parameters are retrieved from HTTP (providing default
password if necessary),
- authenticator's method login is invoked,
- $loginOK variable value is set to the return value of Authenticator.isLoggedIn(),
- upon successful action SuccessfulLogin.wm page/on login error Login.wm is shown.
- ActionServlet can send to browser not only rendered HTML templates, but anything you like -
downloaded file, generated picture etc. - or redirect to given URL. Just use 'show-value-of' or
'show-url' attributes in place of 'show-template'.
- Actions can invoke sub-actions by <invoke> element, so HTTP
parameters can be passed to more than one component or invoke methods upon exceptions/return values,
default actions can be defined for each template and so on.
- Type handlers are used to convert between types - this makes sending data
between components easy. For example, you can take java.util.Vector
returned by one action method and pass it to another component method as an array.
|
|