Struts2.Net

Tutorial

Struts2 Actions

Struts2 Tags

Struts 1 vs 2

Struts1 Migration

Tool Tips

Aticles/References

Download

Reference

Contact Us

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Struts 2 Tutorial

Next Previous  
Step1>Create Hello Application

This tutorial assumes that you have some understanding of Apache Struts1.x and Java Technology. And that you have installed the Struts2 framework on your machine.

Lets start with a simple application, then we will move on to extend it.
In Struts2
when you submit an HTML form, the input is sent to a Java class (action) that you write yourself. These classes are called Actions. After the Action executes, a "Result" selects a resource to render the response using the mappings provided by you to the Struts through Struts.xml configuration file. The resource which displays the output is noramally a java server page(jsp), but it can also be another resource like a PDF file, an Excel file, or a Java applet.

Now lets create a simple "Hello " web application that displays a message to the user.

Start with creating a "tutorial" web application using any IDE (or simply a java editor/compiler). To create the "Hello" application you need to do three things(other than setting web.xml): create a server page to show the message, create an action class to generate the message, and finally create a mapping to link the action with the output page. These resources represent the View, the Model, and the Controller layers in the MVC pattern. Separation of concerns is key to the design ofJ2EE(or EE) applications, as it makes it easier to manage applications as they grow complex. Lets create them.

Hello.jsp--You need a server page (may be a jsp) to present the message.
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
    <head>
        <title>Hello !</title>
    </head>
    <body>
        <h3><s:property value="message" /></h3>
    </body>
</html>
Hello.java -- You also need an Action class to generate a message.
package tutorial;
import com.opensymphony.xwork2.ActionSupport;
public class Hello extends ActionSupport {
    public static final String displayMsg = "Struts2 is running.";
    public String execute() throws Exception {
        setMessage(displayMsg);
        return SUCCESS;
    }
    private String message;
    public void setMessage(String message){
        this.message = message;
    }
    public String getMessage() {
        return message;
    }
}

Create the struts.xml file and add the mappings for Hello action , as shown below:

struts.xml -- Lastly, you need to create mappings to bind it together.
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="yourPackage" extends="struts-default">
        <action name="Hello" class="yourPackage.Hello">
            <result>/Hello.jsp</result>
        </action>
        <!-- Add more actions here -->
    </package>
</struts>

Deploy the application on a web server (if you are using an IDE like Eclipse or WSAD then just select the option of Run On server) and open

http://localhost:8080/tutorial/Hello.action
You should see a page with the title "Hello" and the message "Struts2 is running". Dont forget to compile your actions to WEB-INF/classes directory.

Explanation:

When you enter the above action URL the browser sends request to the web server and is follwed by these steps:

  • The web server send a request to the container for the web resource Hello.action. As specefied in the web application settings in the web deployment descriptor(web.xml), the container finds that all requests will be sent to org.apache.struts2.dispatcher.FilterDispatcher, including the *.action requests. Note that the FilterDispatcher is the entry point into the Struts2 framework for any request.
  • The framework looks for an action mapping named "Hello" in struts.xml file, and it finds that this mapping corresponds to the class "Hello". The framework instantiates the action and calls the action's execute method.
  • The execute method sets the message and returns "SUCCESS" as result name. The framework checks the action mappings to see what page to load if the result is "SUCCESS". TheStrtus2 tells the server to render Hello.jsp as the response. While Hello.jsp is being processed, the <s:property value="message" /> tag invokes the getMessage method of the Hello action, and the tag incorporates the value of the message into the response .
  • An HMTL response is sent back to the browser in response to its request.

Next Previous