Skip to content

Quick start Struts

Dennis Kieselhorst edited this page Feb 6, 2024 · 13 revisions

⚠️ Struts framework is no longer supported starting with version 2.0: To continue using Struts, you need to use Serverless Java Container 1.x.

You can use the aws-serverless-java-container library to run a Apache Struts2 based application in AWS Lambda. You can use the library within your Lambda handler to load your Struts application and proxy events to it.

Serverless Java Container 1.x is tested against Struts version 6.0.x

Maven archetype

You can quickly create a new serverless Struts2 application using our Maven archetype. First, make sure Maven is installed in your environment and available in your PATH. Next, using a terminal or your favorite IDE create a new application, the archetype groupId is com.amazonaws.serverless.archetypes and the artifactId is aws-serverless-struts-archetype;

mvn archetype:generate -DgroupId=my.service -DartifactId=my-service -Dversion=1.0-SNAPSHOT \
       -DarchetypeGroupId=com.amazonaws.serverless.archetypes \
       -DarchetypeArtifactId=aws-serverless-struts-archetype \
       -DarchetypeVersion=1.9.4

The archetype sets up a new project that includes a pom.xml file as well as a build.gradle file. The generated code includes a StreamLambdaHandler class, the main entry point for AWS Lambda; a resource package with a /ping resource; and a set of unit tests that exercise the application.

The project also includes a file called template.yml. This is a SAM template that you can use to quickly test your application in local or deploy it to AWS. Open the README.md file in the project root folder for instructions on how to use the SAM CLI to run your Serverless API or deploy it to AWS.

Manual setup / Converting existing projects

1. Import dependencies

The first step is to import the Struts2 implementation of the library:

<dependency>
    <groupId>com.amazonaws.serverless</groupId>
    <artifactId>aws-serverless-java-container-struts</artifactId>
    <version>1.9.4</version>
</dependency>

This will automatically also import the aws-serverless-java-container-core and aws-lambda-java-core libraries.

Publish your Lambda function

You can follow the instructions in AWS Lambda's documentation on how to package your function for deployment.

Using Struts2 plugins can end into problems when you bundle your projects with the Maven Shade plugin to an Uber JAR because of conflicting struts-plugin.xml file in each plugin jar file. The recommended way for Apache Struts would be creating a **.zip** deployment file with the **maven-assembly-plugin**.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/dist.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>lambda</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>lambda</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <useProjectArtifact>false</useProjectArtifact>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/*.class</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

Deploy your Lambda function

  1. Run mvn install to build your deployment ZIP file
  2. In AWS Console or over AWS CLI deploy generated ZIP file
  3. Use com.amazonaws.serverless.proxy.struts2.Struts2LambdaHandler::handleRequest as Handler