org.apache.struts.scripting
Class ScriptAction

java.lang.Object
  extended by org.apache.struts.action.Action
      extended by org.apache.struts.scripting.ScriptAction

public class ScriptAction
extends Action

This Action uses scripts to perform its action. The scripting framework is Apache's Bean Scripting Framework which allows the scripts to be written many of the popular scripting languages including JavaScript, Perl, Python, and even VBA.

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

Before the script completes, the next ActionForward needs to be specified. This can be done one of two ways:

  1. Set struts.forwardName to the name of the forward
  2. Set struts.forward to the actual ActionForward object
A number of pre-defined variables are available to the script: You can add your own variables by creating a BSFManagerFilter and configuring it in struts-scripting.properties:

To use other scripting engines other than BeanShell, create a file called struts-scripting.properties and add two properties for each engine: This code was originally based off code from JPublish, but has since been almost completely rewritten.


Field Summary
protected static String ENGINE_BASE
          The base property for alternate BSF engines.
protected static String FILTERS_BASE
          The base property for classes that put new variables in the context.
protected static Log LOG
          The logging instance.
protected static String PROPS_PATH
          The default path to the properties file.
 
Fields inherited from class org.apache.struts.action.Action
servlet
 
Constructor Summary
ScriptAction()
           
 
Method Summary
 ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
          Executes the script.
 Locale getLocale(HttpServletRequest req)
          Gets the locale.
 boolean isCancelled(HttpServletRequest req)
          Checks to see if the request is cancelled.
 boolean isTokenValid(HttpServletRequest req)
          Checks to see if the token is valid.
protected static BSFManagerFilter[] loadFilters(Properties props)
          Loads and initializes the filters.
protected  org.apache.struts.scripting.ScriptAction.Script loadScript(String name, ServletContext context)
          Loads the script from cache if possible.
protected  String parseScriptName(String url, org.apache.bsf.BSFManager manager)
          Parses the script name and puts any url parameters in the context.
 void resetToken(HttpServletRequest req)
          Resets the token.
 void saveErrors(HttpServletRequest req, ActionErrors errs)
          Deprecated. Use saveErrors(HttpServletRequest, ActionMessages) instead. This will be removed after Struts 1.2.
 void saveMessages(HttpServletRequest req, ActionMessages mes)
          Saves the messages to the request.
 void saveToken(HttpServletRequest req)
          Saves a token.
protected static String[] split(String line, String delimiter)
          Splits a line with the given delimiter.
 
Methods inherited from class org.apache.struts.action.Action
addErrors, addMessages, execute, generateToken, getErrors, getMessages, getResources, getResources, getServlet, isTokenValid, saveErrors, saveErrors, saveMessages, setLocale, setServlet
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

protected static final Log LOG
The logging instance.


PROPS_PATH

protected static final String PROPS_PATH
The default path to the properties file.

See Also:
Constant Field Values

ENGINE_BASE

protected static final String ENGINE_BASE
The base property for alternate BSF engines.

See Also:
Constant Field Values

FILTERS_BASE

protected static final String FILTERS_BASE
The base property for classes that put new variables in the context.

See Also:
Constant Field Values
Constructor Detail

ScriptAction

public ScriptAction()
Method Detail

execute

public ActionForward execute(ActionMapping mapping,
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response)
                      throws Exception
Executes the script.

Overrides:
execute in class Action
Parameters:
mapping - The action mapping
form - The action form
request - The request object
response - The response object
Returns:
The action forward
Throws:
Exception - If something goes wrong

parseScriptName

protected String parseScriptName(String url,
                                 org.apache.bsf.BSFManager manager)
                          throws Exception
Parses the script name and puts any url parameters in the context.

Parameters:
url - The script url consisting of a path and optional parameters
manager - The BSF manager to declare new parameters in
Returns:
The name of the script to execute
Throws:
Exception - If something goes wrong

loadScript

protected org.apache.struts.scripting.ScriptAction.Script loadScript(String name,
                                                                     ServletContext context)
Loads the script from cache if possible. Reloads if the script has been recently modified.

Parameters:
name - The name of the script
context - The servlet context
Returns:
The script object

loadFilters

protected static BSFManagerFilter[] loadFilters(Properties props)
Loads and initializes the filters.

Parameters:
props - The properties defining the filters
Returns:
An array of the loaded filters

split

protected static String[] split(String line,
                                String delimiter)
Splits a line with the given delimiter.

Parameters:
line - The line to split
delimiter - The string to split with
Returns:
An array of substrings

saveToken

public void saveToken(HttpServletRequest req)
Saves a token.

Overrides:
saveToken in class Action
Parameters:
req - The request object

isCancelled

public boolean isCancelled(HttpServletRequest req)
Checks to see if the request is cancelled.

Overrides:
isCancelled in class Action
Parameters:
req - The request object
Returns:
True if cancelled

isTokenValid

public boolean isTokenValid(HttpServletRequest req)
Checks to see if the token is valid.

Overrides:
isTokenValid in class Action
Parameters:
req - The request object
Returns:
True if valid

resetToken

public void resetToken(HttpServletRequest req)
Resets the token.

Overrides:
resetToken in class Action
Parameters:
req - The request object

getLocale

public Locale getLocale(HttpServletRequest req)
Gets the locale.

Overrides:
getLocale in class Action
Parameters:
req - The request object
Returns:
The locale value

saveMessages

public void saveMessages(HttpServletRequest req,
                         ActionMessages mes)
Saves the messages to the request.

Overrides:
saveMessages in class Action
Parameters:
req - The request object
mes - The action messages

saveErrors

public void saveErrors(HttpServletRequest req,
                       ActionErrors errs)
Deprecated. Use saveErrors(HttpServletRequest, ActionMessages) instead. This will be removed after Struts 1.2.

Saves the errors to the request.

Parameters:
req - The request object
errs - The action errors


Copyright © 2000-2008 Apache Software Foundation. All Rights Reserved.