Scriptable Struts Actions Using BSF
As fate would have it, as I prepared the struts-example demo, idle googling found another project doing the same thing in pretty much the same way: IBM Alphaworks Struts Scripting. Unfortunately, I don't believe IBM's license is exactly open source, so perhaps there is still a need for this project. If anyone knows anything further, please, do share.
- Supports all BSF languages
- Scripts cached in memory on first use
- Ability to pass parameters to script through Struts config
- Reorganized how Struts objects are presented
- Added a configurable filter system to easily manipulate the context
- Added a filter that adds request parameters as variables
- Added ability to pass parameters to scripts in the action definition
- Added and updated Javadocs, code cleanups, documentation
- Added the ability to configure other BSF engines
- Working Action implementation with hardcoded BeanShell support
- Rewrote struts-example to demonstrate usage
To determine what script will be executed, the "parameter" attribute of
the action mapping should contain the name of the script relative to
the web application root directory (i.e. http://server/app). In the place of
the traditional Action implementation, use the value org.twdata.struts.ScriptAction.
<forward name="success" path="/index.jsp"/>
In addition to specifying the script file, the "parameter" attribute can
also contain parameters that will be passed to the script in the form of
pre-defined variables. The format follows the URL format:
One example of how passed parameters can be used is have one script file
handle multiple actions.
Before the script completes, the next ActionForward needs to be
specified. This can be done one of two ways:
- Set struts.forwardName to the name of the forward
- Set struts.forward to the actual ActionForward object
A number of pre-defined variables are available to the script:
request - The HTTP request
response - The HTTP response
session - The session
application - The servlet context
struts - A grouping of all Struts-related objects
log - A logging instance
You can add your own variables by creating a BSFManagerFilter and
configuring it in struts-bsf.properties:
struts-bsf.filters.FILTER_NAME.class=FILTER_CLASS - The
class implementing BSFManagerFilter where FILTER_NAME is the name you
are calling the filter.
- A property to be used by the filter.
To use other scripting engines other than BeanShell, create a file called
struts-bsf.properties and add two properties for each engine:
struts-bsf.engine.ENGINE_NAME.class - The class of the BSF
engine where ENGINE_NAME is the name you are calling the engine.
struts-bsf.engine.ENGINE_NAME.extensions - A comma-delimited
list of file extensions that will be used to identify the engine to use
to execute the script.
with comments, and suggestions.