Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Struts Training Day1 Struts 2 configuration files

Related Tutorials


Share on Google+Share on Google+

Struts 2 configuration files

For developing "Hello World" application you need the following files: Add the following code snippet into the index.html file.

Struts 2 configuration files


For developing "Hello World" application you need the following files:

Add the following code snippet into the index.html file.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<title>RoseIndia.Net Struts Hello World Application</title>

<div align="center">
  <table border="0" cellpadding="0" cellspacing="0" width="400">
  <td><font color="#000080" size="5">
<b> Struts 
Hello World Application</b><br>
   <li><a href="roseindia/HelloWorld.action"> Run Struts Hello World Application</a></li>
<p align="center">&nbsp;</p>

Developing Controller Configuration File:

Struts 2 uses the struts.xml file for configuring the application. Create struts.xml file and save it in the "struts2helloworld\WEB-INF\src" directory with the following content.


<?xml version="1.0" encoding="UTF-8" ?>
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

  <!-- Rose India Struts Tutorials -->
  <constant name="struts.enable.DynamicMethodInvocation" value="false" />
  <constant name="struts.devMode" value="true" />

  <package name="roseindia" namespace="/roseindia" extends="struts-default">

  <action name="HelloWorld" class="net.roseindia.Struts2HelloWorld">



The struts.xml file should be present in the class path of the application, you can either include it in the jar and place in the lib directory of the application or place it in the classes directory of the web application. In our application we are using ant build tool which is including it in the jar file. 

Developing Action (to interact with Model):

Now create and saves it to the "struts2helloworld\WEB-INF\src\java\net\roseindia" directory. This action class creates the message to be displayed on the screen. Here is the code of

package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;

public class Struts2HelloWorld extends ActionSupport {

  public static final String MESSAGE = "Struts 2 Hello World Application!";

  public String execute() throws Exception {
  return SUCCESS;

  private String message;

  public void setMessage(String message){
  this.message = message;

  public String getMessage() {
  return message;

Developing View:
This page is used to display the result on the browser. The HelloWorld.jsp is view part of our application. Create "HelloWorld.jsp" in the struts2helloworld\pages directory and add the following content:

<%taglib prefix="s" uri="/struts-tags" %>
  <title>Struts Hello World Application!</title>
  <h2>Welcome to Roseindia Hello World Application</h2>
  <b><s:property value="message" /></b>

The line <%@ taglib prefix="s" uri="/struts-tags" %> declares data tag library of struts. The struts data tag is used to display the dynamic data. The tag <s:property value="message" /> calls the methods getMessage() respectively of the Struts2HelloWorld action class and merges the values with response.

Download "Hello World" Application



If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 18, 2008

Related Tutorials

Discuss: Struts 2 configuration files   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
September 18, 2011
struts.xml need to be at classpath

thanks for sharing. Just a quick add-on, struts.xml need to be at classpath/