Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

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

×

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

Introduction to Struts2 Framework

Struts 2 is an Model View Controller (MVC) based framework written in Java. In this article we will see how to work with it and some examples.

Introduction:

Struts can be defined as a web application development framework that is based on the requests and is used in order to develop the web applications of java enterprise edition (Java EE). The struts framework is a Model View Controller (MVC) based framework and by the use of MVC the logic, view and user input related tasks gets separated and the work is done in a clear manner that can be verified easily. The web framework of struts is known as the request based web application framework. The struts 2 is an efficient and extensible action framework used in order to create the enterprise level web applications in java and this latest version is much simple as compared with the previous one.

Description of Struts

MVC solves the problems in web applications and the problem is divided into three categories which include model, view and controller. The Model in MVC is responsible to maintain the business logic and data related work and the model also knows nothing about view and controller. The View in MVC is responsible in order to display the related work and presents the model by accessing the getters of models successfully. View does not know about controller. When the change in model occurs the view must be notified. The Controller in MVC basically handles the input related work and controls the interactions between model and view respectively.

The controller also creates and sets the model. In the one architecture of model the java server page is responsible to process the incoming request and send the response to client respectively. In the other architecture of model the servlets and java server page both are used and this approach is used in order to serve the dynamic content respectively. The MVC pattern in latest version of struts consists of the five components including action or model, interceptors, value stack and Open-Graph Navigation Language (OGNL), results and result types, the view technologies respectively. This latest version of struts is different from the MVC framework and in this struts 2, the actions does the work in the form of model instead of the controller as discussed above about the controller in the traditional MVC pattern respectively.

The features of latest struts framework includes Plain Old Java Object (POJO) forms and POJO actions, the tag support, asynchronous JavaScript and XML (AJAX) support, integration with different frameworks like Hibernate, Spring and others, template support, plug-in support, profiling, easy modification of tags and the benefits of less configurations. The dispatch servlet filter and interceptors are used in order to implement the controller. The implementation work of model is done using actions. The view gets implemented by including both the results and result types. The value stack and OGNL enables the integration with other related components and also provides the linking and threads which are common respectively.

Overview:

The struts framework is based on the Java Server Pages (JSP) and java servlets technologies. The struts framework recommends the continuous usage of MVC design pattern throughout the application. The life cycle based on the request of user works in this manner; first the user sends request to server in order to get the desired webpage, the dispatch servlet filter first checks the request and decides the required and suitable action, the functionalities of interceptors after proper configurations are applied on the request depending on its type, the most appropriate action gets executed in order to give response to the received request and after the proper and efficient processing the results gets prepared by the view and finally the result is sent as a response to the user respectively. The tags available in Struts 2 are of different types and these tags include control tags, data tags, form tags and AJAX tags.

The tag in struts 2 is represented using the <s:form action=””> and </s:form> in this format respectively. The control tags in Struts 2 help to control the execution of webpage. Some of the control tags include if, else, else-if, merge, append, iterator tag and many others. The data tags in Struts 2 help the data and display of data related work on a page. Some of the data tags include action, include, date, param, property, set, uri, text and many others. The form tags in Struts 2 are used to provide the user interface and these types of tags help in making the user interface appearance in a proper and clear manner.

Some of the form tags include div, text, form, text-field, password, text-area, label, file, radio, select, combo-box and many others. Similarly, there are the Ajax tags also available in struts 2 like the data, control and form tags. The calls are changes from s to sx like and in this way the other tags can be represented in this manner which shows the work done in Ajax respectively. The annotations are also available in this framework and if this procedure is selected then there is no need to manually enter the configuration details inside the configuration file that is in xml.

Importance:

The importance of MVC software design pattern is to separate the work of each part in an application and to make the code reusable respectively. With the help of this framework, the developers are able to develop the web applications in an easy manner. The developer is responsible to write the code of model and prepare the related working logic and also need to handle the configuration details which are present in an xml document file which combines together the MVC respectively. The main goal of struts is to separate the model related work which contains the logic of application that deals with database system and related work inside the view on which the web-pages in the form of HTML are shown and presented to the user or client and the controller that is an instance which is responsible to pass the information between model and view respectively.

Screenshots:

Following are the screens from the Struts2_Work working application.

Project files, package details and related libraries for struts2 application

Figure 1: Project files, package details and related libraries for struts2 application

This screen shows the related files in the web application using struts2 framework and at the right side of the screen the JSP page whose result in the browser is shown in the next output.

Output of per.jsp in Web Browser

Figure 2: Output of per.jsp in Web Browser

This screen shows the output of the JSP page named as per and the related work done inside it is shown. The user can enter the values and these will be displayed in the table present at the bottom of this webpage.

Entered values by the user and ready to click on the button to see the added result

Figure 3: Entered values by the user and ready to click on the button to see the added result

In this screen, the input in the text-fields entered by the user is shown. After entering the text and related information, the user clicks on the button named as Add Person and the values appear in the present table of the webpage.

The entered values displayed successfully after the button is clicked

Figure 4: The entered values displayed successfully after the button is clicked

This screen shows the entered values and the result that appeared in the table of page respectively.

Implementation & Codes:

Following are the related codes from the Struts2_Work web application in which the struts2 framework has been used and the related concepts are applied in the application.

Package Name: com.person.model

Listing 1: Person.java

package com.person.model;
import java.io.Serializable;
public class Person implements Serializable{
    private String name;
    private String address;
    private String profession;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getProfession() {
        return profession;
    }
    public void setProfession(String profession) {
        this.profession = profession;
    } 
}

This is the class in which the attributes of person are entered and the functions of getters and setters have been implemented to set and get the values respectively.

Package Name: com.person.action

Listing 2: PersonAction.java

package com.person.action;
import com.opensymphony.xwork2.ActionSupport;
import com.person.model.Person;
public class PersonAction extends ActionSupport{
    private String the_message;
    private Person per;
    public Person getPer() {
        return per;
    }
    public void setPer(Person per) {
        this.per = per;
    }
    
    public String addPerson(){
        return "recordadded";
    }
}

This is an action class in which the value of attribute whose type is the class of Person and the function is present to complete the related work and similarly the action is performed of adding the record inside the table.

Webpage:

Listing 3: per.jsp

:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
<p style="background-color: transparent">PERSON DETAILS</p>
        <h1>Information of Person</h1>
        <s:form action="add" method="post">
            <s:textfield name="per.name" label="Person Name: "/>
            <s:textfield name="per.address" label="Address: "/>
            <s:textfield name="per.profession" label="Profession: "/>
            <s:submit value="Add Person" align="center"/>
        </s:form> 
        <br>
                <h1 style="background-color: orange"> The entered details </h1>
        <table border="2">
            <tr>
                <th>Person Name</th>
                <th>Address</th>
                <th>Profession</th>
            </tr>
            <tr>
                <td><s:property value="per.name"/></td>>
                <td><s:property value="per.address"/></td>>
                <td><s:property value="per.profession"/></td>>                
            </tr>
    </body>
</html>

This is the webpage of JSP through which the work is done and the model and action work is performed accordingly.

Configuration Files:

Following are the configuration files in which some change has been performed to get the application in working condition.

Listing 4: struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <include file="example.xml"/>
    <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default">
        <action name="add" class="com.person.action.PersonAction" method="addPerson">
            <result name="recordadded">/per.jsp</result>
        </action>
    </package>
</struts>

This file shows the related work of struts configuration so that call can be made to the required JSP so that the action and view work gets done in a proper manner.

Listing 5: web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/per.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Conclusion

This is the main configuration file of this web application and it contains all the related configurations including the related information of session and welcome page respectively.

This is all for this article. Hope you liked it.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[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