MVC Example using Struts 2 and Tomcat 6

To do a quick introduction to Struts2 framework on tomcat. This example is fairly easy, however it will highlight the use of most of the fundamentals of struts. Leave any questions or problems as comments and I will endeavour to answer them.

You have a compatible version of the JDK installed and tomcat installed and running.

Versions used in this example
Windows XP SP2N/A
Struts 2
JDK 1.5.0N/A
Links to these files can be found here.

Although this is a quick 'helloworld' tutorial we will not sacrifice neatness for speed - so we will follow the standard example web application directory structure.

Create this directory structure in your working directory- similar to something you would get from eclipse. The structure used in this example is given below. Struts2HelloWorld will be referred to as your project or working directory. Create this structure.


Unzip/Install Struts2
  1. Download and unzip/install the struts2 framework. For the purposes of this example the install directory is D:\downloads\struts-2.1.6
  2. `
  3. Navigate to the app directory in your Struts2 install directory.
  4. `
  5. Unzip the struts2-blank-2.1.6.war file.
  6. `
  7. Go to the struts2-blank-2.1.6\WEB-INF\lib and copy all the files to your 'lib' directory under Struts2HelloWorld. These are the core files required to run our example.

Create the HTML/JSP pages
  1. Create the index.html in the View directory under your project directory.
     1. <html>
     2. <head>
     3. <title>StrutsHelloWorld</title>
     4. </head>
     5. <body>
     6.     <a href="SubmitInfo.jsp">Enter details here</a>
     7.     <br/>
     8.     <a href="../helloworld/StrutsHelloWorldCallMe.action">Call the "CallMe" function</a>
     9. </body>
    10. </html>
    Hide line numbers
  2. `
  3. Create the SubmitInfo.jsp file and save it under View directory,
     1. <%@ page contentType="text/html; charset=UTF-8" %>
     2. <%@ taglib prefix="s" uri="/struts-tags" %>
     3. <html>
     4. <head>
     5.     <title>Hello World</title>
     6. </head>
     7. <body>
     9. <s:if test="%{emailAddress==''}">
    10.     <s:text name="Greetings"/>
    11. </s:if>
    13. <s:form namespace="/helloworld" action="StrutsHelloWorldFormSubmit">
    14.     <s:textfield  label="First Name" name="firstName" />
    15.     <s:textfield  label="Email Address" name="emailAddress"/>
    16.     <s:submit/>
    17. </s:form>
    19. </body>
    20. </html>
    Hide line numbers

Create the struts support files
  1. Create the web.xml in the WEB-INF directory. This is a standard file web.xml file with a filter for struts' actions. You can copy this from struts2-blank-2.1.6\WEB-INF directory if you wish,
     1. <?xml version="1.0" encoding="UTF-8"?>
     2. <web-app id="WebApp_9" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
     4.     <display-name>Struts Blank</display-name>
     6.     <filter>
     7.         <filter-name>struts2</filter-name>
     8.         <filter-class></filter-class>
     9.     </filter>
    11.     <filter-mapping>
    12.         <filter-name>struts2</filter-name>
    13.         <url-pattern>/*</url-pattern>
    14.     </filter-mapping>
    16.     <welcome-file-list>
    17.         <welcome-file>index.html</welcome-file>
    18.     </welcome-file-list>
    20. </web-app>
    Hide line numbers
  2. `
  3. Create the struts.xml in the WEB-INF\classes directory.
     1. <!DOCTYPE struts PUBLIC
     2.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
     3.         "">
     5. <struts>
     6.     <constant name="struts.enable.DynamicMethodInvocation" value="false" />
     8.     <package name="helloworld" namespace="/helloworld" extends="struts-default">
    10.         <action name="StrutsHelloWorld" class="helloworld.StrutsHelloWorldController">
    11.             <result>/View/SubmitInfo.jsp</result>
    12.         </action>
    14.         <action name="StrutsHelloWorldFormSubmit" 
    15.             class="helloworld.StrutsHelloWorldController">
    16.             <result name="input">/HelloWorld/SubmitInfo.jsp</result>        
    17.             <result>/View/ThankYou.jsp</result>
    18.         </action>
    20.         <action name="StrutsHelloWorldCallMe" method="CallMe" 
    21.             class="helloworld.StrutsHelloWorldController">
    22.             <result name="input">/HelloWorld/ThankYou.jsp</result>
    23.             <result>/View/ThankYou.jsp</result>
    24.         </action>
    26.     </package>
    27. </struts>
    Hide line numbers
  4. `
  5. Create the file in the WEB-INF\classes\helloworld directory and add this line in it,
     1. Greetings = Please enter your email address.
    Hide line numbers

Create and compile the Class(Bean)
  1. Create the class that will do all the work,
     1. package helloworld;
     3. import com.opensymphony.xwork2.ActionSupport;
     5. public class StrutsHelloWorldController extends ActionSupport{
     7.     public String execute() throws Exception {
     8.         if( getFirstName()!=null && getFirstName().length() !=0 && 
     9.                 getEmailAddress()!=null && getEmailAddress().length() !=0)
    10.             return SUCCESS;
    11.         else 
    12.             return INPUT;
    13.     }
    15.     public String CallMe() throws Exception{
    16.         setFirstName("Sheena");
    17.         setEmailAddress("");
    18.         return SUCCESS;
    19.     }
    21.     private String firstName;
    22.     public String getFirstName(){
    23.         return firstName;
    24.     }
    25.     public void setFirstName(String firstName){
    26.         this.firstName=firstName;
    27.     }
    29.     private String emailAddress;
    30.     public void setEmailAddress(String emailAddress){
    31.         this.emailAddress=emailAddress;
    32.     }
    33.     public String getEmailAddress(){
    34.         return emailAddress;
    35.     }
    36. }
    Hide line numbers
    Save this as HelloWorldController in the WEB-INF\src\java\helloworld directory under the project directory.
  2. `
  3. Open a prompt, cd to the src\java directory and compile. Make sure -extdirs is pointing to your local struts directory. This will compile and copy the classes to the classes directory,
    ...\Struts2HelloWorld\WEB-INF\src\java>javac -extdirs D:\downloads\struts-2.1.6\lib -d ..\..\classes helloworld\*.java
  4. `
  5. Copy the Struts2HelloWorld directory and paste it in your tomcat installations webapps directory. You might have to restart tomcat after this.

  1. Open up your browser and type
  2. `
  3. Select the links to test. Leaving the "Email Address" blank causes a message to be shown.

Back to the tutorial trail | Home


sravan said...

Can anyone post how to configure struts2 with axis on tomcat6 using bottom up approach

Anonymous said...

no idea, dude :)

righteous said...

It's pretty straightforward to configure/install struts 2 on tomcat.

If you have tomcat installed it's just a matter of unzipping the stuts-x war file.

If you are not maintaining legacy code, maybe you should be looking at 'Spring'

suba said...

I got the following error.
Pl help me..
\sem3\cloudcomputing\WS\axis2-1.4.1-bin(6)\axis2-1.4.1\bin\wsdl2java.bat -uri ht
'D:\studies\M.Tech\sem3\cloudcomputing\WS\axis2-1.4.1-bin' is not recognized as
an internal or external command,
operable program or batch file.

righteous said...

you may have a space after your ..4.1-bin. This is just the command line or your unix shell not being able to find wsdl2java.bat. Make sure there are no spaces in the command