July 5, 2011

Struts 2 - Hello World Example (with Annotations)

1) Create a new Web Application Project in Eclipse. Download the Struts 2 jars and other dependent jars from the link here and copy them into the WebContent/WEB-INF/lib folder of the web application.

2) Copy the following configuration for the Struts2 FilterDispatcher in the web.xml file of the web application

<filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
   <init-param>
      <param-name>actionPackages</param-name>
      <param-value>info.icontraining.struts2</param-value>   
   </init-param>
</filter>
<filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
</filter-mapping>

The actionPackages parameter is used to specify the location (in terms of package names) of the action classes. Multiple packages can be specified by a comma-separated list.

3) Add the AnnotatedNameCollector.java class to the src folder of the web application. This class is an action class. Action classes in Struts2 with annotation-based configuration should either implement the Action interface or extend from the ActionSupport class or use a naming convention where the action class names end in the word 'Action'.

package info.icontraining.struts2;

import org.apache.struts2.config.Result;
import com.opensymphony.xwork2.ActionSupport;

@Result( value="/AnnotatedNameCollector.jsp" )
public class AnnotatedNameCollector extends ActionSupport {

}

The URL to access this action from the browser is derived by making the first character of the action class name smaller and appending the '.action' extension after the rest of the class name. So,

AnnotatedNameCollector.java can be accessed with the URL annotatedNameCollector.action

4) Add the AnnotatedHelloWorldAction class also to the src folder of the web application. In this case, the action class name ends with the word 'Action' and therefore does not need to extend the ActionSupport class or implement the Action interface

package info.icontraining.struts2;

import org.apache.struts2.config.Result;

@Result(name="SUCCESS", value="/HelloWorld.jsp" )
public class AnnotatedHelloWorldAction {

   private static final String GREETING = "Hello ";
     public String execute()  {
      setCustomGreeting( GREETING + getName() );
      return "SUCCESS";
   }

   private String name;
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
    
   private String customGreeting;
  
   public String getCustomGreeting() {
      return customGreeting;
   }
  
   public void setCustomGreeting( String customGreeting ){
      this.customGreeting = customGreeting;
   }
}

The URL to access this action from the browser is derived by making the first character of the action class  name smaller, dropping the 'Action' at the end of the class name and appending the '.action' extension after the rest of the class name. So,

AnnotatedHelloWorldAction.java can be accessed with the URL annotatedHelloWorld.action

5) Create the AnnotatedNameCollector.jsp and HelloWorld.jsp as below and add them to the WebContent folder of the web application

AnnotatedNameCollector.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
      <title>Name Collector</title>
  </head>
  <body>       <s:form action="annotatedHelloWorld">
        <s:textfield name="name" label="Your name"/>
        <s:submit/>
      </s:form>
  </body>
</html>

HelloWorld.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
  <head>
     <title>HelloWorld</title>
  </head>
  <body>       
     <h1><s:property value="customGreeting"/></h1>
  </body>
</html>

6) Test the application by typing the following URL in the browser,

http://localhost:8080/WebAppName/annotatedNameCollector.action

No comments:

Post a Comment