What's new? | Help | Directory | Sign in
Google
                
Search
for
Updated Aug 28, 2007 by bpontarelli
Components  
Covers the component support of SmartURLs

Introduction

SmartURLs provides the ability to write highly componentized modules where actions and results can be re-used across applications. Action classes as well as FreeMarker or Velocity results can be added to JAR files and SmartURLs will find these actions and result and reveal them to the application.

Configuration

In order to setup a component that will be found by SmartURLs, you first need to define an XML configuration file inside the JAR. This file must be located in the META-INF directory and named component.xml. This file will tell SmartURLs what action packages are contain in the JAR file as well as where the results for those actions will be stored, either inside the JAR file or as part of the web application. The component.xml file looks like this:

<component name="myComponent">
  <action_package>com.example.component.actions</action_package>
  <base_result_location>/WEB-INF/component/my-component</base_result_location>
</component>

This component.xml file tells SmartURLs that it contains action classes in the com.example.component.actions package (as well as sub-packages) and that all the results of these actions will be located in the directory /WEB-INF/component/my-component.

Hello Component

In order to illustrate a component of SmartURLs, create a Hello Component. First we need to write the action class.

package com.example.component.hello.actions;

import com.opensymphony.xwork2.ActionSupport;

public class HelloComponent extends ActionSupport {
  private String message;

  public String getMessage() {
    return message;
  }

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

Next, let's write a FreeMarker result for this action. Here is our FreeMarker template:

<h3>Component message</h3>
${message}

Next, we need to create our component.xml file to tell SmartURLs how to find the component action and result. Here is our component.xml file:

<component name="helloComponent">
  <action_package>com.example.component.hello.actions</action_package>
  <base_result_location>/WEB-INF/component/hello-component</base_result_location>
</component>

Lastly, we need to compile the Java class and JAR all of these files up. Here is how the hello-component.jar file will look:

hello-component.jar
 |
 |-com
 |  |-example
 |     |-component
 |        |-hello
 |           |-actions
 |              |- HelloComponent.class
 |
 |-META-INF
 |  |-component.xml
 |
 |-WEB-INF
 |  |-component
 |     |-hello-component
 |        |-hello-component.ftl

As you can see, the class file is located in the correct package directories, the component.xml file is located in the META-INF directory and the FreeMarker result for our component is located in the WEB-INF directory as specified by the component.xml file.

Placing this JAR file into a web applications WEB-INF/lib directory will allow SmartURLs to find the component and load it into the Struts runtime. Now, we can access our action using the URL http://localhost:8080/hello-component.


Comment by wumengye, Sep 05, 2007

Hi Pontarelli, thanks for your great work. I'll definitely use smarturls in my projects, I like it.

One of my questions about component is:if my component need to access database using JPA/hibernate or need to do injection using Guice/Spring, how could I write component classes and package all files, and what should I be awared of?


Sign in to add a comment