Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3
Warning
titleDeprecated Plugin

Since 2.1 this plugin has been deprecated in favor of the Convention Plugin. See this page for details on how to port your application to the Convention plugin.

The Codebehind Plugin

Excerpt

reduces mundane configuration by adding "Page Controller" conventions

.

The Codebehind plugin brings elements of the "Page Controller" design to Struts 2 for the purposes of reducing the amount of mundane configuration through conventions. There are two common situations for configuration that could be improved:where the plugin applies convention over configuration:

  1. Default mappings - (or "pages with no mappings") Pages with no Action - These are cases where the page is mostly static and doesn't require an Action class to execute logic. Common examples are index pages and those that heavily use JSP tags or JSF components.
  2. Default results - The purpose of most Actions is to execute code to prepare the data for a specific page. The name of this page is usually often the same as the Action itself.

To improve the first case, the Codebehind plugin will detect the presence of a page with no corresponding Struts Action mapping and automatically substitute the ActionSupport classa mapping that uses the default Action class for the package, which is basically usually ActionSupport, a NO-OP Action.

For the problem of default results, the Codebehind plugin will make it unnecessary to define those results by detecting the presense presence of a page for that Action and creating the appropriate configuration on-the-fly.

In these two ways, the Codebehind plugin allows you to follow encourages a page-based development style, handing handling the linking of Struts Actions actions with pages and pages or with Results in a common way.

Pages with no Action

.

(tick) To see the plugin in action, review the "Person Manager" example in the Showcase application.

Features

  • Provides default mappings for pages that don't have Actions
  • Provides default results by auto-discovering pages

Usage

To use this plugin, simply copy its jar into your application. The plugin can be used to find default mappings and results.

Default Mappings

To better facilitate To better facilite a code-behind development approach, the Codebehind plugin will detect the case where the request has no defined Struts Actionaction mapping, yet there exists a corresponding page available. It will then create a dummy ActionSupport instance to substitute for the missing Action classaction mapping referencing the default Action class (usually ActionSupport), allowing the page to be displayed normally. Additionally, the default interceptor stack for the configured package will be applied, allowing
you to bring bringing the workflow benefits of interceptor stacks to simple pages.

When no explicitly configured Action can be found for a request, the Codebehind Plugin plugin searches the web application for a possible likely page. Specifically, the following pattern is used to locate a page:

...

For example, if the request is for http://www.company.com/myapp/member/login.actionImage Removed, the Codebehind Plugin plugin will look for the following pages, in this order:

...

If any of those pages are found, the plugin will construct an ActionConfig object on the fly, using the ActionSupport class for the Action and a single Result that points to the discovered page. The ActionConfig will be put in the configured package, meaning that it will inherit the default Interceptor stack for that package. The default package is codebehind-default, however, it can be configured in any configuration file (see Constant Element) via the struts.codebehind.defaultPackage constant.

Default Results

Eighty-percent of Results will be ones with the same In many applications, a majority of Results could have the same root name as the Action itselfaction mapping. To reduce this unnecessary configuration, the Struts plugin will try to guess the appropriate Result, if none is explicitly configured. This technique works for any result code, including success. When combined with the Zero Configuration style, the amount of configuration in your an application goes dwindles to next to nothing.

When no explicitly configured Result is found for an Action and its returned 's result code, the Codebehind Pluginplugin, again, searches the web application for a possible matching page. Specifically, the following patterns, in the following order, are used to locate a page:

...

These two patterns are searched for each of the three possible default page extensions: jsp, vm, and ftl. For example, if the request is for http://www.company.com/myapp/member/login.actionImage Removed, so that the action name is login and the namespace is member, and the Action class returned a code of success, the Codebehind Plugin plugin will look for the following pages, in this order:

...

If any of those pages are found, the appropriate Result will be constructed and processed.

Settings

The following settings can be customized. See the developer guide.

Setting

Description

Default

Possible Values

struts.codebehind.defaultPackage

The default package to use for created Action mappings

codebehind-default

Any existing package name

struts.configuration.classpath.disableActionScanning

Whether to disable scanning the classpath for Action classes or not

false

true or false

Installation

This plugin can be installed by copying the plugin jar into your application's /WEB-INF/lib directory. No other files need to be copied or created.