In this tutorial, We will discuss about hello world application using annotation and how to apply annotations to java classes and methods. In the Struts 2.2.1 examples we use a struts configuration file 'struts.xml' for configure and action mapping of the application with the Action.java class and jsp file. Now for action mapping and configuration of struts application struts2.2.1 provide a alternative to the struts.xml file as standard naming convention and annotations for action mapping and configuration.
In this example we enter the username and get a welcome message form the application as a output.
We can enable annotations and standard naming convention by including struts2-convention-plugin in WEB-INF/lib folder. In the struts2.2.1 we use the struts2-convention-plugin-2.2.1.jar file for enabling annotation in the application and use the <init-param> tag as given in the web.xml file.
The following Example will shows how to implement the annotation in the Struts2.2.1 --
Directory structure of the Hello world application using annotation-

First we create a JSP file named index.jsp as follows.
|
<%@ 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>Annotation example</title></ head>< body>< h1>Annotation Example</h1>< hr>< s:form action="welcome"> <s:textfield name="username" label="UserName" /> <s:submit /></ s:form></ body></ html> |
Here is the welcome.jsp
|
<%@ 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>Annotation example</title></ head>< body>< h1>${message}</h1></ body></ html> |
Here is the web.xml
|
<? xml version="1.0" encoding="UTF-8"?>< web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>AnnotationExample</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> <init-param> <param-name>struts.devMode</param-name> <param-value>true</param-value> </init-param> </filter><filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list><welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
The action class Welcome.java is as follows.
|
package roseindia.action;import com.opensymphony.xwork2.ActionSupport;public class Welcome extends ActionSupport { private String username; private String message; public String execute() throws Exception { message = "Welcome Dear " + "'" + username + "'"; return SUCCESS;} public void setUsername(String username) { this.username = username;} public void setMessage(String message) { this.message = message;} public String getMessage() { return message;} public String getUsername() { return username;} } |
This Program produces output on the basis of the Annotation evaluation, This give the output as-
Output:-

