Tuesday, June 30, 2009

Struts2 - Hello World!!

A quick Hello World web app using Struts2 deployed on a Tomcat. The directory structure is that of a normal web app:
 
/Struts2-Example/
/index.jsp
/HelloWorld.jsp
/WEB-INF/
/web.xml
/classes/
/struts.xml
/networked/
/HelloWorld.class
/lib/
/commons-logging-1.1.jar
/freemarker-2.3.8.jar
/ognl-2.6.11.jar
/struts2-core-2.0.11.jar
/xwork-2.0.4.jar
 

Following three snippets provide the code for the three main components for the app.
  1. <%@ page contentType="text/html; charset=UTF-8" %>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <html>  
  4.  <head><title>Name Collector</title></head>  
  5.    
  6.  <body>  
  7.   <h4>Enter your name: </h4>  
  8.   <s:form action="HelloWorld">  
  9.    <s:textfield name="name" label="Your name"/>  
  10.    <s:submit/>  
  11.   </s:form>  
  12.  </body>  
  13. </html>  
 
The index.jsp file submits a name to the HelloWorld.java class:
 
  1. package networked;  
  2.   
  3. public class HelloWorld {  
  4.  private static final String GREETING = "Hello ";  
  5.    
  6.  public String execute() {  
  7.   setCustomGreeting(GREETING + getName());  
  8.   return "SUCCESS";  
  9.  }  
  10.    
  11.  private String name, customGreeting;  
  12.    
  13.  public String getName() {  
  14.   return this.name;  
  15.  }  
  16.    
  17.  public void setName(String name) {  
  18.   this.name = name;  
  19.  }  
  20.    
  21.  public String getCustomGreeting() {  
  22.   return this.customGreeting;  
  23.  }  
  24.    
  25.  public void setCustomGreeting(String customGreeting) {  
  26.   this.customGreeting = customGreeting;  
  27.  }  
  28. }  
 
The HelloWorld.java class creates a custom message, which the following HelloWorld.jsp page picks up:
 
  1. <%@ page contentType="text/html; charset=UTF-8" %>  
  2. <%@ taglib prefix="s" uri="/struts-tags" %>  
  3. <html>  
  4.  <head><title>HelloWorld</title></head>  
  5.  <body>  
  6.   <h3>Custom Greeting Page</h3>  
  7.   <h4><s:property value="customGreeting"/></h4>  
  8.  </body>  
  9. </html>  
 
The deployment descriptor web.xml maps the FilterDispatcher to all the URLs:
 
  1. <web-app id="WebApp_ID" 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">  
  2.     
  3.  <display-name>Example 2</display-name>  
  4.    
  5.  <filter>  
  6.   <filter-name>struts2</filter-name>  
  7.   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  8.  </filter>  
  9.    
  10.  <filter-mapping>  
  11.   <filter-name>struts2</filter-name>  
  12.   <url-pattern>/*</url-pattern>  
  13.  </filter-mapping>  
  14.    
  15.  <welcome-file-list>  
  16.   <welcome-file>index.jsp</welcome-file>  
  17.  </welcome-file-list>  
  18. </web-app>  
 
And, the struts.xml
 
  1. <!DOCTYPE struts PUBLIC  
  2.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  3.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  4. <struts>  
  5.  <constant name="struts.devMode" value="true"> </constant>  
  6.    
  7.  <package name="default" extends="struts-default">   
  8.   <action name="HelloWorld" class="networked.HelloWorld">  
  9.    <result name="SUCCESS">/HelloWorld.jsp</result>  
  10.   </action>  
  11.  </package>  
  12. </struts>