<div dir="ltr" style="text-align: left;">
<div dir="ltr" style="text-align: left;">
<div dir="ltr" style="text-align: left;">
<div dir="ltr" style="text-align: left;">
<div dir="ltr" style="text-align: left;">
<div dir="ltr" style="text-align: left;">
<div dir="ltr" style="text-align: left;">
<div dir="ltr" style="text-align: left;">
<div dir="ltr" style="text-align: left;">
<p>We can achieve Zero Configuration in struts2 using java 5 annotations feature. Previously we normally configured struts.xml for all the configurations . The XML file wires up the action names (employee), with ActionSupport classes (<b>EmployeeAction.java</b>), and with the result to render back to the browser (success.jsp). Struts 2 provides an alternative to using XML to configure your application by using standard naming conventions and annotations for your action names, ActionSupport classes, and results.<br />
We will use annotations in a small example and it is being developed in Eclipse.</p>
<p>Here we will take an example of Employee whose name, emailid, age and telephone would be captured using a simple page and we will put two validation to make sure that use always enter a name , a emailid and a telephone and age should be in between 18 and 65.</p>
<p>To start using annotations in your project, make sure you have included following jar files in your WebRoot/WEB-INF/lib folder:</p>
<ul style="text-align: left;">
<li>struts2-convention-plugin-x.y.z.jar</li>
<li>asm-x.y.jar</li>
<li>asm-commons-x.y.jar</li>
<li>antlr-x.y.z.jar</li>
<li>commons-fileupload-x.y.z.jar</li>
<li>commons-io-x.y.z.jar</li>
<li>commons-lang-x.y.jar</li>
<li>commons-logging-x.y.z.jar</li>
<li>commons-logging-api-x.y.jar</li>
<li>freemarker-x.y.z.jar</li>
<li>javassist-.xy.z.GA</li>
<li>ognl-x.y.z.jar</li>
<li>struts2-core-x.y.z.jar</li>
<li>xwork-core.x.y.z.jar</li>
</ul>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/111331.png" border="0" /></div>
<p> ;</p>
</div>
<h2><b>Create main page:</b></h2>
<p>Let us write main page JSP file<i><b> employee.jsp</b></i>, which will be used to collect Employee related information mentioned above.</p>
<pre class="highlight"><;%@ page contentType="text/html; charset=UTF-8"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
<;html>; 
<;head>; 
<;title>;<;s:property value="getText('global.form')" />; - Struts2 Demo | dineshonjava.com<;/title>; 
<;s:head />; 
<;/head>; 
 
<;body>; 
<;h2>;<;s:property value="getText('global.form')" />;<;/h2>; 
 
<;s:form action="employee" method="post" validate="true" >; 
 <;s:textfield name="name" key="global.name" size="20"/>; 
 <;s:textfield name="age" key="global.age" size="20" />; 
 <;s:textfield name="email" key="global.email" size="20" />; 
 <;s:textfield name="telephone" key="global.telephone" size="20" />; 
 <;s:submit name="submit" key="global.submit" align="center" />; 
<;/s:form>; 
 
<;s:url id="localeEN" namespace="/" action="locale" >; 
 <;s:param name="request_locale" >;en<;/s:param>; 
<;/s:url>; 
<;s:url id="localeHN" namespace="/" action="locale" >; 
 <;s:param name="request_locale" >;hn<;/s:param>; 
<;/s:url>; 
<;s:url id="localeES" namespace="/" action="locale" >; 
 <;s:param name="request_locale" >;es<;/s:param>; 
<;/s:url>; 
<;s:url id="localezhCN" namespace="/" action="locale" >; 
 <;s:param name="request_locale" >;zh_CN<;/s:param>; 
<;/s:url>; 
<;s:url id="localeDE" namespace="/" action="locale" >; 
 <;s:param name="request_locale" >;de<;/s:param>; 
<;/s:url>; 
<;s:url id="localeFR" namespace="/" action="locale" >; 
 <;s:param name="request_locale" >;fr<;/s:param>; 
<;/s:url>; 
 
<;s:a href="%{localeEN}" >;English<;/s:a>; 
<;s:a href="%{localeHN}" >;Hindi<;/s:a>; 
<;s:a href="%{localeES}" >;Spanish<;/s:a>; 
<;s:a href="%{localezhCN}" >;Chinese<;/s:a>; 
<;s:a href="%{localeDE}" >;German<;/s:a>; 
<;s:a href="%{localeFR}" >;France<;/s:a>; 
<;/body>; 
<;/html>; 
</pre>
</div>
<h2><b>Create Views:</b></h2>
<p>We will use JSP file <i><b>success.jsp </b></i>which will be invoked in case defined action returns SUCCESS.</p>
<pre class="highlight"><;%@ page contentType="text/html; charset=UTF-8"%>; 
<;%@ taglib prefix="s" uri="/struts-tags"%>; 
<;html>; 
<;head>; 
<;title>;<;s:property value="getText('global.form')" />; - Struts2 Demo | dineshonjava.com<;/title>; 
<;/head>; 
 
<;body>; 
 <;h2>;<;s:property value="getText('global.success')" />;.<;/h2>; 
<;/body>; 
<;/html>; 
</pre>
<h2><b>Create Action:</b></h2>
<p>This is the place where annotation will be used. Let us re-define action class Employee with annotation, and then add a method called validate() as shown below in <i><b>EmployeeAction.java</b></i> file. Make sure that your action class extends the ActionSupport class, otherwise your validate method will not be executed.</p>
</div>
<p> ;</p>
<pre class="highlight">package com.dineshonjava.struts2.action; 
 
import org.apache.struts2.convention.annotation.Action; 
import org.apache.struts2.convention.annotation.Result; 
import org.apache.struts2.convention.annotation.Results; 
 
import com.opensymphony.xwork2.ActionSupport; 
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator; 
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@Results({ 
 @Result(name="success", location="/success.jsp"), 
 @Result(name="input", location="/employee.jsp") 
}) 
public class EmployeeAction extends ActionSupport { 
 
 private static final long serialVersionUID = 1L; 
 private String name; 
 private Integer age; 
 private String email; 
 private String telephone; 
 
 @Action(value="/employee") 
 public String addEmployee() { 
 return SUCCESS; 
 } 
 
 @RequiredFieldValidator( message = "The name is required" ) 
 public String getName() { 
 return name; 
 } 
 public void setName(String name) { 
 this.name = name; 
 } 
 
 @IntRangeFieldValidator(message = "Age must be in between 28 and 65", 
 min = "18", max = "65") 
 public Integer getAge() { 
 return age; 
 } 
 public void setAge(Integer age) { 
 this.age = age; 
 } 
 
 @RequiredFieldValidator( message = "The email is required" ) 
 public String getEmail() { 
 return email; 
 } 
 public void setEmail(String email) { 
 this.email = email; 
 } 
 
 @RequiredFieldValidator( message = "The phone is required" ) 
 public String getTelephone() { 
 return telephone; 
 } 
 public void setTelephone(String telephone) { 
 this.telephone = telephone; 
 } 
} 
 
</pre>
</div>
<p>We have used few annotations in this example. Let me go through them one by one:</p>
<ul style="text-align: left;">
<li>First, we have included the Results annotation. A Results annotation is a collection of results. Under the results annotation, we have two result annotations. The result annotations have the name that correspond to the outcome of the execute method. They also contain a location as to which view should be served corresponding to return value from <i><b>addEmployee()</b></i>.</li>
<li>The next annotation is the Action annotation. This is used to decorate the <i><b>addEmployee()</b></i> method. The Action method also takes in a value which is the URL on which the action is invoked.</li>
<li>Finally, I have used two validation annotations. I have configured the required field validator on name field and the integer range validator on the age field. I have also specified a custom message for the validations.</li>
</ul>
<h2><b>Configuration Files:</b></h2>
<p>We really do not need <i><b>struts.xml</b></i> configuration file, so let us remove this file and let us check the content of <i><b>web.xml</b></i> file:</p>
</div>
<p> ;</p>
<pre class="highlight"><;?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_3_0.xsd" id="WebApp_ID" version="3.0">; 
 <;display-name>;Struts2I18N<;/display-name>; 
 <;filter>; 
 <;filter-name>;struts2<;/filter-name>; 
 <;filter-class>; 
 org.apache.struts2.dispatcher.FilterDispatcher 
 <;/filter-class>; 
 <;init-param>; 
 <;param-name>;struts.devMode<;/param-name>; 
 <;param-value>;true<;/param-value>; 
 <;/init-param>; 
 <;init-param>; 
 <;param-name>;struts.custom.i18n.resources<;/param-name>; 
 <;param-value>;global<;/param-value>; 
 <;/init-param>; 
 <;/filter>; 
 <;filter-mapping>; 
 <;filter-name>;struts2<;/filter-name>; 
 <;url-pattern>;/*<;/url-pattern>; 
 <;/filter-mapping>; 
 <;welcome-file-list>; 
 <;welcome-file>;employee.jsp<;/welcome-file>; 
 <;/welcome-file-list>; 
<;/web-app>; 
</pre>
</div>
<p>Now, right click on the project name and click Export >; WAR File to create a War file. Then deploy this WAR in the Tomcat&#8217;s webapps directory. Finally, start Tomcat server and try to access<br />
<b>URL http://localhost:8080/doj/employee.jsp. </b></p>
<p>This will give you following screen:</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/66666-1.png" border="0" /></div>
<p><b>Employee error page because of validation fails</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/777.png" border="0" /></div>
<p><b><br />
</b> <b>Employee page on success</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/08/8888.png" border="0" /></div>
</div>
<h2><b>Download Source Code</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/Struts2Annotation.zip?attredirects=0&;d=1" target="_blank" rel="noopener">Struts2Annotation.zip</a></b></h2>
<p> ;</p>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/struts-2-exception-handling/">Previous</a> <;<; || <a href="https://dineshonjava.com/struts-2-baby-step-to-learn/">Index </a>|| >;>;<a href="https://dineshonjava.com/struts-2-control-tags/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/struts-2-exception-handling/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/struts-2-control-tags/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '378'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…