Struts2 Example index


First Struts2 Application on Tomcat Web Server v6


Actions in Struts2

The action classes are core concepts in Struts2. It is Model class and acts as controller for processing on the data. So actions are the core part of Struts2 framework.


More in Struts2 Action

In Struts2 Action class can be created by different ways - by extending ActionSupport class, by implementing Action interface and POJO class.


Model Driven action in Struts2

Action class itself treats as model class and request parameters are bound directly to fields in the actions class.


Dependency Injection In Struts2

The Inversion of Control (IoC) and Dependency Injection (DI) are programming design pattern, which is used to reduce compiling in computer programs.


ApplicationAware Interface

It allows to set Map object with Application object. This Map object contains different other objects which can be accessible from entire application.


CookieAware Interface Demo

To implement traditional J2EE Cookie in Struts 2 application, you need to implement CookieAware interface and override setCookiesMap() method.


ServletContext Interface

It allows to set attributes in Application scope.The ServletContext is used to store information and it can be accessible from any web components.


RequestAware Interface

To manage attributes of J2EE HttpServletRequest object in Struts 2 framework, you need to implement RequestAware interface and override public void setRequest(Map reqmap) { } method.


ServletRequestAware Interface

To obtain J2EE HttpServletRequest object in Struts 2 framework, you need to implement ServletRequestAware interface and override public void setServletRequest(HttpServletRequest req) { } method.


ServletResponseAware Interface

To obtain J2EE HttpServletResponse object in Struts 2 framework, you need to implement ServletResponseAware interface and override public void setServletResponse(HttpServletResponse response) { } method.


SessionAware Interface

To obtain J2EE HttpSession object implement SessionAware interface and override public void setSession(Map map) { } method.


Interceptors in Struts2

Interceptors is used to controlling request and response. It executed before and after the Action. So It can be used to process some logics on the request before Action and after Action also.


alias Interceptor

Normally, the request parameter's name must same as action's properties name or HTML control name must be same as properties in Action class. But in some cases, you can define different UI control name than properties in Action or POJO class.


Chaining Interceptor

It is used to copies all the object from current value stack to the next value stack in action class. It will not copy the objects which implements Unchainable interface.


Cookie Interceptor

It is used to manage cookies in the stack/action. It is not part of defaultStack interceptor in struts-default.xml. I have to configure manually in struts.xml file.


Debugging Interceptor


Execute and wait Interceptor


Fileupload Interceptor


Logger Interceptor


Params Interceptor


Prepare Interceptor


User defined interceptor


Tags in Struts2

The Struts2 framework provides powerful tag library to create view pages. It can be used to create view pages in very easy.


Control Tag - if-elseif-else

It is used to control the execution behavior of data in the page. Find below details of each control tags.


Control Tag - append

It is used to appends iteratable two or more lists together to create appended list. Sometimes we need to append more collection objects in single object.


Control Tag - generator

It is used to generate an iterator based on val attribute supplied to this tag. The genarated iterator is pushed to the top of the stack and popped at the end of the tag.


Control Tag - iterator

It is ued to iterate over the list or given values. It allows traversal over any collections object like, Collection, Map, Enumeration and array.


Control Tag - merge

It is used to create a new iteratable list by merging two or more lists. It is used with iterator tag and different than append tag.


Control Tag - sort

It is used to sorts a List using a Comparator both passed in as the tag attribute.


Control Tag - subset

A tag that takes an iterator and outputs a subset of it. It delegates to SubsetIteratorFilter internally to perform the subset functionality.


data tag - a

The Data tags are used for manipulating data in page. These tags are used to fetch data from valuestack or store value/object in the value stack.


data tag - action

This tag enables developers to call actions directly from a JSP page by specifying the action name. The body content of the tag is used to render the results from the action.


data tag - bean

It is used to create instance of a POJO class which conforms to the JavaBeans specification. It allows to create an instance of simple JavBean and push it into the valuestack.


data tag - date

It is used to format Date in differently. You can specify a custom format or you can use a predefined format as key '' from properties file.


data tag - i18n

It is used to access the key-value information from resource bundle [.properties file ]. It has two attribute - and name.


data tag - include

It is used to include the Servlet or JSP pages. It has two attributes id and name.


data tag - param

It is used to pass parameters to other tags. The include tag and bean tag are examples of param tag.


data tag - property

It is used to get the value from valueStack. If the value is not specofied it retunrs the object from the top of the ValueStack.


data tag - push

It is used to list the value in the ValueStack. It also allows to push an object reference onto the ValueStack.


data tag - set

The set tag assigns a value to a variable in a specified scope.


data tag - text

It is used for accessing a text message from the resource bundle. If the named message is not found in the .properties file, then the body of the tag will be used as the default message.


data Tag - url

This tag is used to create a URL.


form tag - form

It is used to create a form to accept data from user. All the input tag must into this tag. It is frequently used in the web application.


form tag - textfield

It is used to create HTML form input control. It has three important attributes - name (name of the control), label (display label before input control) and size (size of control).


form tag - password

It is used to accept password from user in HTMl form


form tag - textarea

It is used to create input text for multiple lines.


form tag - hidden

It is used to create hidden field in HTML form. It does not show to the user.


form tag - checkbox

It is used to create chebox in form. It takes String or boolena as value. It can be populated from ValueStack.


form tag - checkboxlist

It is used to create same as radio button in the form of checkbox. User can select none,one or many.


form tag - combobox

It is used to create HTML control to select an option from list or user can add new value other than list also.


form tag - radio

It is used to create HTML radio button. It allows to selexct only one from the list of options provided through radio button.


form tag - select

It is used to create a list of options to select from. It is same as combox, but user can not enter new value. User has to select from list only.


form tag - doubleselect

The two HTML select element is created and options in second select element will be populated dynamically based on selection item from first select element.


form tag - optgroup

It is used to create HTML control to select option from a list and provides grouping options within a category.


form tag - updownselect


form tag - optiontransferselect

Create a option transfer select component which is basically two tag with buttons in the middle of them allowing options in each of the

Non UI tag - actionerror

It is used to display errors of Action class. The Action error can be set using addActionError() method.


Non UI tag - fielderror

It is same as actionerror tag but it display error messages against each field in the form.


Non UI tag - actionmessage

It is used to display message associated with an action class. It can be set using addActionMessage(message) method.


Results Type in Struts2

After executing the Action class , the target view page will be executed based on configuration in struts.xml.


Struts2 Dispatcher result

It is used to include or forward to a target view page(JSP). It is default result type in Struts2 framework.


Struts2 Chain result

It is used to achive action chaining. In the action chaining the multiple actions can be executed in defined sequence.


Struts2 Freemarker result

It is used to integrate the Freemarker templates in the view page. The Freemarker engine renders the freemarker view page and extension will be .ftl of view page.


Struts2 Redirect result

It is used to redirect the browser request to the new resource. It works based on HttpServletResponse.sendRedirect() method.


Struts2 RedirectAction result

It is same as redirect result type. But tne target result must be an action either in same application or in the other application.


Struts2 Stream result

It is used to Streaming the InputStream back to the client and client can download the content in the specified formmat.


Struts2 Velocity result

It is usedn to integration of Velocity template in Struts2 application. Using the Servlet container's JspFactory, this result mocks a JSP execution environment and then displays a Velocity template that will be streamed directly to the servlet output.


Struts2 XSL result

It is used to integration of XML/XSLT in view page. It is used to display the ValueStack data in the XSLT format.


Struts2 PlainText result

It is used to display raw content of a file in the browser.


Handling Exception in Struts 2

The exception handling is the common task in any Java based application.


Dynamic Method Selection

Struts 2 has feature to execute method from action dynamically. Suppose you have five methods (execute, add, delete, update and query) in action class and want to configure in struts.xml file.


Populate data from action

You can populate data in the view page from action.


Data validations

The Struts 2 framework provides excellent validation feature. It can be configured vie XML of annotations.


Data validation through XML

The validation through XML based, the xml file must be -validation.xml file and root tag is validators.


Data validation through Annotations

You can validate data through annotations also.


Internationalization in Struts2

The Struts2 provides feature for web application can created for global use. The web content will be changed according to the language selection by the user at run time.


Tiles in Struts2

The Apache Tiles is templating framework is used to design several pages in easiest way. It provides features to divide entire page in different sections including title and page can be inserted in the sections at run time.


Annotation in Struts 2

The Struts2 framework scans Action class in classpath using Convention Plug-in. You can use annotations in action class for mapping rather than configuration file struts.xml.


@Actions and @Action

The Convention plugin allows action mapping using Action annotation in class. The annotation can also be used inside @Actions annotations for multiple URLs.


@Results and @Result

The Convention plugin allows action classes to define many results for an action.


@ExceptionMappings and @ExceptionMapping

This annotation can be used to define exception mappings to actions. These mappings can be applied to the class level and action level also.


@InterceptorRefs and @InterceptorRef

Interceptors can be specified at the method level, using the @Action annotation or at the class level using the @InterceptorRefs annotation.


Hibernate integration in Struts2

The Hibernate is most popular ORM (Object Relational Mapping) framework in the market. You can integrate it in all Java/J2EE based framework.


Spring integration

Spring is an Open Source lightweight framework works with all tier software application layers. It provides container to manage bean and uses Dependency Injection(DI).


Hibernate-plug-in in Struts 2

You can use hibernate plug-in for the same. You can inject Session and Transaction in DAO class and it can be configured using annotations, xml and both.

Share your valuable comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code :