Class ScriptAction

java.lang.Object
org.apache.struts.action.Action
org.apache.struts.scripting.ScriptAction
All Implemented Interfaces:
Serializable

public class ScriptAction extends Action
This Action uses scripts to perform its action. The scripting framework is Bean Scripting Framework 3.0 (JSR 223) 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. https://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:

  • 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 ScriptContextFilter and configuring it in struts-scripting.properties:

  • struts-scripting.filters.FILTER_NAME.class=FILTER_CLASS - The class implementing ScriptContextFilter 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.

To use other scripting engines, add them to the classpath.

To register more extensions to a scripting engine, create a file called struts-scripting.properties and add one propertie for each 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.

This code was originally based off code from JPublish, but has since been almost completely rewritten.

See Also:
  • Field Details

    • PROPS_PATH

      protected static final String PROPS_PATH
      The default path to the properties file.
      See Also:
    • ENGINE_BASE

      protected static final String ENGINE_BASE
      The base property for alternate BSF engines.
      See Also:
    • FILTERS_BASE

      protected static final String FILTERS_BASE
      The base property for classes that put new variables in the context.
      See Also:
  • Constructor Details

    • ScriptAction

      public ScriptAction()
  • Method Details

    • 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, Map<String,String> params)
      Parses the script name and puts any URL parameters in the context.
      Parameters:
      url - The script URL consisting of a path and optional parameters
      params - A parameter-map to declare new parameters in
      Returns:
      The name of the script to execute
    • loadScript

      protected org.apache.struts.scripting.Script loadScript(String name, ServletContext context) throws IOException, ScriptException
      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
      Throws:
      IOException - if an I/O error occurs
      ScriptException - if compilation fails
    • loadFilters

      protected static ScriptContextFilter[] loadFilters(Properties props)
      Loads and initializes the filters.
      Parameters:
      props - The properties defining the filters
      Returns:
      An array of the loaded filters
    • 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

      @Deprecated 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