Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

SmartURLs is

Excerpt

a code by convention and component plugin similar to the Codebehind and Zero-config plugins

.Documentation coming soon to this page.

Introduction

SmartURLs is a drop in replacement (well pretty close anyways) for the codebehind and zero-config functionality that comes with Struts. This plugin provides the following features in addition to the features provided by those two:

  • SEO compliant URLs (i.e. my-action rather than MyAction)
  • Action name support in classes using annotations
  • Results based on action names
  • Component support for shipping actions inside JAR files, modifying action namespaces and result locations
  • Default action and result handling (i.e. /products will try com.example.actions.Products as well as com.example.actions.products.Index)

SmartURLs requires minimal configuration in order to work properly because of the current method that Struts uses for custom configuration providers. We'll cover that next.

Setup

In order to use the SmartURLs plugin, you first need to add the JAR file to the WEB-INF/lib directory of you application. In addition, you need to configure the FilterDispatcher in the WEB-INF/web.xml so that Struts will load the configuration provider for SmartURLs. In order to configure the FilterDispatcher, add this init-param to the filter definition:

Code Block
titleWEB-INF/web.xml
borderStylesolid

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    <init-param>
      <param-name>configProviders</param-name>
      <param-value>org.texturemedia.smarturls.SmarturlsConfigurationProvider</param-value>
    </init-param>
  </filter>

Hello world

Now that the SmartURLs plugin is setup for use, let's start with a very simple example. This example will use an actionless result that is identified by the URL. By default, SmartURLs uses assumes that all of the results are stored in WEB-INF/content. This can be changed by setting the property smarturls.base.result.location in the Struts properties file to the new location. Don't worry about trailing slashes, SmartURLs handles this for you. Here is our hello world JSP:

Code Block
titleWEB-INF/content/hello-world.jsp
borderStylesolid

Hello world!

If you start Tomcat (or whichever J2EE container you are using) and type in *http://localhost:8080/hello-world.action*Image Added in to your browser you should see our JSP. This illustrates that SmartURLs will find results ever when no action exists and it is all based on the URL passed to Struts.

Code behind hello world

Configuration reference