MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to create a web application using struts annotations

In this article, we will learn about struts annotation and how we can configure struts application using annotations. We will also create a sample add to numbers application using struts annotation library.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

Introduction:

A struts application can be configured in two ways using a struts.xml file that is the traditional way of configuration and using annotations. In this tutorial, we are going to talk about the configuration of a struts web application using annotations. In this article, we will go through with sample struts web application configured using annotation classes. This application will perform addition of two given numbers and add action will be performed for the given inputs.

Struts using annotations in detail:

To create a struts application, we are required a bundle of jar files that provides a set of required classes to run a struts web application. The following list names these set of jar files:

  • struts2-convention-plugin-x.y.z.jar
  • asm-x.y.jar
  • antlr-x.y.z.jar
  • commons-fileupload-x.y.z.jar
  • commons-io-x.y.z.jar
  • commons-lang-x.y.jar
  • commons-logging-x.y.z.jar
  • commons-logging-api-x.y.jar
  • freemarker-x.y.z.jar
  • javassist-.xy.z.GA
  • ognl-x.y.z.jar
  • struts2-core-x.y.z.jar
  • xwork-core.x.y.z.jar

These jar files should be kept into the directory path WebContent/WEB-INF/lib directory. Annotation in struts framework are the classes that gives the Meta - data information about the classes and their configuration. We will go through few of the Annotation classes, which are required to configure struts application and their actions to respond a user. Annotation classes take place of struts.xml and its configuration. Hence, we will be required only one configuration file (web.xml) to run this struts web application.

The web.xml file is the main configuration file of web application that is the deployment descriptor for a web application. All landing request to the web application first hits the web.xml file. It is decided by this deployment descriptor, what would be the path to process the landing request. The web.xml file defines a tag that transfers all the request to struts configuration file using struts.xml configuration file or annotations.

Listing 1: 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_3_0.xsd"
   id="WebApp_ID" version="3.0">

   <display-name>Add Two Numbers Struts Application</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>

   <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>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

The first tab of this file defines the xml version and encoding type. The next tag <web-app> defines the schema and deployment type information to web or application server. The sub – tag <display-name> describes the struts application name, once it is deployed on the web or application server. The next sub-tag <welcome-file-list> may contain multiple list tags to define a welcome file, while a web application is being loaded. It is defined using <welcome-file> tag.

The <filter> tag is responsible to redirect all the landing requests to the filter – dispatcher class of struts framework so that the appropriate action could be performed for the desired result. This tag defines <filter-name>, <filter-class> and <init-param> tag to define filter name, class and init parameter names respectively. The <filter-name> of <filter> tag should be same as defined in the sub-tag <filter-name> of <filter-mapping> tag. <url-pattern> tag defines that all the landing requests are being redirected to filter-dispatcher class of the struts framework.

Listing 2: index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Add Two Number</title>
</head>

<body>

   <s:form action="add" method="post">
      <s:textfield name="first" label="first no." size="20" />
      <s:textfield name="second" label="second no." size="20" />
      <s:submit name="submit" label="Submit" align="center" />
   </s:form>
</body>
</html>

The first jsp tag defines the language type, response, character set type and encoding page type. The above file code defines two input fields and a submit button. A struts tag library is used to define these object fields using the define prefix “s” inside the <taglib> jsp tag. The next tag defines a html document type specification for the client application. <s:form> tag creates a form tag that hit to “add” action, once it is submitted. This form tag will process to input parameters defines using <s:textfield> with their specific names “first” and “second” that will be submitted by <s:submit> tag button.

Once the index page has been submitted it should be captured by the “add” action of the application. This action is defined in Add.java class file.

Listing 3: add.java

package com.ajo.actions;

import com.opensymphony.xwork2.ActionSupport;
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.validator.annotations.*;

@Results({
   @Result(name="success", location="/success.jsp"),
   @Result(name="input", location="/index.jsp")
})
public class Add extends ActionSupport{
   private int first;
   private int second;
   private int doAdd;
   
   
   @Action(value="/add")
   public String execute() 
   {
       return SUCCESS;
   }

   @IntRangeFieldValidator(message = "Please enter frist number.")   
   public int getFirst() {
	   return first;
   }


	public void setFirst(int first) {
		this.first = first;
	}
	
	
	@IntRangeFieldValidator(message = "Please enter second number.")
	public int getSecond() {
		return second;
	}

	public void setSecond(int second) {
		this.second = second;
	}
	
	public int getDoAdd() {
		doAdd = getSecond() + getFirst();
		   return doAdd;
	}

	public void setDoAdd(int doAdd) {
		this.doAdd = doAdd;
	}

}

This action is defined inside com.ajo.actions package and extends ActionSupport class. This class is defined in com.opensymphony.xwork2.ActionSupport package. The add.java file also defines two result type “success” and “input”, which are defined using @Results and @Result annotations. When we are having more than one results for an action, these can be summarized using @Results annotation type class.

In the Add class, two data member variables and their getter, setter methods are defined. The getter methods are validated using annotation class @RequiredFieldValidator.

An action url is validated using annotation class @Action that accepts name attribute to match with action url string. This annotation is defined before the execute method that returns a result string. We also define one more integer type variable doAdd and its getter() and setter() methods to perform addition of these two numbers. The getDoAdd() method return addition of the two given numbers.

To display the addition of these two given numbers, we create a new jsp file “success.jsp” will get performed addition result using getter() method of doAdd variable using struts tag library tag and its attribute “value”, where “s” is the struts tag library prefix to access the tag library tags.

Listing 4: success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Addition Performed</title>
</head>

<body>
      Adding Result of given two integers: 
       <s:property value = "doAdd"/>
</body>
</html>
 index.jsp page, accepting two integers

Figure 1: index.jsp page, accepting two integers

The above figure shows the index.jsp page view that accepts two integer to perform addition action of two given integers. This page redirects the data to Add action class that reads data using their getter() methods and perform addition task and assign the data to new variable doAdd variable. The Add action class transfer the control to success.jsp page view, if all goes fine.

success.jsp page view displays addition of two given numbers

Figure 2: success.jsp page view displays addition of two given numbers

As shown in figure 1, two integers 5 and 7 was places as input to perform addition of two given numbers that processed as 12 by Add action class and displayed in success.jsp page as shown into the figure 2.

Directory Structure for Struts Application configured using Annotation

Figure 3: Directory Structure for Struts Application configured using Annotation

The above figure shows the directory structure created into the Eclipse IDE.

Required Tools for the Struts Web Application

  • Eclipse IDE
  • JDK (Java Development Kit)
  • Tomcat Apache Web Server
  • Struts Library

Conclusion:

In this article, we learn:

  • What is struts annotation?
  • How do we configure struts application using annotations?
  • Create a sample add to numbers application using struts annotation library?


Have more than 5 years experience about Java, .Net, C and C++.

What did you think of this post?

Did you like the post?

Help us to keep publishing good contents like this.

SUPPORT US

funded

remaining

[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login