Showing posts with label Struts. Show all posts
Showing posts with label Struts. Show all posts

Tuesday, January 10, 2012

STRUTS 2 Interceptors

Interceptors in STRUTS 2

Many of the features provided in the Struts2 framework are implemented using interceptors; examples include exception handling, file uploading, lifecycle callbacks and validation. Interceptors are conceptually the same as servlet filters or the JDKs Proxy class. They provide a way to supply pre-processing and post-processing around the action. Similar to servlet filters,interceptors can be layered and ordered. They have access to the action being executed, as well as all environmental variables and execution 
properties. (Reference: Ian Roughley - Starting Struts 2 Book)

Below are the implementing interceptors mentioned:
  1. Spring Framework  the ActionAutowiringInterceptor interceptor.
  2. Request String and Form Values  the ParametersInterceptor interceptor.
  3. Servlet-based objects  the ServletConfigInterceptor interceptor.
The first two interceptors work independently, with no requirements from the action, but the last interceptor ServletConfigInterceptor is different.It works with the assistance of the following interfaces:
  • SessionAware to provide access to all the session attributes via a Map
  • ServletRequestAware to provide access to the HttpServletRequest object
  • RequestAware to provide access to all the request attributes via a Map
  • ApplicationAware to provide access to all the application attributes via a Map
  • ServletResponseAware to provide access to the HttpServletResponse object
  • ParameterAware to provide access to all the request string and form values attributes via a Map
  • PrincipalAware to provide access to the PrincipleProxy object; this object implements the principle and role methods of the HttpServletRequest object in implementation, but by providing a proxy,allows for implementation independence in the action
  • ServletContextAware to provide access to the ServletContext object

Read more...

Wednesday, March 3, 2010

Struts ActionForm & DynaActionForm

Difference between ActionForm and DynaActionForm


An ActionForm represents an HTML form that the user interacts with over one or more pages. You will provide properties to hold the state of the form with getters and setters to access them. Whereas, using DynaActionForm there is no need of providing properties to hold the state. Instead these properties and their type are declared in the struts-config.xml

The DynaActionForm bloats up the Struts config file with the xml based definition. This gets annoying as the Struts Config file grow larger.

The DynaActionForm is not strongly typed as the ActionForm. This means there is no compile time checking for the form fields. Detecting them at runtime is painful and makes you go through redeployment.

ActionForm can be cleanly organized in packages as against the flat organization in the Struts Config file.

ActionForm were designed to act as a Firewall between HTTP and the Action classes, i.e. isolate and encapsulate the HTTP request parameters from direct use in Actions. With DynaActionForm, the property access is no different than using request.getParameter( .. ).

DynaActionForm construction at runtime requires a lot of Java Reflection (Introspection) machinery that can be avoided.

Read more...

Sunday, July 26, 2009

Struts 2

Programming the abstract classes instead of interfaces is one of design problem of struts1 framework that has been resolved in the struts 2 framework

Most of the Struts 2 classes are based on interfaces and most of its core interfaces are HTTP independent

Unlike ActionForwards, Struts 2 Results provide flexibility to create multiple type of
outputs

In Struts 2 any java class with execute() method can be used as an Action class

ActionForms feature is no more known to the Struts 2 framework. Simple JavaBean flavored actions are used to put properties directly

Struts 2 Actions are HTTP independent and framework neutral. This enables to test struts applications very easily without resorting to mock objects

Java 5 annotations can be used as an alternative to XML and Java properties configuration

Struts 2 lets to customize the request handling per action, if desired. Struts 1 lets to customize the request processor per module

Struts 2 Actions are Spring-aware.Just need to add Spring beans

AJAX client side validation

Read more...

Tuesday, July 7, 2009

Struts Framework Tag Libraries

The Struts framework includes custom JSP tag libraries that you can use to create JSP pages that work with the rest of the Struts framework objects in your web application

Struts HTML Tags

Used to create Struts input forms, as well as other tags generally useful in the creation of HTML-based user interfaces.

Struts Bean Tags

Useful in accessing beans and their properties, as well as defining new beans (based on these accesses) that are accessible to the remainder of the page via scripting variables and page scope attributes. Convenient mechanisms to create new beans based on the value of request cookies, headers, and parameters are also provided.

Struts Logic tag

Useful in managing conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management.

Struts Nested

Brings a nested context to the functionality of the Struts custom tag library. The purpose of this tag library is to enable the tags to be aware of the tags which surround them so they can correctly provide the nesting property reference to the Struts system.

Struts Tiles Tags

Provides tiles tags. Tiles were previously called Components.

Struts Templates

Three tags: put, get, and insert. A put tag moves content into request scope, which is retrieved by a get tag in a different JSP page. That template is included with the insert tag. Tags from the various Struts custom JSP tag libraries appear in JDeveloper on the Component Palette.

Read more...

Sunday, July 5, 2009

Struts and JSP

Struts makes it possible for JSP pages to externalize flow control, rather than specify physical links to various JSP pages within the JSP file, the JSP file contains a Struts-defined logical URI. The Struts URI defines a logical page request mapped to actions that may return different physical JSP pages depending on the context of the HTTP request.

Struts simplifies the development of the actual JSP file content by limiting it to user interface generation only. Java that would otherwise appear inside the JSP files appears in separate servlet action classes that the JSP page invokes at runtime.

Struts helps to separate the development roles into user interface designer (HTML or tag library user) and JSP action-handler developer. For example, one person can write JSP page using only HTML or suitable tag libraries, while another person works independently to create the page action handling classes in Java.

Struts externalizes JSP actions that would otherwise appear inside all the JSP pages of your project into a single configuration file. This greatly simplifies debugging and promotes reuse. Struts consolidates String resources that would otherwise appear inside all the JSP pages of your project into a single file. This simplifies the task of localizing JSP applications.

Read more...

Tuesday, June 30, 2009

Struts Action Classes

An Action Class performs a role of an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request. Different kinds of actions in Struts are:

  • ForwardAction
  • IncludeAction
  • DispatchAction
  • LookupDispatchAction
  • SwitchAction
DispatchAction in Struts

The DispatchAction class is used to group related actions into one class. Using this class, We can have a method for each logical action compared than a single execute method. The DispatchAction dispatches to one of the logical actions represented by the methods. It picks a method to invoke based on an incoming request parameter. The value of the incoming parameter is the name of the method that the DispatchAction will invoke.

Struts ForwardAction

The ForwardAction class is useful when you’re trying to integrate Struts into an existing application that uses Servlets to perform business logic functions. You can use this class to take advantage of the Struts controller and its functionality, without having to rewrite the existing Servlets. Use ForwardAction to forward a request to another resource in your application, such as a Servlet that already does business logic processing or even another JSP page. By using this predefined action, you don’t have to write your own Action class. You just have to set up the struts-configfile properly to use ForwardAction.

IncludeAction in Struts

The IncludeAction class is useful when you want to integrate Struts into an application that uses Servlets. Use the IncludeAction class to include another resource in the response to the request being processed.

Struts LookupDispatchAction

The LookupDispatchAction is a subclass of DispatchAction. It does a reverse lookup on the resource bundle to get the key and then gets the method whose name is associated with the key into the Resource Bundle. LookupDispatchAction is useful if the method name in the Action is not driven by its name in the front end, but by the Locale independent key into the resource bundle. Since the key is always the same, the LookupDispatchAction shields your application from the side effects of I18N.

SwitchAction in Struts

The SwitchAction class provides a means to switch from a resource in one module to another resource in a different module. SwitchAction is useful only if you have multiple modules in your Struts application. The SwitchAction class can be used as is, without extending.

Read more...

Sunday, June 14, 2009

Struts Framework in Brief

Jakarta Struts is open source implementation of MVC 2 (Model-View-Controller) pattern for the development of web based applications. Jakarta Struts is robust architecture and can be used for the development of application of any size. Struts framework makes it much easier to design scalable, reliable Web applications with Java. Struts is a Web-based user interface framework also an open source framework. It is a matured and proven framework, which has been used in many J2EE projects.

Struts framework was created by Craig R. McClanahan and donated to the Apache Software Foundation in 2000. The Project now has several committers, and many developers are contributing to overall to the framework.

Struts is a open source framework which make building of the web applications easier based on the java Servlet and JavaServer pages technologies. The core of the Struts framework is a flexible control layer based on standard technologies like Java Servlets, JavaBeans, ResourceBundles, and XML, as well as various Jakarta Commons packages. Struts encourages application architectures based on the Model 2 approach (MVC 2), a variation of the classic Model-View-Controller (MVC) design paradigm.

Struts frames work is based on three components.

Model: A model represents an application’s data and contains the busuiness logic for accessing and manipulating that data. Any data that is part of the persistent state of the application should reside in the model objects. The business objects update the application state. ActionForm bean represents the Model state at a session or request level, and not at a persistent level. Model services are accessed by the controller for either querying or effecting a change in the model state. The model notifies the view when a state change occurs in the model. The JSP file reads information from the ActionForm bean using JSP tags. Components like business logic /business processes and data are the part of model.

The business logic updates the state of the model and helps control the flow of the application. With Struts this is done with an Action class as a thin wrapper to the actual business logic.

View: The view is responsible for rendering the state of the model. The presentation semantics are encapsulated within the view, therefore model data can be adapted for several different kinds of clients.The view modifies itself when a change in the model is communicated to the view. A view forwards user input to the controller.The view is simply a JSP file. There is no flow logic, no business logic, and no model information just tags. Tags are one of the things that make Struts unique compared to other frameworks like Velocity. HTML, JSP are the view components.

Controller: The controller is responsible for intercepting and translating user input into actions to be performed by the model. The controller is responsible for selecting the next view based on user input and the outcome of model operations.The Controller receives the request from the browser, and makes the decision where to send the request. With Struts, the Controller is a command design pattern implemented as a servlet. The struts-config.xml file configures the Controller. Action Servlet of Struts is part of Controller components which works as front controller to handle all the requests.

Struts provides its own Controller component and integrates with other technologies to provide the Model and the View. For the Model, Struts can interact with standard data access technologies, like JDBC and EJB, as well as most any third-party packages, like Hibernate, iBATIS, or Object Relational Bridge. For the View, Struts works well with JavaServer Pages, including JSTL and JSF, as well as Velocity Templates, XSLT, and other presentation systems.

Struts is a set of cooperating classes, servlets, and JSP tags that make up a reusable MVC 2 design. This definition implies that Struts is a framework, rather than a library, but Struts also contains an extensive tag library and utility classes that work independently of the framework.

Developing web application using struts frame work is fairly complex, but it eases things after it is setup. It encourages software development following the MVC design pattern. Many web applications are JSP-only or Servlets-only. With JSP and Servlets, Java code is embedded in the HTML code and the Java code calls println methods to generate the HTML code respectively. Both approaches have their advantages and drawbacks. Struts gathers their strengths to get the best of their association.

Read more...

Friday, June 12, 2009

Model View Controller (MVC) Design Pattern

Model-View-Controller (MVC) is a design pattern put together to help control change. MVC decouples interface from business logic and data. The MVC (Model-View-Controller) architecture the client request is first intercepted by a servlet referred as controller servlet, this servlet handles the initial processing of the request and determines which JSP page to display next. Here the controller servlet is the single point of entry, there is a clear sepration of business logic, presentation output and request processing.

MCV architecture is a way of decomposing an application into three parts:

Model : The model contains the core of the application's functionality. The model encapsulates the state of the application. Sometimes the only functionality it contains is state. It knows nothing about the view or controller.The model maintains the state and data that the application represents.

View: The view provides the presentation of the model. It is the look of the application. The view can access the model getters, but it has no knowledge of the setters. In addition, it knows nothing about the controller. The view should be notified when changes to the model occur.The view allows the display of information about the model to the user.

Controller: The controller reacts to the user input. It creates and sets the model.The controller allows the user to manipulate the application.

MVC Design Pattern comprised of two models.
  • MVC Model 1 is a Page Centric Architechure
  • MVC Model 2 is a Serlet Centric Architechure
Struts frame work extensively makes use of MVC 2 Architecture

MVC was originally applied in the graphical user interaction model of input, processing and output. In Struts, the view is handled by JSPs and presentation components, the model is represented by Java Beans and the controller uses Servlets to perform its action.

Read more...
Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP