The problem of duplicate form submission arises when a user clicks the Submit button more than once before the response is sent back. This may result in inconsistent transactions and must be avoided. In Struts this problem can be handled by using the saveToken() and isTokenValid() methods of Action class. saveToken() method creates a token (a unique string) and saves that in the user’s current session, while isTokenValid() checks if the token stored in the user’s current session is the same as that was passed as the request parameter.

Use the Action Token methods to prevent duplicate submits

There are methods built into the Struts action to generate one-use tokens. A token is placed in the session when a form is populated and also into the HTML form as a hidden property. When the form is returned, the token is validated. If validation fails, then the form has already been submitted, and the user can be apprised.

saveToken(request)

on the return trip,

isTokenValid(request)

resetToken(request)

2. What are the drawback of struts

Disadvantages of Struts:

Bigger Learning Curve To use MVC with Struts, you have to be comfortable with the standard JSP and servlet APIs and a large and elaborate framework that is almost equal in size to the core system.

Less Transparent Struts applications are: Harder to understand and Harder to benchmark and optimize.

Rigid Approach The flip side of the benefit that Struts encourages a consistent approach to MVC is that Struts makes it difficult to use other approaches.

Bigger Learning Curve

To use MVC with the standard RequestDispatcher, you need to be comfortable with the standard JSP and servlet APIs. To use MVC with Struts, you have to be comfortable with the standard JSP and servlet APIs and a large and elaborate framework that is almost equal in size to the core system. This drawback is especially significant with smaller projects, near-term deadlines, and less experienced developers; you could spend as much time learning Struts as building your actual system.

Worse Documentation.

Compared to the standard servlet and JSP APIs, Struts has fewer online resources, and many first-time users find the online Apache documentation confusing and poorly organized. There are also fewer books on Apache Struts than on standard servlets and JSP.

Less Transparent.

With Struts applications, there is a lot more going on behind the scenes than with normal Java-based Web applications. As a result, Struts applications are:

Harder to understand

Harder to benchmark and optimize

Rigid Approach.

The flip side of the benefit that Struts encourages a consistent approach to MVC is that Struts makes it difficult (but by no means impossible) to use other approaches.

3. What are the advantage of struts

You can easily implement the MVC approach by using RequestDispatcher.forward in your servlets and jsp:getProperty or the JSP 2.0 expression language in your JSP pages. However, Struts offers a number of significant advantages over these techniques alone. Here is a summary:

• 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 standardjsp: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 builtin 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).

Consistent Approach.

Struts encourages consistent use of MVC throughout your application.

4. What are the important methods in ActionForm

The important methods of ActionForm are : validate() & reset().

5. Difference between ActionError and ActionMessage

Action Errors(org.apache.struts.action.ActionErrors): A class that encapsulates the error messages being reported by the validate() method of an ActionForm. Validation errors are either global to the entire ActionForm bean they are associated with, or they are specific to a particular bean property (and, therefore, a particular input field on the corresponding form). Each individual error is described by an ActionMessage object, which contains a message key (to be looked up in an appropriate message resources database), and up to four placeholder arguments used for parametric substitution in the resulting message.

ActionMessage(org.apache.struts.action.ActionMessage): An encapsulation of an individual message returned by the validate method of an ActionForm, consisting of a message key (to be used to look up message text in an appropriate message resources database) plus up to four placeholder objects that can be used for parametric replacement in the message text.

6. What are the design patterns used in struts.

Struts is based on model 2 MVC (Model-View-Controller) architecture. Struts controller uses the command design pattern and the action classes use the adapter design pattern. The process() method of the RequestProcessor uses the template method design pattern. Struts also implement the following J2EE design patterns.

Service to Worker

Dispatcher View

Composite View (Struts Tiles)

Front Controller

View Helper

Synchronizer Token

7. Explain Struts plugins.

Struts Plugins are modular extensions to the Struts COntroller. They are defined by the org.apache.struts.action.Plugin interface.Struts Plugins are useful are useful when you are allocating resources or preparing connections to the databases or even JNDI resources. This interface defines two lifecycle mathods: init() and desstroy().

8. How to have multiple config files.

Yes, we can have more than one struts-config.xml for a single Struts application. They can be configured as follows:

The SwitchAction cl 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 cl can be used as is, without extending.

10. What is include action

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