Struts 2 Tutorial – Getting Started
Struts 2 is an MVC web development framework. It is based off of Web Work and has far less configuration than the original Struts. I would strongly recommend Struts 2 over Struts for new development due to the faster development times it allows. This is a very simple example and follow up posts will be coming shortly to help you develop more complex apps with Struts 2.
Getting Started
Below is an example project layout that you can use for your project. It may be useful in helping you to decide where in your project to place your various files.
To get started, you’ll need to download the Struts libraries. At a minimum, you will need to put the following in your WEB-INF/lib directory:
- struts2-core.jar (The framework itself)
- xwork.jar (Struts 2 is built on the XWork 2 framework)
- ognl.jar (Object Graph Notation Language is used throughout the framework to access object properties)
- freemarker.jar (Freemarker is used to create UI tag templates in Struts 2)
- commons-logging.jar (Used to log to log4j or JDK logging)
Now we need to add a filter mapping to your web.xml file in order to have the Struts called whenever a page on your site is accessed:
<?xml version="1.0"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
It’s now time to make your first action.
package com.lumidant.tutorial.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
import com.lumidant.tutorial.struts2.dao.*;
import com.lumidant.tutorial.struts2.model.*;
public class ExampleAction extends ActionSupport {
private static final long SerialVersionUID = 1L;
private String id;
private Employee employee;
public String execute() throw Exception {
EmployeeDao dao = new EmployeeDao();
employee = dao.getEmployeeById(id);
return SUCCESS;
}
public Employee getEmployee() {
return employee;
}
public void setId(String id) {
this.id = id;
}
}
There are a couple important things to note here. The first is that our Action extends ActionSupport, which is a pattern you’ll want to continue for future actions. Secondly, the input to our page has a setter which the framework needs to have present and the output has a getter. Finally, our Action returns the result type SUCCESS. The other predefined result types are ERROR, INPUT, LOGIN, and NONE.
Struts places its configuration in a struts.xml file. A good place to put it is at the root of your source folder. We’ll need to add our new action to the struts.xml file.
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="ExampleAction" class="com.lumidant.tutorial.struts.action.ExampleAction">
<result name="success">/WEB-INF/pages/displayEmployee.jsp</result>
</action>
</package>
</struts>
And finally, we need to create a .jsp view for our results to be rendered in.
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Struts 2 Tutorial Example</title>
</head>
<body>
<h2>Selected Employee:</h2>
<span class="fn n">
<span class="given-name"><s:property value="employee.firstName" /></span>
<span class="additional-name"><s:property value="employee.middleName" /></span>
<span class="family-name"><s:property value="employee.lastName" /></span>
</span>
</body>
</html>
The important takeaway for the view is the manner in which properties can be accessed from the view. Struts 2 uses OGNL, which for the majority of cases means you’ll just leave the get off of your method name. For example <s:property value=”employee.firstName” /> will call your Action’s getEmployee() method and then will call the Employee’s getFirstName() method.
So now you should have an end-to-end working example. Depending on which application server you’re using, you should be able to access your sample page at a url like http://localhost:9090/ExampleAction.action You’ll of course need to create some sort of dummy EmployeeDao if you’re going to use this exact example, but you should be well on your way.
You’ll also want to continue reading about Struts 2 in the continuation of this tutorial:
Struts 2 Tutorial – Creating Views
Struts 2 Tutorial – Interceptors
Struts 2 Tutorial – Struts Configuration
As an optional, but nice complement, I’d also recommend SiteMesh Tutorial with Examples.
I read you all material and best thing is I find it very useful to understans struts 2 in comparison of other material on the Internet . I found you have not provide any example of the model class or doa class
example so if you can elaborate those two aspects then every one will have full idea about the Struts 2
Hi, McCann
I have just started to learn struts 2 my self. Means self learning. You have provided your example structure too good. But one confusion what’s the model here? and how it can be used? One another question is as in struts 1 does we required actionforms here or it’s just merged with action? Please reply. Thanks in advance.
Sorry, I have just tried this example and it is not working for me, I am using 2.1.6 and have the set up like this (although I have added the commons-fileupload-1.2.1.jar & commons-io-1.3.2.jar) but I just get a
org.apache.jasper.JasperException: File “/WEB-INF/struts-tags” not found
Error.
Hey the tutorial is good…
But is there any specification of the application server that have to be used in particular……
As i m new to struts i woyld like to know it…
Viresh, Struts 2 is not dependent upon a particular application server (nor is any other library that I’m aware of).
Hi
I am new to Struts 2 . Just started learning.The tutorial is great. But it will be very useful if you upload the complete working example. I am facing a few issues to make this example work. And believe me it is a great feeling if your first example works the first time itself!!!
Hi ketan,
The model is a Java object that holds data such as the Employee object in this example. You may find it helpful to read a bit about the MVC (model, view, controller) design pattern. You’re correct that in Struts 2 there is no ActionForm and that it was merged into the Action itself.
Hi ,
i just started to read struts………..
can anybody tell me best book for struts….and a link to download free e-book for struts.
I had followed the same way you had shown in the sample but getting deployment exception. Please refer the log shown below:
00:29:59,937 INFO [XmlConfigurationProvider] Parsing configuration file [struts-default.xml]
00:29:59,984 ERROR [Dispatcher] Dispatcher initialization failed
Unable to load configuration. – bean – jar:file:/C:/Users/Mayuree/Praveen/projects/Servers/jboss-4.0.4.GA/server/default/tmp/deploy/tmp45324struts2Example-exp.war/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:47:178
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58)
Am i using the wrong jar? or anything else? Can u plz suggest..
its very helpful to everyone to understand how struts2 works.
When i write program using
using this tag i got error in netbeans any one help y this error occur on my program pls help me
Hi
what is default action ref in struts2
thanks,
srinivas
Can any body please upload struts2.0 working examples?
Please update the figure or the code. This is not an end to end example as it doesn’t complile in the present format. Moreover it would be good if you can provide the Employee and EmployeeDao class which goes inside model and dao folders
I am so impressed with this site.
I just started programming and found very informational articles on struts 2.I appreciate the great work
Hi All,
I run the struts2.0 i get this error please give the solution.
My versions jre,jdk,and jvm are 1.6. Tomcat 6.0.
My error:
The requested resource (/struts2-blank-2.0.6/) is not available.
am new to struts2.can u give me detail description of struts2 how it works .how action works how return value is retured and all those basic things