Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.struts.scripting
Class ScriptAction

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

    public class ScriptAction

extends org.apache.struts.action.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:

- `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-scripting.properties:

- `struts-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.

To use other scripting engines other than BeanShell, 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.

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

Field Summary
`protected static java.lang.String` | `ENGINE_BASE` The base property for alternate BSF engines.
`protected static java.lang.String` | `FILTERS_BASE` The base property for classes that put new variables in the context.
`protected static org.apache.commons.logging.Log` | `LOG` The logging instance.
`protected static java.lang.String` | `PROPS_PATH` The default path to the properties file.

Fields inherited from class org.apache.struts.action.Action
`defaultLocale, servlet`

Constructor Summary
`ScriptAction()`

Method Summary
`org.apache.struts.action.ActionForward` | `execute(org.apache.struts.action.ActionMapping mapping, org.apache.struts.action.ActionForm form, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)` Executes the script.
`java.util.Locale` | `getLocale(javax.servlet.http.HttpServletRequest req)` Gets the locale.
`boolean` | `isCancelled(javax.servlet.http.HttpServletRequest req)` Checks to see if the request is cancelled.
`boolean` | `isTokenValid(javax.servlet.http.HttpServletRequest req)` Checks to see if the token is valid.
`protected static BSFManagerFilter[]` | `loadFilters(java.util.Properties props)` Loads and initializes the filters.
`protected org.apache.struts.scripting.ScriptAction.Script` | `loadScript(java.lang.String name, javax.servlet.ServletContext context)` Loads the script from cache if possible.
`protected java.lang.String` | `parseScriptName(java.lang.String url, org.apache.bsf.BSFManager manager)` Parses the script name and puts any url parameters in the context.
`void` | `resetToken(javax.servlet.http.HttpServletRequest req)` Resets the token.
`void` | `saveErrors(javax.servlet.http.HttpServletRequest req, org.apache.struts.action.ActionErrors errs)` Deprecated. Use saveErrors(HttpServletRequest, ActionMessages) instead. This will be removed after Struts 1.2.
`void` | `saveMessages(javax.servlet.http.HttpServletRequest req, org.apache.struts.action.ActionMessages mes)` Saves the messages to the request.
`void` | `saveToken(javax.servlet.http.HttpServletRequest req)` Saves a token.
`protected static java.lang.String[]` | `split(java.lang.String line, java.lang.String delimiter)` Splits a line with the given delimiter.

Methods inherited from class org.apache.struts.action.Action
`addErrors, addMessages, execute, generateToken, getDataSource, getDataSource, 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 org.apache.commons.logging.Log LOG

  The logging instance.

### PROPS_PATH

    protected static final java.lang.String PROPS_PATH

  The default path to the properties file.

  See Also:
    Constant Field Values

### ENGINE_BASE

    protected static final java.lang.String ENGINE_BASE

  The base property for alternate BSF engines.

  See Also:
    Constant Field Values

### FILTERS_BASE

    protected static final java.lang.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 org.apache.struts.action.ActionForward execute(org.apache.struts.action.ActionMapping mapping,
                                                          org.apache.struts.action.ActionForm form,
                                                          javax.servlet.http.HttpServletRequest request,
                                                          javax.servlet.http.HttpServletResponse response)
                                                   throws java.lang.Exception

  Executes the script.

  Overrides:
    `execute` in class `org.apache.struts.action.Action`
  Parameters:
    `mapping` - The action mapping
    `form` - The action form
    `request` - The request object
    `response` - The response object
  Returns:
    The action forward
  Throws:
    `java.lang.Exception` - If something goes wrong

### parseScriptName

    protected java.lang.String parseScriptName(java.lang.String url,
                                               org.apache.bsf.BSFManager manager)
                                        throws java.lang.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:
    `java.lang.Exception` - If something goes wrong

### loadScript

    protected org.apache.struts.scripting.ScriptAction.Script loadScript(java.lang.String name,
                                                                         javax.servlet.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(java.util.Properties props)

  Loads and initializes the filters.

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

### split

    protected static java.lang.String[] split(java.lang.String line,
                                              java.lang.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(javax.servlet.http.HttpServletRequest req)

  Saves a token.

  Overrides:
    `saveToken` in class `org.apache.struts.action.Action`
  Parameters:
    `req` - The request object

### isCancelled

    public boolean isCancelled(javax.servlet.http.HttpServletRequest req)

  Checks to see if the request is cancelled.

  Overrides:
    `isCancelled` in class `org.apache.struts.action.Action`
  Parameters:
    `req` - The request object
  Returns:
    True if cancelled

### isTokenValid

    public boolean isTokenValid(javax.servlet.http.HttpServletRequest req)

  Checks to see if the token is valid.

  Overrides:
    `isTokenValid` in class `org.apache.struts.action.Action`
  Parameters:
    `req` - The request object
  Returns:
    True if valid

### resetToken

    public void resetToken(javax.servlet.http.HttpServletRequest req)

  Resets the token.

  Overrides:
    `resetToken` in class `org.apache.struts.action.Action`
  Parameters:
    `req` - The request object

### getLocale

    public java.util.Locale getLocale(javax.servlet.http.HttpServletRequest req)

  Gets the locale.

  Overrides:
    `getLocale` in class `org.apache.struts.action.Action`
  Parameters:
    `req` - The request object
  Returns:
    The locale value

### saveMessages

    public void saveMessages(javax.servlet.http.HttpServletRequest req,
                             org.apache.struts.action.ActionMessages mes)

  Saves the messages to the request.

  Overrides:
    `saveMessages` in class `org.apache.struts.action.Action`
  Parameters:
    `req` - The request object
    `mes` - The action messages

### saveErrors

    public void saveErrors(javax.servlet.http.HttpServletRequest req,
                           org.apache.struts.action.ActionErrors errs)

  Deprecated. Use saveErrors(HttpServletRequest, ActionMessages) instead. This will be removed after Struts 1.2.

  Saves the errors to the request.

  Overrides:
    `saveErrors` in class `org.apache.struts.action.Action`
  Parameters:
    `req` - The request object
    `errs` - The action errors

Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2003-2005 The Apache Software Foundation. All Rights Reserved.