<p>We are going to explain the entire example on the Struts 2 annotations. we
have to follow the given instructions or steps to make a proper working
annotation application using the eclipse IDE.</p>
<p>The steps need to be followed are as follows:-</p>
<p> Step 1:- Create the dynamic project as go to<b> new prokect</b>
> <b>dynamic web project</b> and create the view file <b>index,jsp</b> in >
<b>web content folder.</b></p>
<div style="width:600px; height:auto; border:1px solid gray; background-color:#ccc;">
<font color="#bf5f3f" size="2">
</font><p align="LEFT"><font color="#bf5f3f" size="2"><%@</font><font size="2"> </font><font color="#3f7f7f" size="2">
page</font><font size="2"> </font><font color="#7f007f" size="2">language</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"java"</i></font><font size="2">
</font><font color="#7f007f" size="2">contentType</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"text/html;
charset=ISO-8859-1"
</i></font><font color="#7f007f" size="2">pageEncoding</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"ISO-8859-1"</i></font><font color="#bf5f3f" size="2">%></font></p><font color="#bf5f3f" size="2">
</font><p align="LEFT"><font color="#bf5f3f" size="2"><%@</font><font size="2"> </font><font color="#3f7f7f" size="2">
taglib</font><font size="2"> </font><font color="#7f007f" size="2">prefix</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"s"</i></font><font size="2">
</font><font color="#7f007f" size="2">uri</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"/struts-tags"</i></font><font color="#bf5f3f" size="2">%></font></p><font color="#bf5f3f" size="2">
</font><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">DOCTYPE</font><font size="2">
</font><font color="#008080" size="2">html</font><font size="2"> </font>
<font color="#808080" size="2">PUBLIC</font><font size="2"> </font>
<font color="#008080" size="2">"-//W3C//DTD HTML 4.01 Transitional//EN"</font><font size="2">
</font></font></p><font color="#008080" size="2"><font size="2">
</font><font color="#3f7f5f" size="2">
</font><p align="LEFT"><font color="#3f7f5f" size="2">"http://www.w3.org/TR/html4/loose.dtd"</font><font color="#008080" size="2">></font></p><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">html</font><font color="#008080" size="2">></font></p><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">head</font><font color="#008080" size="2">></font></p><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">title</font><font color="#008080" size="2">></font><font size="2">Employee
info</font><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">title</font><font color="#008080" size="2">></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">head</font><font color="#008080" size="2">></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font><font size="2">
</font><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">body</font><font color="#008080" size="2">></font></p><font color="#008080" size="2">
</font><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">s:form</font><font size="2">
</font><font color="#7f007f" size="2">action</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"empinfo"</i></font><font size="2">
</font><font color="#7f007f" size="2">method</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"post"</i></font><font color="#008080" size="2">></font></p><font color="#008080" size="2">
</font><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">s:textfield</font><font size="2">
</font><font color="#7f007f" size="2">name</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"name"</i></font><font size="2">
</font><font color="#7f007f" size="2">label</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"Name"</i></font><font size="2">
</font><font color="#7f007f" size="2">size</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"20"</i></font><font size="2">
</font><font color="#008080" size="2">/></font></p><font color="#008080" size="2">
</font><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">s:textfield</font><font size="2">
</font><font color="#7f007f" size="2">name</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"age"</i></font><font size="2">
</font><font color="#7f007f" size="2">label</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"Age"</i></font><font size="2">
</font><font color="#7f007f" size="2">size</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"20"</i></font><font size="2">
</font><font color="#008080" size="2">/></font></p><font color="#008080" size="2">
</font><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">s:submit</font><font size="2">
</font><font color="#7f007f" size="2">name</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"submit"</i></font><font size="2">
</font><font color="#7f007f" size="2">label</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"Submit"</i></font><font size="2">
</font><font color="#7f007f" size="2">align</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"center"</i></font><font size="2">
</font><font color="#008080" size="2">/></font></p><font color="#008080" size="2">
</font><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">s:form</font><font color="#008080" size="2">></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font><font size="2">
</font><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">body</font><font color="#008080" size="2">></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font><p><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">html</font><font color="#008080" size="2">></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font>
</font></font></font></font></font></font></div><font color="#008080" size="2"><font color="#008080" size="2">
<p> Step 2:- Create another view <b>success.jsp</b></p>
<div style="width:600px; height:auto; border:1px solid gray; background-color:#ccc;">
<font color="#bf5f3f" size="2">
</font><p align="LEFT"><font color="#bf5f3f" size="2"><%@</font><font size="2"> </font><font color="#3f7f7f" size="2">
page</font><font size="2"> </font><font color="#7f007f" size="2">language</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"java"</i></font><font size="2">
</font><font color="#7f007f" size="2">contentType</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"text/html;
charset=ISO-8859-1"</i></font></p><font color="#2a00ff" size="2"><i>
</i></font><font size="2">
</font><p align="LEFT"><font color="#7f007f" size="2">pageEncoding</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"ISO-8859-1"</i></font><font color="#bf5f3f" size="2">%></font></p><font color="#bf5f3f" size="2">
</font><p align="LEFT"><font color="#bf5f3f" size="2"><%@</font><font size="2"> </font><font color="#3f7f7f" size="2">
taglib</font><font size="2"> </font><font color="#7f007f" size="2">prefix</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"s"</i></font><font size="2">
</font><font color="#7f007f" size="2">uri</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"/struts-tags"</i></font><font color="#bf5f3f" size="2">%></font></p><font color="#bf5f3f" size="2">
</font><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">DOCTYPE</font><font size="2">
</font><font color="#008080" size="2">html</font><font size="2"> </font>
<font color="#808080" size="2">PUBLIC</font><font size="2"> </font>
<font color="#008080" size="2">"-//W3C//DTD HTML 4.01 Transitional//EN"</font><font size="2">
</font></font></p><font color="#008080" size="2"><font size="2">
</font><font color="#3f7f5f" size="2">
</font><p align="LEFT"><font color="#3f7f5f" size="2">"http://www.w3.org/TR/html4/loose.dtd"</font><font color="#008080" size="2">></font></p><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">html</font><font color="#008080" size="2">></font></p><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">head</font><font color="#008080" size="2">></font></p><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">title</font><font color="#008080" size="2">></font><font size="2">Success</font><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">title</font><font color="#008080" size="2">></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">head</font><font color="#008080" size="2">></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">body</font><font color="#008080" size="2">></font></p><font color="#008080" size="2">
</font><font size="2">
<p align="LEFT">Employee Information is entered successfully.</p>
</font><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">body</font><font color="#008080" size="2">></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font><p><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">html</font><font color="#008080" size="2">></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font>
</font></font></font></font></font></div><font color="#008080" size="2"><font color="#008080" size="2">
<p> Step 3:- Create the Action class <b>Employee.java</b></p>
<div style="width:600px; height:auto; border:1px solid gray; background-color:#ccc;">
<font color="#7f0055" size="2"><b>
</b></font><p align="LEFT"><font color="#7f0055" size="2"><b>package</b></font><font size="2"> r4r;</font></p><font size="2">
</font><font color="#7f0055" size="2"><b>
</b></font><p align="LEFT"><font color="#7f0055" size="2"><b>import</b></font><font size="2">
com.opensymphony.xwork2.ActionSupport;</font></p><font size="2">
</font><font color="#7f0055" size="2"><b>
</b></font><p align="LEFT"><font color="#7f0055" size="2"><b>import</b></font><font size="2">
org.apache.struts2.convention.annotation.Action;</font></p><font size="2">
</font><font color="#7f0055" size="2"><b>
</b></font><p align="LEFT"><font color="#7f0055" size="2"><b>import</b></font><font size="2">
org.apache.struts2.convention.annotation.Result;</font></p><font size="2">
</font><font color="#7f0055" size="2"><b>
</b></font><p align="LEFT"><font color="#7f0055" size="2"><b>import</b></font><font size="2">
org.apache.struts2.convention.annotation.Results;</font></p><font size="2">
</font><font color="#7f0055" size="2"><b>
</b></font><p align="LEFT"><font color="#7f0055" size="2"><b>import</b></font><font size="2">
com.opensymphony.xwork2.validator.annotations.*;</font></p><font size="2">
</font><font color="#646464" size="2">
</font><p align="LEFT"><font color="#646464" size="2">@Results</font><font size="2">({</font></p><font size="2">
</font><p align="LEFT"><font color="#646464" size="2">@Result</font><font size="2">(name=</font><font color="#2a00ff" size="2">"success"</font><font size="2">,
location=</font><font color="#2a00ff" size="2">"/success.jsp"</font><font size="2">),</font></p><font size="2">
</font><p align="LEFT"><font color="#646464" size="2">@Result</font><font size="2">(name=</font><font color="#2a00ff" size="2">"input"</font><font size="2">,
location=</font><font color="#2a00ff" size="2">"/index.jsp"</font><font size="2">)</font></p><font size="2">
<p align="LEFT">})</p>
</font><font color="#7f0055" size="2"><b>
</b></font><p align="LEFT"><font color="#7f0055" size="2"><b>public</b></font><font size="2"> </font>
<font color="#7f0055" size="2"><b>class</b></font><font size="2"> Employee
</font><font color="#7f0055" size="2"><b>extends</b></font><font size="2">
ActionSupport {</font></p><font size="2">
</font><p align="LEFT"><font color="#7f0055" size="2"><b>private</b></font><font size="2">
</font><font color="#7f0055" size="2"><b>static</b></font><font size="2"> </font>
<font color="#7f0055" size="2"><b>final</b></font><font size="2"> </font>
<font color="#7f0055" size="2"><b>long</b></font><font size="2"> </font>
<font color="#0000c0" size="2"><i>serialVersionUID</i></font><font size="2"> =
1L;</font></p><font size="2">
</font><p align="LEFT"><font color="#7f0055" size="2"><b>private</b></font><font size="2">
String </font><font color="#0000c0" size="2">name</font><font size="2">;</font></p><font size="2">
</font><p align="LEFT"><font color="#7f0055" size="2"><b>private</b></font><font size="2">
</font><font color="#7f0055" size="2"><b>int</b></font><font size="2"> </font>
<font color="#0000c0" size="2">age</font><font size="2">;</font></p><font size="2">
</font><p align="LEFT"><font color="#646464" size="2">@Action</font><font size="2">(value=</font><font color="#2a00ff" size="2">"/empinfo"</font><font size="2">)</font></p><font size="2">
</font><p align="LEFT"><font color="#7f0055" size="2"><b>public</b></font><font size="2">
String execute() </font></p><font size="2">
<p align="LEFT">{</p>
</font><p align="LEFT"><font color="#7f0055" size="2"><b>return</b></font><font size="2">
</font><font color="#0000c0" size="2"><i>SUCCESS</i></font><font size="2">;</font></p><font size="2">
<p align="LEFT">}</p>
</font><p align="LEFT"><font color="#646464" size="2">@RequiredFieldValidator</font><font size="2">(
message = </font><font color="#2a00ff" size="2">"The name is required"</font><font size="2">
)</font></p><font size="2">
</font><p align="LEFT"><font color="#7f0055" size="2"><b>public</b></font><font size="2">
String getName() {</font></p><font size="2">
</font><p align="LEFT"><font color="#7f0055" size="2"><b>return</b></font><font size="2">
</font><font color="#0000c0" size="2">name</font><font size="2">;</font></p><font size="2">
<p align="LEFT">}</p>
</font><p align="LEFT"><font color="#7f0055" size="2"><b>public</b></font><font size="2">
</font><font color="#7f0055" size="2"><b>void</b></font><font size="2">
setName(String name) {</font></p><font size="2">
</font><p align="LEFT"><font color="#7f0055" size="2"><b>this</b></font><font size="2">.</font><font color="#0000c0" size="2">name</font><font size="2">
= name;</font></p><font size="2">
<p align="LEFT">}</p>
</font><p align="LEFT"><font color="#646464" size="2">@IntRangeFieldValidator</font><font size="2">(message
= </font><font color="#2a00ff" size="2">"Age must be in between 28 and 65"</font><font size="2">,</font></p><font size="2">
</font><p align="LEFT"><font size="2">min = </font><font color="#2a00ff" size="2">"29"</font><font size="2">,
max = </font><font color="#2a00ff" size="2">"65"</font><font size="2">)</font></p><font size="2">
</font><p align="LEFT"><font color="#7f0055" size="2"><b>public</b></font><font size="2">
</font><font color="#7f0055" size="2"><b>int</b></font><font size="2"> getAge()
{</font></p><font size="2">
</font><p align="LEFT"><font color="#7f0055" size="2"><b>return</b></font><font size="2">
</font><font color="#0000c0" size="2">age</font><font size="2">;</font></p><font size="2">
<p align="LEFT">}</p>
</font><p align="LEFT"><font color="#7f0055" size="2"><b>public</b></font><font size="2">
</font><font color="#7f0055" size="2"><b>void</b></font><font size="2"> setAge(</font><font color="#7f0055" size="2"><b>int</b></font><font size="2">
age) {</font></p><font size="2">
</font><p align="LEFT"><font color="#7f0055" size="2"><b>this</b></font><font size="2">.</font><font color="#0000c0" size="2">age</font><font size="2">
= age;</font></p><font size="2">
<p align="LEFT">}</p>
<p align="LEFT">}</p>
</font>
</div>
<p align="LEFT">Step 4:- Create the configuration file <b>web.xml </b></p>
<div style="width:600px; height:auto; border:1px solid gray; background-color:#ccc;">
<font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><!--?</font--><font color="#3f7f7f" size="2">xml</font><font size="2">
</font><font color="#7f007f" size="2">version</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"1.0"</i></font><font size="2">
</font><font color="#7f007f" size="2">encoding</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"UTF-8"</i></font><font color="#008080" size="2">?></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">web-app</font><font size="2">
</font><font color="#7f007f" size="2">xmlns:xsi</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"http://www.w3.org/2001/XMLSchema-instance"</i></font><font size="2">
</font><font color="#7f007f" size="2">xmlns</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"http://java.sun.com/xml/ns/javaee"</i></font><font size="2">
</font><font color="#7f007f" size="2">xsi:schemaLocation</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"</i></font><font size="2">
</font><font color="#7f007f" size="2">version</font><font size="2">=</font><font color="#2a00ff" size="2"><i>"3.0"</i></font><font color="#008080" size="2">></font></p><font color="#008080" size="2">
</font><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">display-name</font><font color="#008080" size="2">></font><font size="2">Login</font><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">display-name</font><font color="#008080" size="2">></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">welcome-file-list</font><font color="#008080" size="2">></font></p><font color="#008080" size="2">
</font><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">welcome-file</font><font color="#008080" size="2">></font><font size="2">index.jsp</font><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">welcome-file</font><font color="#008080" size="2">></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">welcome-file-list</font><font color="#008080" size="2">></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">filter</font><font color="#008080" size="2">></font><font size="2">
</font></p><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">filter-name</font><font color="#008080" size="2">></font><font size="2">struts2</font><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">filter-name</font><font color="#008080" size="2">></font><font size="2">
</font></font></p><font color="#008080" size="2"><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">filter-class</font><font color="#008080" size="2">></font><font size="2">
</font></p><font size="2">
<p align="LEFT">
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </p>
</font><p align="LEFT"><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">filter-class</font><font color="#008080" size="2">></font><font size="2">
</font></font></p><font color="#008080" size="2"><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">filter</font><font color="#008080" size="2">></font><font size="2">
</font></font></p><font color="#008080" size="2"><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">filter-mapping</font><font color="#008080" size="2">></font><font size="2">
</font></p><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">filter-name</font><font color="#008080" size="2">></font><font size="2">struts2</font><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">filter-name</font><font color="#008080" size="2">></font><font size="2">
</font></font></p><font color="#008080" size="2"><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><</font><font color="#3f7f7f" size="2">url-pattern</font><font color="#008080" size="2">></font><font size="2">/*</font><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">url-pattern</font><font color="#008080" size="2">></font><font size="2">
</font></font></p><font color="#008080" size="2"><font size="2">
</font><p align="LEFT"><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">filter-mapping</font><font color="#008080" size="2">></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font><font size="2">
</font><font color="#008080" size="2">
</font><p><font color="#008080" size="2"><!--</font--><font color="#3f7f7f" size="2">web-app</font><font color="#008080" size="2">></font></font></p><font color="#008080" size="2"><font color="#008080" size="2">
</font>
</font></font></font></font></font></font></font></font></font></font></font></div><font color="#008080" size="2"><font color="#008080" size="2">
<p> </p>
<p>As there are used the annotations then we really don't need struts .xml now
we can do all with the annotations only. this is the benefit of annotation
feature in struts 2</p>
<p>These are some very general and easy steps to learn, how to use annotation in
Struts2.</p>
<p>Now you can download the Files from hear:-</p>
<p><button type="button"><a href="http://r4r.in/struts/struts-application-examples/Annoatation.zip">Download project in zip</a></button>
<button type="button"><a href="#">Download War</a></button>
<button type="button"><a href="#">Download Lib</a></button>
</p>
</font></font></font></font></font></font>