A Step-by-Step Tutorial on How to Create helloWorld Application

Average rating:
Covered in this article is the step-by-step process of developing a simple helloWorld application using Struts 2 Framework. I decided to create this article because I was tired of looking up so many examples online that do not actually work and give you so much time for debugging. This helloWorld application serves as your stepping stone for developing bigger and complex applications using Struts 2 Framework. In this tutorial you will learn how to create directory structure, configuring web.xml and struts.xml, and your JSP. Also, we will be using an XML-BASED DECLARATIVE ARCHITECTURE.
Before we begin, I would like to give a brief introduction about the Framework. Apache Struts 2 is not just a new release of Struts 1. It is a brand new release which is originally known as WebWork2. After several years of working independently, open-source societies Struts and WebWork combined forces to create Struts 2. Struts 2 provides and encourages everyone to implement Model-View-Controller (MVC) architecture. MVC is an architectural pattern that separates business logic from the presentation logical codes. You have probably already researched Struts 2, so begin by exactly following the steps below.
Step 1:

Set up your tools for development.

After downloading all those tools, extract them. I presume that you already installed Java 6.

Step 2:

Create helloWorld directory structure.

  • Open eclipse IDE.
  • Create a Dynamic Web Project.

              File > New > Other > Dynamic Web Project > Next
              Type "Sample" on Project Name and click Finish

  • Copy necessary Struts 2 jar files to lib Folder.

              commons-fileupload-1.2.1.jar, commons-logging-1.1.jar, freemarker-2.3.12.jar,   

              ognl-2.6.11.jar, struts2-core-2.1.6.jar, xwork-2.1.2.jar

  • Create necessary files and folders. 

             > Sample
                > Java Resources: src

                >> sample(package)

                >>> HelloWorld.java 

                >> struts.xml

                > WebContent

                >> pages

                >>> helloWorld.jsp

                >> WEB-INF

                >>> lib

                >>>> commons-fileupload-1.2.1.jar

                >>>> commons-logging-1.1.jar

                >>>> freemarker-2.3.12.jar

                >>>> ognl-2.6.11.jar

                >>>> struts2-core-2.1.6.jar

                >>>> xwork-2.1.2.jar

                >>> web.xml

                >>index.html

Step 3:

Configure your web.xml by adding the following code.

 <?xml version="1.0" encoding="UTF-8"?>
<web-app
id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Sample Struts 2</display-name>
 
  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
 
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
 
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
 
</web-app>

Step 4:

Configure your struts.xml adding the following code below.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
    <constant name="struts.devMode" value="false"/>
    
    <package name="sample" namespace="/sample" extends="struts-default">
        <action name="HelloWorld" class="sample.HelloWorld">
            <result>/pages/helloWorld.jsp</result>
        </action>
    </package>
</struts>

Step 5:

Code your helloWorld.java by adding the following code below.

package sample;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport{

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
    public String execute() throws Exception {
        setMessage("Hello World!");
        return SUCCESS;
    }
    

}

Step 6:

Code your index.html by adding the following code below.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World Application</title>

</head>
<body>

<div align=left><font color="#00000">To your information this is your <b>index.html</b> page under WebContent directiory.</font></div>

<br><br><br>
<a href="sample/HelloWorld.action">Click Here</a>

</body>

Step 7:

Code your helloWorld.jsp by adding the following code below.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>

</head>
<body>
<div align=left><font color="#00000">To your information this is your <b>helloWorld.jsp</b> page under WebContent directiory.</font></div>
<br>
<br>
<center><h1><s:property value="message"/></h1></center>
</body>
</html>

Step 8:

Run the helloWorld Application on a Tomcat Server.

  • Left Click on the Servers tab on eclipse IDE.
  • Right Click > New > Server
  • Select Tomcat v6.0 Server under Apache and Left Click Next.
  • Browse the Tomcat installation directory.
  • Select java-6-sun-1.6.0.07 and Left Click Finish.
  • Left Click Project.
  • Go to Run > Run As > Run on Server

That's it! Eclipse web browser will appear on your IDE with your project up and running. Congratulations! You just created your first Struts 2 application.

 

 

Comments

Perfect. I was looking for just a basic setup for Struts 2, Eclipse, and Tomcat. This worked perfectly, and now I have everything setup and ready to start playing with. Thanks a ton.

Hi,
I am new to struts. When i tried this code, as given above. i got this error. Can anyone tell me what to do now to rectify this error

EVERE: Exception starting filter struts2
java.lang.ClassNotFoundException: org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1358)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:249)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3693)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4340)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Jul 19, 2009 6:16:31 PM org.apache.catalina.core.StandardContext start
SEVERE: Error filterStart

@ sharma vivek : hi! Please double check your web.xml. Make sure its exactly the same as above... Also check your jar files...

hi!! how i should include java files in src(Java Resources),how it compile from src.please guide me

hi!! thanks for this information.I want to know,we developed project(struts2) on Eclipse,what format (files or setup) i have deliver to client ,so that it can run on client machine(where tomcat server is there).will u please me provide me guidance abt it.thanks

Thanks a lot man
i was stucked in this HTTP 404 error from 2 days n u saved my life..
keep posting gud work :)

There is no Action mapped for action name HelloWorld. - [unknown location]
at com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:177)
at org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
at org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
at com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:458)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:595)
I'm getting this error.I've mapped the action class in struts.xml like you mentioned. Where am i going wrong?

Pls anybody knows solution for the above problem
I can't understand how it works for the other people

@ Manoj Kumar:

hi there! The error says "There is no Action mapped for action name HelloWorld" probably because the app could not find the action you mapped on struts.xml. Please check if your struts.xml equals the struts content above. Better yet, copy and paste the code above to avoid typo errors.

and may be that u struts.xml is not under src next to the package sample
i have the same pb and when i moved it, it works
thx and good luck