Ajp 5
Ajp 5
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.
1.Struts encourages good design practices and modeling because the framework
is designed with “time-proven” design patterns.
4.It takes much of the complexity out as instead of building your own MVC
framework, you can use struts.
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 −
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
Welcome.java
package com.javatpoint;
public class Welcome {
public String execute(){
return "success";
}
}
Struts 2 Interceptors Tutorial
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.
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
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.
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
This article will cover the steps required for struts 2 database access.
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 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;
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.
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.