Tutorial Downloads .com

Home

Struts interview Questions and answers | FAQs

Interview questions and answers of Struts

Describe Jakarta Struts Framework

It implements Model-View-Controller pattern in the framework and it is an open source code. It saves time in design and implementation of a web based application due to this framework and pattern as I is highly robust, scalable and reliable.

 

What are the components of Struts?

Struts is based on the MVC design pattern. Struts components can be categories into Model, View and Controller.Model: Components like business logic / business processes and data are the part of Model.

 

View: JSP, HTML etc. are part of View

 

Controller: Action Servlet of Struts is part of Controller components which works as front controller to handle all the requests.

 

Describe is ActionServlet

ActionServlet provides the "controller" in the Model-View-Controller (MVC) design pattern for web applications that is commonly known as "Model 2". This nomenclature originated with a description in the JavaServerPages Specification, version 0.92, and has persisted ever since (in the absence of a better name).Controller is responsible for handling all the requests.

 

What is an Action Class used for?

An Action is an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request. The controller (ActionServlet) will select an appropriate Action for each request, create an instance (if necessary), and call the perform method.

Actions must be programmed in a thread-safe manner, because the controller will share the same instance for multiple simultaneous requests. In this means you should design with the following items in mind:

• Instance and static variables MUST NOT be used to store information related to the state of a particular request. They MAY be used to share global resources across requests for the same action.

• Access to other resources (JavaBeans, session variables, etc.) MUST be synchronized if those resources require protection. (Generally, however, resource classes should be designed to provide their own protection where necessary.

 

When an Action instance is first created, the controller servlet will call setServlet() with a non-null argument to identify the controller servlet instance to which this Action is attached. When the controller servlet is to be shut down (or restarted), the setServlet() method will be called with a null argument, which can be used to clean up any allocated  resources in use by this Action.

 

What is ActionForm?

An ActionForm is a JavaBean optionally associated with one or more ActionMappings. Such a bean will have had its properties initialized from the corresponding request parameters before the corresonding action's perform() method is called. When the properties of this bean have been populated, but before the perform() method of the action is called, this bean's validate() method will be called, which gives the bean a chance to verify that the properties submitted by the user are correct and valid. If this method finds problems, it returns an error messages object that encapsulates those problems, and the controller servlet will return control to the corresponding input form. Otherwise, the validate() method returns null(), indicating that everything is acceptable and the corresponding Action's perform() method should be called.This class must be subclassed in order to be instantiated. Subclasses should provide property getter and setter methods for all of the bean properties they wish to expose, plus override any of the public or protected methods for which they wish to provide modified functionality.

 

What is Struts Validator Framework?

Struts Framework provides the functionality to validate the form data. It can be use to validate the data on the users browser as well as on the server side. Struts Framework emits the java scripts and it can be used validate the form data on the client browser.Server side validation of form can be accomplished by sub classing your From Bean with DynaValidatorForm class. 

 

What are the core classes of the Struts Framework?

Core classes of Struts Framework are ActionForm, Action, ActionMapping, ActionForward, ActionServlet etc.

 

What are Tag Libraries provided with Struts?

Struts provides a number of tag libraries that helps to create view components easily. These tag libraries are:

a) Bean Tags: Bean Tags are used to access the beans and their properties.

b) HTML Tags: HTML Tags provides tags for creating the view components like forms, buttons, etc..

c) Logic Tags: Logic Tags provides presentation logics that eliminate the need for scriptlets.

d) Nested Tags: Nested Tags helps to work with the nested context.

 

What are difference between ActionErrors and ActionMessage?

ActionMessage: A class that encapsulates messages. Messages can be either global or they are specific to a particular bean property.

Each individual message 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.

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).

 

How you will handle exceptions in Struts?

In Struts you can handle the exceptions in two ways:

a) Declarative Exception Handling: You can either define global exception handling tags in your struts-config.xml or define the exception handling tags within .. tag.

Example:

 

key="database.error.duplicate"

path="/UserExists.jsp"

type="mybank.account.DuplicateUserException"/>

b) Programmatic Exception Handling: Here you can use try{}catch{} block to handle the exception.

 

Give the Details of XML files used in Validator Framework?

The Validator Framework uses two XML configuration files validator-rules.xml and validation.xml

The validation-rules.xml is provided with the Validator Framework and it declares and assigns the logical names to the validation routines. It also contains the client-side javascript code for each validation routine. The validation routines are java methods plugged into the system to perform specific validations.

 

Following table contains the details of the elements in this file:

Element

 

 

 

Attributes and Description

 

 

 

form-validation

 

 

 

This is the root node. It contains nested elements for all of the other configuration settings.

 

 

 

global

 

 

 

The validator details specified within this, are global and are accessed by all forms.

 

 

 

validator

 

 

 

The validator element defines what validators objects can be used with the fields referenced by the formset elements.

 

The attributes are:

 

name: Contains a logical name for the validation routine

 

classname: Name of the Form Bean class that extends the subclass of ActionForm class

 

method: Name of the method of the Form Bean class

 

methodParams: parameters passed to the method

 

msg:Validator uses Struts' Resource Bundle mechanism for externalizing error messages. Instead of having hard-coded error messages in the framework, Validator allows you to specify a key to a message in the ApplicationResources.properties file that should be returned if a validation fails. Each validation routine in the validator-rules.xml file specifies an error message key as value for this attribute.

 

depends: If validation is required, the value here is specified as 'required' for this attribute.

 

jsFunctionName: Name of the javascript function is specified here. 

 

 

 

javascript

 

 

 

Contains the code of the javascript function used for client-side validation. Starting in Struts 1.2.0 the default javascript definitions have been consolidated to commons-validator. The default can be overridden by supplying a element with a CDATA section, just as in struts 1.1.

 

 

 

 

The Validator plug-in (validator-rules.xml) is supplied with a predefined set of commonly used validation rules such as Required, Minimum Length, Maximum length, Date Validation, Email Address validation and more. This basic set of rules can also be extended with custom validators if required.

 

Structure of validation.xml

This validation.xml configuration file defines which validation routines that is used to validate Form Beans. You can define validation logic for any number of Form Beans in this configuration file. Inside that definition, you specify the validations you want to apply to the Form Bean's fields. The definitions in this file use the logical names of Form Beans from the struts-config.xml file along with the logical names of validation routines from the validator-rules.xml file to tie the two together.

 

 

Example of form in the validation.xml file:

depends="required">

depends="required,mask">

mask

^[0-9a-zA-Z]*$

Element

 

 

 

Attributes and Description

 

 

 

form-validation

 

 

 

This is the root node. It contains nested elements for all of the other configuration settings

 

 

 

global

 

 

 

The constant details are specified in element within this element.

 

 

 

constant

 

 

 

Constant properties are specified within this element for pattern matching.

 

 

 

constant-name

 

 

 

Name of the constant property is specified here

 

 

 

constant-value

 

 

 

Value of the constant property is specified here.

 

 

 

formset

 

 

 

This element contains multiple

elements

 

 

 

form

 

 

 

This element contains the form details.

The attributes are:

name:Contains the form name. Validator uses this logical name to map the validations to a Form Bean defined in the struts-config.xml file

 

 

 

field

 

 

 

This element is inside the form element, and it defines the validations to apply to specified Form Bean fields.

 

The attributes are:

 

property: Contains the name of a field in the specified Form Bean

 

depends: Specifies the logical names of validation routines from the validator-rules.xml file that should be applied to the field.

 

 

 

arg

 

 

 

A key for the error message to be thrown incase the validation fails, is specified here

 

 

 

var

 

 

 

Contains the variable names and their values as nested elements within this element.

 

 

 

var-name

 

 

 

The name of the criteria against which a field is validated is specified here as a variable

 

 

 

var-value

 

 

 

The value of the field is specified here

 

 

 

 

var AdBrite_Title_Color = '000000';

var AdBrite_Text_Color = '000000';

var AdBrite_Background_Color = 'FFFFFF';

var AdBrite_Border_Color = 'FFFFFF';

 

 

How you will display validation fail errors on jsp page?

Following tag displays all the errors:

 

 

How you will enable front-end validation based on the xml in validation.xml?

The tag to allow front-end validation based on the xml in validation.xml. For example the code: generates the client side java script for the form "logonForm" as defined in the validation.xml file. The when added in the jsp file generates the client site validation script.

 

Can I setup Apache Struts to use multiple configuration files?

Yes Struts can use multiple configuration files. Here is the configuration example:

banking

org.apache.struts.action.ActionServlet

config

/WEB-INF/struts-config.xml,

/WEB-INF/struts-authentication.xml,

/WEB-INF/struts-help.xml

1

 

How you will make available any Message Resources Definitions file to the Struts Framework Environment?

Message Resources Definitions file are simple .properties files and these files contains the messages that can be used in the struts project. Message Resources Definitions files can be added to the struts-config.xml file through tag.

Example:

 

 

What is Struts Flow?

Struts Flow is a port of Cocoon's Control Flow to Struts to allow complex workflow, like multi-form wizards, to be easily implemented using continuations-capable JavaScript. It provides the ability to describe the order of Web pages that have to be sent to the client, at any given point in time in an application. The code is based on a proof-of-concept Dave Johnson put together to show how the Control Flow could be extracted from Cocoon. (Ref: http://struts.sourceforge.net/struts-flow/index.html )

 

What are the difference between and ?

: This tag is used to output locale-specific text (from the properties files) from a MessageResources bundle.

: This tag is used to output property values from a bean. is a commonly used tag which enables the programmers to easily present the data.

 

What is LookupDispatchAction?

An abstract Action that dispatches to the subclass mapped execute method. This is useful in cases where an HTML form has multiple submit buttons with the same name. The button name is specified by the parameter property of the corresponding ActionMapping.

 

What do you understand by DispatchAction?

DispatchAction is an action that comes with Struts 1.1 or later, that lets you combine Struts actions into one class, each with their own method. The org.apache.struts.action.DispatchAction class allows multiple operation to mapped to the different functions in the same Action class.

For example:

A package might include separate RegCreate, RegSave, and RegDelete Actions, which just perform different operations on the same RegBean object. Since all of these operations are usually handled by the same JSP page, it would be handy to also have them handled by the same Struts Action.

A very simple way to do this is to have the submit button modify a field in the form which indicates which operation to perform.

 

function set(target)

{document.forms[0].dispatch.value=target;}

SAVE

SAVE AS NEW

DELETE

Then, in the Action you can setup different methods to handle the different operations, and branch to one or the other depending on which value is passed in the dispatch field.

String dispatch = myForm.getDispatch();

if ("create".equals(dispatch)) { ...

if ("save".equals(dispatch)) { ...

The Struts Dispatch Action [org.apache.struts.actions] is designed to do exactly the same thing, but without messy branching logic. The base perform method will check a dispatch field for you, and invoke the indicated method. The only catch is that the dispatch methods must use the same signature as perform. This is a very modest requirement, since in practice you usually end up doing that anyway.

To convert an Action that was switching on a dispatch field to a DispatchAction, you simply need to create methods like this

public ActionForward create(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException { ...

public ActionForward save(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws IOException, ServletException { ...

Cool. But do you have to use a property named dispatch? No, you don't. The only other step is to specify the name of of the dispatch property as the "parameter" property of the action-mapping. So a mapping for our example might look like this:

 

path="/reg/dispatch"

type="app.reg.RegDispatch"

name="regForm"

scope="request"

validate="true"

parameter="dispatch"/>

If you wanted to use the property "o" instead, as in o=create, you would change the mapping to

 

path="/reg/dispatch"

type="app.reg.RegDispatch"

name="regForm"

scope="request"

validate="true"

parameter="o"/>

Again, very cool. But why use a JavaScript button in the first place? Why not use several buttons named "dispatch" and use a different value for each?

You can, but the value of the button is also its label. This means if the page designers want to label the button something different, they have to coordinate the Action programmer. Localization becomes virtually impossible.

 

Is struts threadsafe?Give an example?

Struts is not only thread-safe but thread-dependant. The response to a request is handled by a light-weight Action object, rather than an individual servlet. Struts instantiates each Action class once, and allows other requests to be threaded through the original object. This core strategy conserves resources and provides the best possible throughput. A properly-designed application will exploit this further by routing related operations through a single Action

 

What are the uses of tiles-def.xml file, resourcebundle.properties file, validation.xml file?

tiles-def.xml is is an xml file used to configure tiles with the struts application. You can define the layout / header / footer / body content for your View.

 

What is the difference between perform() and execute() methods?

Perform method is the method which was deprecated in the Struts Version 1.1. In Struts 1.x, Action.perform() is the method called by the ActionServlet. This is typically where your business logic resides, or at least the flow control to your JavaBeans and EJBs that handle your business logic. As we already mentioned, to support declarative exception handling, the method signature changed in perform. Now execute just throws Exception. Action.perform() is now deprecated; however, the Struts v1.1 ActionServlet is smart enough to know whether or not it should call perform or execute in the Action, depending on which one is available.

 

How Struts relates to J2EE?

Struts framework is built on J2EE technologies (JSP, Servlet, Taglibs), but it is itself not part of the J2EE standard.

 

What is Struts actions and action mappings?

A Struts action is an instance of a subclass of an Action class, which implements a portion of a Web application and whose perform or execute method returns a forward.

 

An action can perform tasks such as validating a user name and password.

 

An action mapping is a configuration file entry that, in general, associates an action name with an action. An action mapping can contain a reference to a form bean that the action can use, and can additionally define a list of local forwards that is visible only to this action.

 

An action servlet is a servlet that is started by the servlet container of a Web server to process a request that invokes an action. The servlet receives a forward from the action and asks the servlet container to pass the request to the forward's URL. An action servlet must be an instance of an org.apache.struts.action.ActionServlet class or of a subclass of that class. An action servlet is the primary component of the controller.


Resource:
Posted By : preet
On date : 07.11.08

Most used JAVA Tutorials

JAVA Tutorials ( Ebooks , PDF's )
Java Code Conventions Java and C++ A critical comparison Generics in the Java Programming Language Writing Robust Java Code
Brewing Java: A Tutorial
Part 1 is a brief introduction to what Java is, why it's cool and what you need to use it. Part 2 is a tutorial introduction to Java that just covers what you need to know to start programming command line applications in Java. This is an introduction to the basic syntax of the language. It skims over many details and completely omits little used features like bit-shift operators. This section is fairly complete. Part 3 covers the basics of writing applets in Java. Part 4 introduces you to objects and classes.
JAVA Interview Questions & Answers
What is a transient variable, Why do threads block on I/O, How are Observer and Observable used, What is synchronization and why is it important, Can a lock be acquired on a class, What is the Collections API,What is the List interface, What is the Vector class, What is an Iterator interface, What is the difference between yielding and sleeping, What are wrapper classes, What is the difference between preemptive scheduling and time slicing, What is clipping, What class is the top of the AWT event hierarchy, What is the purpose of the wait(), notify(), and notifyAll() methods ... etc... etc...
Struts interview Questions and answers | FAQs
Interview questions and answers of Struts
The java.util.concurrent package Collections
The java.util.concurrent package includes a number of additions to the Java Collections Framework.
JAVA PDF Tutorials, Code Conventions, Writing Robust Java Code
Java Code Conventions,Java and C++ A critical comparison,Generics in the Java Programming Language,Writing Robust Java Code
JFreeChart Introduction
JFreeChart is a free 100% Java chart library that makes it easy for developers to display professional quality charts in their applications. JFreeChart's extensive feature set includes:
SCJP Sun Certified Programmer for Java 6 Exam 310-065 - Tutorial book
With hundreds of practice questions and hands-on exercises, SCJP Sun Certified Programmer for Java 6 Study Guide covers what you need to know--and shows you how to prepare--for this challenging exam.
NetBeans, Java, and JFreeChart | PDF study material
This set of “papers” consists of an introduction to JFreeChart and NetBeans as an IDE for Java programming. I attempt herein to ease the learning burden, a little, compared to the misery that I had to suffer through. We are using JDK 1.5.0_03, NetBeans 4.1 (for future work, we've gone over to 5.0), and jfreechart-1.0.0-rc1. I hope its not necessary to mention that all of these programs are free, in the public domain, and can be downloaded and installed on modern PCs.
JSF and Struts Classic Reference Manual | Pdf study material
The following chapters describe how to deal with classic/old style of JSF and Struts development. We recommend users to use JBoss Seam [http://www.redhat.com/developers/jbds/Getting_Started/GetStartSeamGen.html] to simplify development, but until then you can read about classical JSF and Struts usage here.We are going to show you how to create a simple JSF application using the JBoss Developer Studio plug-in for Eclipse.
Effective Java (2nd Edition) - Java Book
Are you looking for a deeper understanding of the Java™ programming language so that you can write code that is clearer, more correct, more robust, and more reusable? Look no further! Effective Java™, Second Edition, brings together seventy-eight indispensable programmer’s rules of thumb: working, best-practice solutions for the programming challenges you encounter every day.
Java Interface User Guide (JAVA PDF)
This Java tutorial covers the details of Java Interface User Guide.An Introduction to the Java Interface The Datalogics Java Interface provides a Java-language wrapper to the Adobe PDF Library to facilitate the rapid creation of PDF documents via Java calls. The Adobe PDF Library is C-based, not Java-sourced, but as it is usable by any application type which supports calls to a C-based library, the Datalogics Java Interface has been created.
Java Technology - What java does? - How java works
Java technology is both a programming language and a platform. In the Java programming language, all source code is first written in plain text files ending with the .java extension. Those source files are then compiled into .class files by the javac compiler. A .class file does not contain code that is native to your processor; it instead contains bytecodes - the machine language of the Java Virtual Machine1 (Java VM). The java launcher tool then runs your application with an instance of the Java Virtual Machine.
Executor Interfaces in JAVA
The java.util.concurrent package defines three executor interfaces: Executor, a simple interface that supports launching new tasks. ExecutorService, a subinterface of Executor, which adds features that help manage the lifecycle, both of the individual tasks and of the executor itself.
BASIC CONCEPTS LESSON-1 JAVA OVERVIEW (JAVA PDF)
This Java tutorial covers the details of BASIC CONCEPTS LESSON-1 JAVA OVERVIEW (JAVA PDF).Before you can start writing Java programs, you need acquire and set up some kind of Java programming software.

Latest added JAVA Tutorials

TDD and Acceptance TDD for Java Developers tutorial book
Learn hands-on to test drive Java code How to avoid common TDD adoption pitfalls Acceptance test driven development and the Fit framework How to test Java EE components-Servlets, JSPs, and Spring Controllers
Java Concurrency in Practice - Book
Threads are a fundamental part of the Java platform. As multicore processors become the norm, using concurrency effectively becomes essential for building high-performance applications. Java SE 5 and 6 are a huge step forward for the development of concurrent applications, with improvements to the Java Virtual Machine to support high-performance, highly scalable concurrent classes and a rich set of new concurrency building blocks. In Java Concurrency in Practice, the creators of these new facilities explain not only how they work and how to use them, but also the motivation and design patterns behind them.
SCJP Sun Certified Programmer for Java 6 Exam 310-065 - Tutorial book
With hundreds of practice questions and hands-on exercises, SCJP Sun Certified Programmer for Java 6 Study Guide covers what you need to know--and shows you how to prepare--for this challenging exam.
Head First Design Patterns - Java tutorial Book
At any given moment, somewhere in the world someone struggles with the same software design problems you have. You know you don't want to reinvent the wheel (or worse, a flat tire), so you look to Design Patterns--the lessons learned by those who've faced the same problems. With Design Patterns, you get to take advantage of the best practices and experience of others, so that you can spend your time on... something else. Something more challenging. Something more complex. Something more fun.
Effective Java (2nd Edition) - Java Book
Are you looking for a deeper understanding of the Java™ programming language so that you can write code that is clearer, more correct, more robust, and more reusable? Look no further! Effective Java™, Second Edition, brings together seventy-eight indispensable programmer’s rules of thumb: working, best-practice solutions for the programming challenges you encounter every day.
Head First Java, 2nd Edition - Java Book
It has taken four years, but with Head First Java the introductory Java book category has finally come of age. This is an excellent book, far more capable than any of the scores of Java-for-novices books that have come before it. Kathy Sierra and Bert Bates deserve rich kudos--and big sales--for developing this book's new way of teaching the Java programming language, because any reader with even a little bit of discipline will come away with true understanding of how the language works. Perhaps best of all, this is no protracted "Hello, World" introductory guide. Readers get substantial exposure to object-oriented design and implementation, serialization, neatwork programming, threads, and Remote Method Invocation (RMI).
Learn how to load, validate and submit forms in Ext JS
This tutorial focuses on the topic of 'Processing Forms'. The journey will include client-side and server-side ?eld validation, form loading, submission, ?eld customization, and layout techniques that will make it a breeze to build great-looking and friendly forms.
Useful Apache Maven Plugins
This tutorial discusses some of the Maven plugins, both from Apache Maven and Codehaus Mojo project which are of great help to Maven builds.
while - Java Term
Details about the Java term while
volatile - Java Term
Details about the Java term volatile