public class NameBasedActionResolver extends AnnotatedClassActionResolver
An ActionResolver that uses the names of classes and methods to generate sensible default
URL bindings and event names respectively. Extends the default
AnnotatedClassActionResolver, and is fully backward compatible. Any classes and
methods that are annotated with UrlBinding and
HandlesEvent will retain the bindings specified in
those annotations. In the case when an annotation is absent then a default binding is
generated.
The generation of ActionBean URL bindings is done by taking the class name and removing
any extraneous packages at the front of the name, removing the strings "Action" and "Bean"
from the end of the name, substituting slashes for periods and appending a suffix (.action by
default). The set of packages that are trimmed is specified by the
getBasePackages() method. By default this method returns the set
[web, www, stripes, action]. These packages (and their parents) are removed from the
class name. E.g. com.myco.web.foo.BarActionBean would become foo.BarActionBean.
Continuing on, the list of Action Bean suffixes, specified by the getActionBeanSuffixes()
method, are trimmed from the end of the Action Bean class name. With the defaults,
[Bean, Action], we would trim foo.BarActionBean further to foo.Bar, and then
translate it to /foo/Bar. Lastly the suffix returned by getBindingSuffix()
is appended, giving the binding /foo/Bar.action.
The translation of class names into URL bindings is designed to be easy to override and
customize. To that end you can easily change how this translation is done by overriding
getBasePackages() and/or getBindingSuffix(), or completely customize the
behaviour by overriding getUrlBinding(String).
Mapping of method names to event names is simpler. Again the parent class is delegated to
in case the method is annotated. If it is not, and the method is a concrete public method that
returns a Resolution (or subclass thereof) it is mapped to an event of the same name as the
method. So an un-annotated method "public Resolution view()" is mapped to an event
called "view". It should be noted that there is no special method name that signifies the
default handler. If there is more than one handler and you require a default handler you
must still mark the default method with @DefaultHandler.
Another useful feature of the NameBasedActionResolver is that when a request arrives for a
URL that is not bound to an ActionBean the resolver will attempt to map the request to a view
and return a 'dummy' ActionBean that will take the user to the view. The exact behaviour is
modifiable by overriding one or more of
handleActionBeanNotFound(ActionBeanContext, String), findView(String) or
getFindViewAttempts(String). The default behaviour is to map the URL being requested
to three potential JSP names/paths, check for the existence of a JSP at those locations and if
one exists then to return an ActionBean that will render the view. For example if a user
requested '/account/ViewAccount.action' but an ActionBean does not yet exist bound to that URL,
the resolver will check for JSPs in the following order:
The value of this approach comes from the fact that by default all pages can appear to have
a pre-action whether they actually have one or not. In the above can you might chose to link
to /account/ViewAccount.action even though you know that no action exists and you want
to navigate directly to a page. This way, if you later decide you do need a pre-action for any
reason you can simply code the ActionBean and be done. No URLs or links need to be modified
and all requests to /account/ViewAccount.action will flow through the ActionBean.
| Modifier and Type | Field and Description |
|---|---|
static Set<String> |
BASE_PACKAGES
Default set of packages (web, www, stripes, action) to be removed from the front
of class names when translating them to URL bindings.
|
static List<String> |
DEFAULT_ACTION_BEAN_SUFFIXES
Default list of suffixes (Bean, Action) to remove to the end of the Action Bean class name.
|
static String |
DEFAULT_BINDING_SUFFIX
Default suffix (.action) to add to URL bindings.
|
PACKAGESRESOLVED_ACTION| Constructor and Description |
|---|
NameBasedActionResolver() |
| Modifier and Type | Method and Description |
|---|---|
protected Resolution |
findView(String urlBinding)
Attempts to locate a default view for the urlBinding provided and return a
ForwardResolution that will take the user to the view.
|
ActionBean |
getActionBean(ActionBeanContext context,
String urlBinding)
Overridden to trap the exception that is thrown when a URL cannot be mapped to an
ActionBean and then attempt to construct a dummy ActionBean that will forward the
user to an appropriate view.
|
protected List<String> |
getActionBeanSuffixes()
Returns a list of suffixes to be removed from the end of the Action Bean class name, if present.
|
protected Set<String> |
getBasePackages()
Returns a set of package names (fully qualified or not) that should be removed
from the start of a classname before translating the name into a URL Binding.
|
protected String |
getBindingSuffix()
Returns a non-null String suffix to be used when constructing URL bindings.
|
protected List<String> |
getFindViewAttempts(String urlBinding)
Returns the list of attempts to locate a default view for the urlBinding provided.
|
String |
getHandledEvent(Method handler)
First checks with the super class to see if an annotated event name is present, and if
not then returns the name of the handler method itself.
|
String |
getUrlBinding(Class<? extends ActionBean> clazz)
Finds or generates the URL binding for the class supplied.
|
protected String |
getUrlBinding(String name)
Takes a class name and translates it into a URL binding by removing extraneous package names,
removing Action, Bean, or ActionBean from the end of the class name if present, replacing
periods with slashes, and appending a standard suffix as supplied by
getBindingSuffix(). |
protected ActionBean |
handleActionBeanNotFound(ActionBeanContext context,
String urlBinding)
Invoked when no appropriate ActionBean can be located.
|
void |
init(Configuration configuration)
First invokes the parent classes init() method and then quietly adds a specialized
ActionBean to the set of ActionBeans the resolver is managing.
|
addActionBean, assertGetContextWorks, findClasses, getActionBean, getActionBeanClasses, getActionBeanType, getConfiguration, getDefaultHandler, getEventName, getEventNameFromEventNameParam, getEventNameFromPath, getEventNameFromRequestAttribute, getEventNameFromRequestParams, getHandler, getRequestedPath, getUrlBindingFactory, getUrlBindingFromPath, makeNewActionBean, processMethods, removeActionBean, setActionBeanContextpublic static final Set<String> BASE_PACKAGES
public static final String DEFAULT_BINDING_SUFFIX
public void init(Configuration configuration) throws Exception
init in interface ConfigurableComponentinit in class AnnotatedClassActionResolverconfiguration - the Configuration object being used by StripesException - should be thrown if the component cannot be configured well enough to use.public String getUrlBinding(Class<? extends ActionBean> clazz)
Finds or generates the URL binding for the class supplied. First delegates to the parent
class to see if an annotated url binding is present. If not, the class name is taken
and translated into a URL binding using getUrlBinding(String name).
getUrlBinding in interface ActionResolvergetUrlBinding in class AnnotatedClassActionResolverclazz - a Class representing an ActionBeanprotected String getUrlBinding(String name)
getBindingSuffix().
For example the class com.myco.web.action.user.RegisterActionBean would be
translated to /user/Register.action. The behaviour of this method can be
overridden either directly or by overriding the methods getBindingSuffix() and
getBasePackages() which are used by this method.
name - the name of the class to create a binding forprotected Set<String> getBasePackages()
protected String getBindingSuffix()
protected List<String> getActionBeanSuffixes()
public String getHandledEvent(Method handler)
getHandledEvent in interface ActionResolvergetHandledEvent in class AnnotatedClassActionResolverhandler - a method which may or may not be a handler methodpublic ActionBean getActionBean(ActionBeanContext context, String urlBinding) throws StripesServletException
Overridden to trap the exception that is thrown when a URL cannot be mapped to an
ActionBean and then attempt to construct a dummy ActionBean that will forward the
user to an appropriate view. In an exception is caught then the method
handleActionBeanNotFound(ActionBeanContext, String) is invoked to handle
the exception.
getActionBean in interface ActionResolvergetActionBean in class AnnotatedClassActionResolvercontext - the ActionBeanContext of the current requesturlBinding - the urlBinding determined for the current requestStripesServletException - if no ActionBean or alternate strategy can be foundprotected ActionBean handleActionBeanNotFound(ActionBeanContext context, String urlBinding)
findView(String). If a view is found
then a dummy ActionBean is constructed that will send the user to the view. If no appropriate
view is found then null is returned.context - the ActionBeanContext of the current requesturlBinding - the urlBinding determined for the current requestprotected Resolution findView(String urlBinding)
Attempts to locate a default view for the urlBinding provided and return a
ForwardResolution that will take the user to the view. Looks for views by using the
list of attempts returned by getFindViewAttempts(String).
For each view name derived a check is performed using
ServletContext.getResource(String) to see if there is a file located at that URL.
Only if a file actually exists will a Resolution be returned.
Can be overridden to provide a different kind of resolution. It is strongly recommended when overriding this method to check for the actual existence of views prior to manufacturing a resolution in order not to cause confusion when URLs are mistyped.
urlBinding - the url being accessed by the client in the current requestprotected List<String> getFindViewAttempts(String urlBinding)
Returns the list of attempts to locate a default view for the urlBinding provided. Generates attempts for views by converting the incoming urlBinding with the following rules. For example if the urlBinding is '/account/ViewAccount.action' the following views will be returned in order:
Can be overridden to look for views with a different pattern.
urlBinding - the url being accessed by the client in the current request© Copyright 2005-2006, Stripes Development Team.