Quick Reference Study Notes for Apache Struts 2.x (Advanced)


Apache Struts 2

Struts 2 is framework used to create Web Application in JAVA. Its original name was Web work 2. This framework follow all the concept of MVC pattern, and use JSP and JAVA Servlet in it.

Features:

Struts 2 uses POJO Forms and POJO Actions for Action forms. POJO stands for Plain Old JAVA Object. POJO is a simple JAVA object which is not bounded by any restriction.

Strut 2 support Multiple view options technology to write the code for user view like JSP, XSLT etc.

Strut 2 support a wide range of plugins that can be use to enhance the functionality of this framework.

There are many debugging methods are available in Strut 2. That are already integrated in it.

Strut 2 also provide support for AJAX to update the data on view part without loading the complete page. Also its very simple to use AJAX in Strut 2.

We can modify the view of any tag using simple HTML, XML, or CSS. Strut 2 provide support for them.

Strut 2 MVC flow chart

 

 

Components of Strut 2 Project

Action: Controllers in the Strut 2 are known as Action. When the user send a request from the browser, the request is handled by Action and the Action interact with the model and send the results to a particular View depend upon the request.

Interceptors: Strut2 provide the functionality for interceptors also. An Interceptor is a function which is executed before or after an action is called.

Here is the list of interceptor functions that are available in Strut2:

  • Alias: This function can be used to give alias name to a request variable.

  • Checkbox: When a form is submitted to the action page then all of the checkbox element does not appears in the request variable. In such case the Checkbox interceptor is used to set false value of checkbox.

  • conversionError: if some error occurs while changing the string to a parameter type then this interceptor will add the error information to the error field of action.

  • createSession: it will create a session on its own if there is no session available.

  • Debugging: it adds the functionality to debug code.

  • execAndWait: it will show a common waiting page during the time when the action is working in background.

  • Exception: it provide the functionality of automatic exception handling by redirecting the flow using mapping.

  • And many more interceptors……..

 

Configuring a Struts2 Application:

 

There are 4 configuration files in Struts2, that we need to take care of.

Web.xml: This is a Java Enterprise edition file. The configuration of this decide, in which way the HTTP request will be handled. This file is very important for the successfully running of Struts 2 project.

 

Struts.xml: This is a configuration file for all the actions and it contains many information about each action. As we create new action we need to keep updated this file for all new actions.

Here is the list of attributes of this file.

  • Name

  • Extends

  • Abstract

  • namespace

Struts-config.xml: This file is a common point between the Models and View Components of the project.
Here is the list tags for this file:

  • Struts-config

  • Form-beans

  • Global forwards

  • Action-mappings

  • Controller

  • plug-in

 

Struts.properties: This file is used to save the constant variables that are used in project like database credentials, some login details of third party plug in etc. We can also save these variables in init-param tag in web.xml configuration file or in struts.xml configuration file.

Directory Structure of struts 2 project:

Project folder(Struts)

↳META-INF

    â†³MANIFEST.MF

↳WEB-INF

|     â†³Classes

|     |       â†³Struts.properties

|     â†³lib

|           â†³struts.jar

|    â†³struts-config.xml

|     â†³web.xml

|     â†³other-libraries (*.tld)

|     â†³….

↳index.jsp

 

Purpose of files in META-INF folder: The only file in this folder contains the meta information about the project. This information will be used by some third party libraries to identify the project.

Purpose of files in WEB-INF/classes folder: In this folder we place our own classes like Actions.

Purpose of WEB-INF/classes/Struts.properties: This file contains all the constants of the project like: database credentials, other messages etc.

Purpose of WEB-INF/*.tld: These are to struts tag library files.

WEB-INF/struts-config.xml: This is the main configuration file of struts

WEB-INF/web.xml: This another configuration file.

Index.jsp: these jsp file are the main view file of the project.

*NOTE : "This study material is collected from multiple sources to make a quick refresh course available to students."

This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Cookie Policy. More info. I Agree