Struts (Start with Maven Jetty + Eclipse)

Create maven structure

Using the following command to create the structure:


mvn archetype:create  -DartifactId=strutsApp -DarchetypeGroupId=org.apache.struts  -DarchetypeArtifactId=struts2-archetype-starter -DarchetypeVersion=2.0.5-SNAPSHOT -DremoteRepositories=


Go to the project directory and input command

mvn eclipse:eclipse

Build the project to Eclipse

Import the project as a web application. Create the jsp page in the jsp directory. For example

<%@ taglib prefix="s" uri="/struts-tags" %>




        <title>Hello World!</title>



        <h2><s:property value="message" /></h2>



Highlighted part is import of the struts tag library.  s:property is used as the get method to retrieve the value from the bean.


Create an Action class to create the message

package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {


    public static final String MESSAGE = "Struts is up and running …";


    public String execute() throws Exception {


        return SUCCESS;



    private String message;


    public void setMessage(String message){

        this.message = message;



    public String getMessage() {

        return message;




Create a mapping to couple the action and page in the struts.xml. Please be aware the correct path for the jsp file and action class should be provided for the result element.


    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"



    <package name="tutorial" extends="struts-default">

        <action name="HelloWorld" class="tutorial.HelloWorld">



        <!– Add your actions here –>




You should also make sure that correct mapping for bean id and its class in the applicationContext.xml

<bean id="helloWorld" class="" singleton="false"/>

How it works

Your browser sends to the web server a request for the URL http://localhost:9090/tutorial/HelloWorld.action

1.       The container receives from the web server a request for the resource HelloWorld.action. According to the settings loaded from the web.xml, the container finds that all requests are being routed to org.apache.struts2.dispatcher.FilterDispatcher, including the *.action requests. The FilterDispatcher is the entry point into the framework.

2.       The framework looks for an action mapping named "HelloWorld", and it finds that this mapping corresponds to the class "HelloWorld". The framework instantiates the Action and calls the Action’s execute method.

3.       The execute method sets the message and returns SUCCESS. The framework checks the action mapping to see what page to load if SUCCESS is returned. The framework tells the container to render as the response to the request, the resource HelloWorld.jsp.

4.       As the page HelloWorld.jsp is being processed, the <s:property value="message" /> tag calls the getter getMessage of the HelloWorld Action, and the tag merges into the response the value of the message.

5.       A pure HMTL response is sent back to the browser.

Run with Jetty

Jetty provides a container for rapid webapp development. This goal is used in-situ on a Maven project without first requiring that the project is assembled into a war, saving time during the development cycle. The plugin forks a parallel lifecycle to ensure that the "compile" phase has been completed before invoking Jetty. This means that you do not need to explicity execute a "mvn compile" first. It also means that a "mvn clean jetty:run" will ensure that a full fresh compile is done before invoking Jetty.


You are able to configure the Jetty plugin either in the pom file or provide an external configuration file







                  <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">





           <requestLog implementation="org.mortbay.jetty.NCSARequestLog">









Change the connection port number to 9090 inline within the pom file.







<jettyConfig>/my/special/jetty.xml</jett yConfig>



Reference to the external Jetty configuration file at /my/special/jetty.xml (content should be the same as described in the preceding inline configuration)



This entry was posted in Struts. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s