0% found this document useful (0 votes)
14 views42 pages

Ajp 5

The document provides an overview of the Struts and Spring frameworks, highlighting their roles in developing Java web applications using MVC architecture. Struts facilitates the creation of maintainable web applications with features like input validation and internationalization, while Spring offers a lightweight, modular framework for enterprise applications, supporting various modules such as IoC and AOP. Additionally, it covers aspects like the architecture of Struts, configuration files, action classes, and transaction management in Spring.

Uploaded by

Tallapaka Sagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views42 pages

Ajp 5

The document provides an overview of the Struts and Spring frameworks, highlighting their roles in developing Java web applications using MVC architecture. Struts facilitates the creation of maintainable web applications with features like input validation and internationalization, while Spring offers a lightweight, modular framework for enterprise applications, supporting various modules such as IoC and AOP. Additionally, it covers aspects like the architecture of Struts, configuration files, action classes, and transaction management in Spring.

Uploaded by

Tallapaka Sagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

MODULE - 5

Struts and Spring Frame Work


Struts Framework
Struts is an open source framework that extends the Java Servlet API and employs a
Model, View, Controller (MVC) architecture. It enables you to create maintainable,
extensible, and flexible web applications based on standard technologies, such as JSP
pages, JavaBeans, resource bundles, and XML.

A framework is therefore a set of tools and modules that can be reused for
various projects. One of the most well-known and used frameworks is the
Microsoft . NET Framework for websites.

The Spring Framework (Spring) is an open-source application framework that


provides infrastructure support for developing Java applications. One of the
most popular Java Enterprise Edition (Java EE) frameworks, Spring helps
developers create high performing applications using plain old Java objects
(POJOs).
Basics of Struts
Struts is used to create a web applications based on servlet and JSP. Struts
depend on the MVC (Model View Controller) framework. Struts application is a
genuine web application. Struts are thoroughly useful in building J2EE (Java 2
Platform, Enterprise Edition) applications because struts takes advantage of J2EE
design patterns. Struts follows these J2EE design patterns including MVC.

1.Struts encourages good design practices and modeling because the framework
is designed with “time-proven” design patterns.

2.Struts is almost simple, so easy to learn and use.

3.It supports many convenient features such as input validation and


internationalization.

4.It takes much of the complexity out as instead of building your own MVC
framework, you can use struts.

5.Struts is very well integrated with J2EE.

6.Struts has large user community.


7.It is flexible and extensible, it is easy for the existing web applications to adapt
the struts framework.
Architecture of struts

The architecture and flow of struts 2 application, is combined with many


components such as Controller, ActionProxy, ActionMapper, Configuration Manager,
ActionInvocation, Inerceptor, Action, Result etc.

Here, we are going to understand the struts flow by 2 ways:

1.struts 2 basic flow

2.struts 2 standard architecture and flow provided by apache struts

1.Struts 2 basic flow

Let's try to understand the basic flow of struts 2 application by this simple figure:
Struts 2 basic flow
Struts 2 standard flow (Struts 2 architecture)
Request Handling Life Cycle
Building a simple struts
We would have to create following four components for any Struts 2 project −

Sr.No Components & Description

1 Action
Create an action class which will contain complete business logic and control the interaction
between the user, the model, and the view.

2 Interceptors
Create interceptors if required, or use existing interceptors. This is part of Controller.

3 View
Create a JSPs to interact with the user to take input and to present the final messages.

4 Configuration Files
Create configuration files to couple the Action, View and Controllers. These files are struts.xml,
web.xml, struts.properties.
Struts 2 Configuration File

The struts application contains two main configuration files struts.xml file
and struts.properties file.

The struts.properties file is used to override the default values of default.xml file
provided by struts framework. So it is not mandatory. Mostly, you will not use
struts.properties file. We will learn about it later.
Here, we are going to learn all about struts.xml file. First of all let us see the
simple example of struts.xml file
Struts 2 Action

In struts 2, action class is POJO (Plain Old Java Object).


POJO means you are not forced to implement any interface or extend any
class.

Generally, execute method should be specified that represents the business


logic. The simple action class may look like:

Welcome.java
package com.javatpoint;
public class Welcome {
public String execute(){
return "success";
}
}
Struts 2 Interceptors Tutorial

Interceptor is an object that is invoked at the preprocessing and postprocessing


of a request. In Struts 2, interceptor is used to perform operations such as
validation, exception handling, internationalization, displaying intermediate result
etc.

Advantage of interceptors
Pluggable If we need to remove any concern such as validation, exception
handling, logging etc. from the application, we don't need to redeploy the
application. We only need to remove the entry from the struts.xml file.

Struts 2 default interceptors


There are many interceptors provided by struts 2 framework. We have option to
create our own interceptors. The struts 2 default interceptors are as follows:

1) alias It converts similar parameters that have different names between


requests.
3) chain If it is used with chain result type, it makes the properties of previous
action available in the current action.
4) checkbox It is used to handle the check boxes in the form. By this, we can
detect the unchecked checkboxes.
Results
the <results> tag plays the role of a view in the Struts2 MVC framework.
The action is responsible for executing the business logic. The next step after
executing the business logic is to display the view using the <results> tag.
Often there is some navigation rules attached with the results.

For example, if the action method is to authenticate a user, there are three
possible outcomes.

Successful Login
Unsuccessful Login - Incorrect username or password
Account Locked

In this scenario, the action method will be configured with three possible
outcome strings and three different views to render the outcome. We have
already seen this in the previous examples.
Struts Tags

A tag library provides a number of predefined actions that behind functionalities to


a specific JSP page. JSTL provides tag libraries that include a wide range of
actions to perform common tasks. For example, if you want to access data from
database, you can use SQL tag library in your applications.
The Struts 2 tags are primarily used to manipulate the data displayed on a page.

Struts 2 - Data Tags


The Action Tag. This tag enables developers to call actions directly from a JSP
page by specifying the action name and an optional namespace.
...
1.The Include Tag. ...

2.The Bean Tag.


...
3.The Date Tag. ...

4.The Param Tag. ..


.
5.The Property Tag. ...
6.The Push Tag. ...
7.The Set Tag.
Struts2 XML Based Validations

To avoid the wrong values, we need to perform validation on forms where user
submits some values. For example, if user writes his/her email id as abc, we need
to give error message to the user that the given email id is not correct. So that we
can have only valuable informations.

There are three ways to perform validation in struts 2.

1) By Custom Validation Here, we must implement the Validateable interface (or


extend ActionSupport class) and provide the implementation of validate method.

2) By Input Validation (built-in validators) Struts 2 provides a lot of predefined


that can be used in struts 2 application to perform validation.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">

<display-name>struts_validation</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.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

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

MVC stands for Model-View-Controller, which divides an application into three


primary logical parts: the model, the view, and the controller. These parts are
created to deal with a particular application development task. Struts 2 is a
framework for development. Struts 2 frameworks can be used to create
applications. We need to access the database to store and retrieve stored data
in applications. Struts offer strong support for JPA/Hibernate integration despite
being an MVC framework rather than a database framework. Struts applications
can connect to the database via JDBC to store and retrieve data.

This article will cover the steps required for struts 2 database access.

We will understand the Struts 2 database access using an example. In this


example, we'll use the Struts Framework to build a user login form and store the
data in a MySQL database. Depending on your requirements, you can also use
different databases like Oracle, DB2, etc.
Let's learn struts 2 database access with the help of an example.
Create Main Page
Next, we will create the index.jsp JSP file to collect the email id and password.
The database will be searched to see if this email id and password are valid.
Overview of Spring

Spring is the most popular application development framework for enterprise


Java. Millions of developers around the world use Spring Framework to create
high performing, easily testable, and reusable code. Spring framework is an
open source Java platform.

It was developed by Rod Johnson in 2003. Spring framework makes the easy
development of JavaEE application.
It is helpful for beginners and experienced persons.

Spring Framework
Spring is a lightweight framework. It can be thought of as a framework of
frameworks because it provides support to various frameworks such
as Struts, Hibernate, Tapestry, EJB, JSF, etc. The framework, in broader sense,
can be defined as a structure where we find solution of the various technical
problems.

The Spring framework comprises several modules such as IOC, AOP, DAO,
Context, ORM, WEB MVC etc. We will learn these modules in next page. Let's
understand the IOC and Dependency Injection first.
Spring Architecture

Spring could potentially be a one-stop shop for all your enterprise applications.
However, Spring is modular, allowing you to pick and choose which modules are
applicable to you, without having to bring in the rest. The following section
provides details about all the modules available in Spring Framework.

The Spring Framework provides about 20 modules which can be used based on
an application requirement.
Core Container
The Core Container consists of the Core, Beans, Context, and Expression
Language modules the details of which are as follows −

The Core module provides the fundamental parts of the framework, including the
IoC and Dependency Injection features.

The Bean module provides BeanFactory, which is a sophisticated implementation


of the factory pattern.

The Context module builds on the solid base provided by the Core and Beans
modules and it is a medium to access any objects defined and configured. The
ApplicationContext interface is the focal point of the Context module.

The SpEL module provides a powerful expression language for querying and
manipulating an object graph at runtime.
bean life cycle

The lifecycle of any object means when & how it is born, how it behaves
throughout its life, and when & how it dies. Similarly, the bean life cycle refers to
when & how the bean is instantiated, what action it performs until it lives, and
when & how it is destroyed. In this article, we will discuss the life cycle of the
bean.
Bean life cycle is managed by the spring container. When we run the program
then, first of all, the spring container gets started. After that, the container creates
the instance of a bean as per the request, and then dependencies are injected.
And finally, the bean is destroyed when the spring container is closed. Therefore, if
we want to execute some code on the bean instantiation and just after closing the
spring container, then we can write that code inside the custom init() method and
the destroy() method.
Note: We can choose custom method name instead of init() and destroy(). Here,
we will use init() method to execute all its code as the spring container starts up
and the bean is instantiated, and destroy() method to execute all its code on
closing the container.
XML Configuration on Spring

By XML: In this approach, in order to avail custom init() and destroy() method for
a bean we have to register these two methods inside Spring XML configuration
file while defining a bean. Therefore, the following steps are followed:
Firstly, we need to create a bean HelloWorld.java in this case and write the init()
and destroy() methods in the class.
// Java program to create a bean
// in the spring framework
package beans;

public class HelloWorld {

// This method executes


// automatically as the bean
// is instantiated
public void init() throws Exception
{
System.out.println(
"Bean HelloWorld has been "
+ "instantiated and I'm "
+ "the init() method");
}

// This method executes


// when the spring container
// is closed
public void destroy() throws Exception
{
System.out.println(
"Container has been closed "
+ "and I'm the destroy() method");
Aspect – oriented Spring

Aspect Oriented Programming (AOP) compliments OOPs in the sense that it


also provides modularity. But the key unit of modularity is aspect than class.
AOP breaks the program logic into distinct parts (called concerns). It is used to
increase modularity by cross-cutting concerns.

A cross-cutting concern is a concern that can affect the whole application and
should be centralized in one location in code as possible, such as transaction
management, authentication, logging, security etc.

Why use AOP?


It provides the pluggable way to dynamically add the additional concern before,
after or around the actual logic. Suppose there are 10 methods in a class as given
below:
class A{
public void m1(){...}
public void m2(){...}
public void m3(){...}
public void m4(){...}
public void m5(){...}
public void n1(){...}
public void n2(){...}
public void p1(){...}
public void p2(){...}
public void p3(){...}
}

There are 5 methods that starts from m, 2 methods that starts from n and 3
methods that starts from p.

Understanding Scenario I have to maintain log and send notification after calling
methods that starts from m.
Problem without AOP We can call methods (that maintains log and sends
notification) from the methods starting with m. In such scenario, we need to
write the code in all the 5 methods.
But, if client says in future, I don't have to send notification, you need to change
all the methods. It leads to the maintenance problem.

Solution with AOP We don't have to call methods from the method. Now we
can define the additional concern like maintaining log, sending notification etc. in
the method of a class. Its entry is given in the xml file.
In future, if client says to remove the notifier functionality, we need to change
only in the xml file. So, maintenance is easy in AOP.
Managing Database
Managing Transaction.
Spring framework provides an abstract layer on top of different underlying
transaction management APIs. Spring's transaction support aims to provide an
alternative to EJB transactions by adding transaction capabilities to POJOs.
Spring supports both programmatic and declarative transaction management.
EJBs require an application server, but Spring transaction management can be
implemented without the need of an application server.
Programmatic vs. Declarative
Spring supports two types of transaction management −
Programmatic transaction management − This means that you have to manage the
transaction with the help of programming. That gives you extreme flexibility, but it is
difficult to maintain.
Declarative transaction management − This means you separate transaction
management from the business code. You only use annotations or XML-based
configuration to manage the transactions.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy