Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

Name

SmartURLs Plugin

Publisher

Brian Pontarelli

License

Open Source (ASL2)

Version

0.1

Homepage

http://code.google.com/p/smarturls-s2/

Download

http://code.google.com/p/smarturls-s2/downloads/list

Wiki Markup
{rate:title=Rating|theme=dynamic}

SmartURLs is

Excerpt

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

.

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.actionImage Removed 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

Let's expand on this example and add a code behind class. In order to do this we first have to tell SmartURLs where the classes live. This is controlled via the property smarturls.action.packages setting in the struts.properties file. If you don't have a struts.properties file, simply create one and place it into the classpath of the application. This can be done by putting the file in WEB-INF/classes or into a JAR file in the root directory of that JAR file. Here's how it should look:

Code Block
titlestruts.properties
borderStylesolid

smarturls.action.packages=com.example.actions

This configuration tells SmartURLs to go find all the actions in the com.example.actions package and configure them for use. This will also find all actions in sub-packages as well. We'll get to sub-packages and namespaces next. Here's our code behind:

Code Block
titlecom.example.actions.HelloWorld
borderStylesolid

package com.example.actions;

import 

public class HelloWorld extends ActionSupport {
  private String message;

  public String getMessage() {
    return message;
  }

  public String execute() {
    message = "Hello World!"
    return SUCCESS;
  }
}

Sub-packages and namespaces

Results and locations

Components

Extensionless URLs

Configuration reference

...

is distributed with Struts since 2.1 See the Convention Plugin