Struts (Start with Maven Jetty + Eclipse)

Create maven structure

Using the following command to create the structure:

 

mvn archetype:create -DgroupId=hongliang.com.firstHibernate  -DartifactId=strutsApp -DarchetypeGroupId=org.apache.struts  -DarchetypeArtifactId=struts2-archetype-starter -DarchetypeVersion=2.0.5-SNAPSHOT -DremoteRepositories=http://people.apache.org/repo/m2-snapshot-repository

 

http://maven.apache.org/plugins/maven-archetype-plugin/usage.html

 

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" %>

 

<html>

    <head>

        <title>Hello World!</title>

    </head>

    <body>

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

    </body>

</html>

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 {

        setMessage(MESSAGE);

        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.

<!DOCTYPE struts PUBLIC

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

    "http://struts.apache.org/dtds/struts-2.0.dtd&quot;>

<struts>

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

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

            <result>/HelloWorld.jsp</result>

        </action>

        <!– Add your actions here –>

    </package>

</struts>

 

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

<bean id="helloWorld" class="hongliang.com.firstHibernate.HelloWorld" 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

<plugin>

    <groupId>org.mortbay.jetty</groupId>

    <artifactId>maven-jetty-plugin</artifactId>

    <configuration>

           <scanIntervalSeconds>10</scanIntervalSeconds>

           <connectors>

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

                        <port>9090</port>

                        <maxIdleTime>60000</maxIdleTime>

                  </connector>

           </connectors>

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

                  <filename>target/yyyy_mm_dd.request.log</filename>

                  <retainDays>90</retainDays>

                  <append>true</append>

                  <extended>false</extended>

                  <logTimeZone>GMT</logTimeZone>

           </requestLog>

    </configuration>

</plugin>

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

 

<plugin>

    <groupId>org.mortbay.jetty</groupId>

    <artifactId>maven-jetty-plugin</artifactId>

    <configuration>

           <scanIntervalSeconds>10</scanIntervalSeconds>

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

    </configuration>

</plugin>

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

 

http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin

http://www.hippocms.org/display/CMS/Changing+the+Jetty+configuration

 

 

Advertisements
This entry was posted in Struts. Bookmark the permalink.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s