The entire Struts Scripting component is only a few classes, since most of the heavy lifting is done by the Bean Scripting Framework and the scripting engines themselves. Therefore, there are only a few steps you'll need to take in order to sucessfully install and use Struts Scripting.
struts-scripting-1.0.1.jar
, to your application's
libraries directory. When deployed, this directory will be WEB-INF/lib
.
Next, choose which scripting engine you want to use. BSF ships with knowledge about the following languages and script extensions:
javascript (js) | jacl (jacl) | netrexx (nrx) |
java (java) | javaclass (class) | bml (bml) |
vbscript (vbs) | jscript (jss) | perlscript (pls) |
perl (pl) | jpython (py) | jython (py) |
lotusscript (lss) | xslt (xslt) | pnuts (pnut) |
beanbasic (bb) | beanshell (bsh) | ruby (rb) |
judoscript (judo, jud) |
Once you have picked a language, you need to download its libraries and install it into your application, which usually means copying its jar files into the same location you copied the Struts Scripting jar. If you are not sure, we recommend Groovy which ships with the Struts Scripting Mailreader example, as it features a very similar Java-like syntax.
Optional: If you don't see your desired language that claims to support BSF,
or their BSF engine has changed from what BSF expects, you can manually define BSF
engines and have them loaded by Struts Scripting.
Create a file called
struts-scripting.properties
and add two properties for each engine:
struts-scripting.engine.ENGINE_NAME.class
- The class of the BSF
engine where ENGINE_NAME is the name you are calling the engine.struts-scripting.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.struts-scripting.properties
should then stored in a directory
accessible to the classloader, usually WEB-INF/classes
.
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.apache.struts.scripting.ScriptAction
.
For example:
<action path="/logoff" type="org.apache.struts.scripting.ScriptAction" parameter="/WEB-INF/scripts/Logoff.bsh"> <forward name="success" path="/index.jsp"/> </action>
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:
parameter="/path/file.bsh?PARAMETER_NAME=PARAMETER_VALUE[&PARAMETER_NAME=PARAMETER_VALUE..]"
This ability to pass parameters can enable DispatchAction-type behavior where one script file
can handle multiple action paths. If you are using Struts Action 1.3 or later, this feature
isn't as useful since you can pass multiple values through ActionMapping using the
<set-property key="foo" value="bar">
syntax.
Before the script completes, the next ActionForward needs to be specified. This can be done one of two ways:
struts.forwardName
to the name of the forwardstruts.forward
to the actual ActionForward object
request
- The HTTP requestresponse
- The HTTP responsesession
- The sessionapplication
- The servlet contextstruts
- A grouping of all Struts-related objectslog
- A logging instancestruts-scripting.filters.FILTER_NAME.class=FILTER_CLASS
- The
class implementing BSFManagerFilter where FILTER_NAME is the name you
are calling the filter.struts-scripting.filters.FILTER_NAME.PROPERTY_NAME=PROPERTY_VALUE
- A property to be used by the filter.