Struts FAQ
February 22, 2008 by muralis
Check here and read about Struts FAQ .
Difference between Design Pattern and Framework.
Framework is a skeleton of code for developing particular type of application. For example, house can be treated as framework because it have predefined components like kitchen, bathroom, bedroom etc. Further it can be used to use to develop any type of house.
Where as a design pattern is a solution for particular problem. For example design pattern for re-usability.
So the relationship between framework and design pattern is that a framework is made by using one or more design patterns.
Drawbacks of StrutsIn struts , their is no facility of backward flow.Suppose we are in page 1 and when we submit it calls action mapping page2.Their may be lot of variable stored in session , which is available to page2.Now we wish to go page1 from page 2, for this we have to call the action mapping of page1. But struts flow is always in forward direction. So when we call page 1, values stored in session never get reversed.So it reduces the performance.To resolve this problem of struts, Their is a framework called Web Flow Navigation Manager(WFNM) of Sourgeforge.net.This framework can be integrated with struts.
Token feature in StrutsTokens are used to prevent multi click problem in struts.In Action class method at the beginning of the code, need to call the saveToken() that generates an unique identifier for the “request url” and stores it in the session and also in the jsp as hidden param to which the request forwarded to. The hidden param in jsp is automatically created by struts if we use tag
isTokenValid() compares the users session token with the token given as a request parameter (either through hidden form field (form tag) or through additional parameter on a url (link tag)).
resetToken() removes the token from the session.
Difference between frameworks and librariesA framework is just an application with a lot of hooks; you can design a framework in an entirely ad-hoc fashion by starting with an app that does one thing and trying to generalize in various directions. You can stop at almost any moment and call it “a framework”. But a good library requires much more — there, you need to start with requirements, abstractions and attempt at a minimal API that addresses the maximal set of requirements. Frameworks have no requirement to be minimal in size while maximal in features.
A framework is a set of classes which handles the flow necessary to perform a complex task, but which requires plug-in classes specific to your application (database, app server), and which allows for additional plug-in classes to extend the functionality. For example, you could have a framework which handles the general flow of an online store. This framework would handle tasks such as “put item in shopping cart”, “generate order on checkout”, etc. On the other hand, you would need to provide the classes which enable the framework to persist the shopping cart to the database of your choice, to connect to a credit-card processor, to send orders to a warehouse, among other things. Further, if you wanted to decided that you wanted to offer monogramming, you would need to provide classes to do so, and the framework should make it easy to plug these in.
An API is really no more than a set of method signatures and other information necessary to use a class or set of classes. The API is totally distinct from the implementation. For example, a number of vendors have implemented the servlet API, each in a different way. The API by itself has no implementation.
A class library would have an API, as would a framework.
Some more differences between frameworks and libraries.
- A framework does not have as stable an api as a library.
- A framework is often hyped. As the word ‘framework’ is a buzzword, buzzwords often hang around frameworks. Buzzwords being marketing, marketing being manipulation.
- A framework is not as modular. Often you need to eat the whole framework to use small parts, or the parts are not used separately much.
Why do we need Struts ?
Although a dynamic web application can be developed using JSP and Servlet technology, there are many problems.
Tightly coupled classes - one change affects other classes
Business logic resides in both presentation and data access layer. So if there is a change in a business rule, needs multiple places to find the business rules. Also promotes duplicate business rules.
Data sources are difficult to change as data access methods may be used in the presentation layer.
Logic to determine what to do next is across the application,no centralized decision.
To solve these problems,we need a mechanism that can separate the presentation layer of a web application from business logic. Luckily we have MVC and we have Struts. MVC is a design pattern (best practice method) that separates the application design into there main parts:
Model, View and Controller
Model - data and logic (business logic methods).
View - output displayed to the user
Controller - takes the input from the user and passes it to the relevant model. The model returns and based on the result, the controller decides which view (page) to show the user.
Advantages of Struts :
Struts offers many advantages to the application programmer while reducing the development time and making the manageability of the application easier. Here are few key advantages of using Struts instead of managing the every layer of the web application yourself.
Centralized File-Based Configuration
Rather than hard coding information into java programs,many Struts values are represented in XML or property files. This loose coupling means that many changes can be made without modifying or recompiling Java code,and that wholesale changes can be made by editing a single file. This approach also lets Java and Web developers focus on their specific tasks (implementing business logic,presenting certain values to clients,etc.) without needing to know about the overall system layout.
Form Beans
In JSP,you can use property=”*” with jsp:setProperty to automatically populate a JavaBean component based on incoming request parameters. Unfortunately,however,in the standard API this capability is unavailable to servlets,even though with MVC it is really servlets,not JSP pages,that should usually be the target of form submissions. Apache Struts extends this capability to Java code and adds in several useful utilities,all of which serve to greatly simplify the processing of request parameters.
Bean Tags
Apache Struts provides a set of custom JSP tags (bean:write,in particular) that let you easily output the properties of JavaBeans components. Basically,these are concise and powerful variations of the standard jsp:useBean and jsp:getProperty tags.
HTML tags
Apache Struts provides a set of custom JSP tags to create HTML forms that are associated with JavaBeans components. This bean/form association serves two useful purposes:
It lets you get initial form-field values from Java objects.
It lets you redisplay forms with some or all previously entered values intact.
Form Field Validation
Apache Struts has built-in capabilities for checking that form values are in the required format. If values are missing or in an improper format,the form can be automatically redisplayed with error messages and with the previously entered values maintained.
This validation can be performed on the server (in Java),or both on the server and on the client (in JavaScript).