Frequently Asked Struts Interview Questions Part – 2
1.What is Struts?
1.Struts is a web page development framework and an open source software that helps developers build web applications quickly and easily.Struts combines Java Servlets,Java Server Pages,custom tags,and message resources into a unified framework.It is a cooperative,synergistic platform,suitable for development teams,independent developers,and everyone between.
2.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,a variation of the classic Model-View-Controller (MVC) design paradigm.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.
The Struts framework provides the invisible underpinnings every professional web application needs to survive.Struts helps you create an extensible development environment for your application,based on published standards and proven design patterns.
3.struts Frame work is the implementation of Model-View-Controller (MVC) design pattern for the JSP.Struts is maintained as a part of Apache Jakarta project and is open source.Struts Framework is suited for the application of any size.
2.Why its called Struts?
Because the designers want to remind us of the invisible underpinnings that hold up our houses,buildings,bridges,and ourselves when we are on stilts.This excellent description of Struts reflect the role the Struts plays in developing web applications.
3.Who makes the Struts?
Struts is hosted by the Apache Software Foundation(ASF) as part of its Jakarta project,like Tomcat,Ant and Velocity.
4.How Struts control data flow?
Struts implements the MVC/Layers pattern through the use of ActionForwards and ActionMappings to keep control-flow decisions out of presentation layer.
5.What configuration files are used in Struts?
ApplicationResources.properties
struts-config.xml
These two files are used to bridge the gap between the Controller and the Model.
6.How can one enable front-end validation based on the xml in validation.xml?
The tag allows front-end validation based on the xml in validation.xml.
For example the code:
generates the client side JavaScript for the form “logonForm” as defined in the validation.xml file.
The when added in the JSP file generates the client side validation script.
7.How would you display “validation fail” errors on a JSP page?
Following tag displays all the errors:
8.Describe the details of XML files used in the “Validator Framework”?
The Validator Framework uses two XML configuration files
1) validator-rules.xml and
2) validation.xml.
The validator-rules.xml defines the standard validation routines.These are reusable and used in validation.xml to define the form specific validations.
The validation.xml defines the validations applied to a form bean.
9.What is Struts Validator Framework?
The “Struts Framework” provides the functionality to validate the form data.
It can be used to validate the data in the user’s browser as well as on the server side.
Struts Framework checks the JavaScript code and it can be used to validate the form data on the client browser.
Server side validation of form data can be accomplished by subclassing your “form” Bean with DynaValidatorForm class.
The “Validator” framework was developed by David Winterfeldt as a third-party “add-on” to Struts.
Now the Validator framework is part of the “Jakarta Commons” project and it can be used with or without Struts.
The Validator framework comes integrated with the Struts Framework and can be used without any making any additional settings.
10.What is an “ActionForm”?
An “ActionForm” is a JavaBean that extends org.apache.struts.action.ActionForm.ActionForm maintains the session state for web application and the “ActionForm” object is automatically populated on the server side with data entered from a form on the client side.
11.Write code of any Action Class?
Here is the code of Action Class that returns the ActionForward object.
package j2eeonline.jdj.com;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class TestAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServlet
Request request,
HttpServletResponse response) throws Exception{
return mapping.findForward(“testAction”);
}
}
12.What is an “Action Class”?
The “Action Class” is part of the “Model” and is a wrapper around the business logic.The purpose of the “Action Class” is to translate the HttpServletRequest to the business logic.To use the “Action”,we need to subclass and overwrite the execute() method.All the database and business processing is done in the “Action” class.It is advisable to perform all the database related work in the “Action” class.
The ActionServlet (command) passes the parameterized class to ActionForm using the execute() method.The return type of the execute method is ActionForward which is used by the Struts Framework to forward the request to the file according to the value of the returned ActionForward object.
13.How can one make any “Message Resources” definitions file available to the “Struts Framework” environment?
“Message Resources” definitions file are simple .properties files and these files contain the messages that can be used in the struts project.
“Message Resources” definition files can be added to the struts-config.xml file
through tag.Example:
14.What is an ActionServlet?
The class org.apache.struts.action.ActionServlet is called the ActionServlet.In the Jakarta Struts Framework this class plays the role of controller.All the requests to the server go through the “Controller”.The “Controller” is responsible for handling all the requests.
15.What is the Jakarta Struts Framework?
Jakarta Struts is an open source implementation of MVC (Model-View-Controller) pattern for the development of web based applications.Jakarta Struts is a robust architecture and can be used for the development of applications of any size.The “Struts framework” makes it easier to design scalable,reliable Web applications.
16.How is the MVC design pattern used in Struts framework?
In the MVC design pattern,application flow is mediated by a central Controller.The Controller delegates requests to an appropriate handler.The handlers are tied to a Model,and each handler acts as an adapter between the request and the Model.
The Model represents,or encapsulates,an application’s business logic or state.Control is usually then forwarded back through the Controller to the appropriate View.The forwarding can be determined by consulting a set of mappings,usually loaded from a database or configuration file.
This provides a loose coupling between the View and Model,which can make an application significantly easier to create and maintain.Controller–Servlet controller which supplied by Struts itself; View — what you can see on the screen,a JSP page and presentation components; Model — System state and a business logic JavaBeans.
17.What are the core classes of Struts?
Action,ActionForm,ActionServlet,ActionMapping,ActionForward are basic classes of Struts.
18.What configuration files are used in Struts?
ApplicationResources.properties
struts-config.xml
These two files are used to bridge the gap between the Controller and the Model.
19.How you will enable front-end validation based on the xml in validation.xml?
The < html:javascript > tag to allow front-end validation based on the xml in validation.xml.For example the code:
< html:javascript formName=logonForm dynamicJavascript=true staticJavascript=true / > generates the client side java script for the form logonForm as defined in the validation.xml file.The < html:javascript > when added in the jsp file generates the client site validation script.
20.Resources under WEB-INF
According to the Servlet specification,resources (e.g.JSP files) stored under WEB-INF are protected and cannot be accessed directly by the browsers.One design idiom for Struts
1.0 is to put all the JSP files under WEB-INF and front them by Actions so that clients cannot illegally access the JSPs.
With the introduction of sub-application prefixes in Struts
1.1,mapping resources under WEB-INF gets complicated.Extra configuration steps utilizing the pagePattern and forwardPattern attributes of the element in struts-config.xml is required to inform Struts to construct the paths correctly.More specifically,you need to set these attributes to the pattern “/WEB-INF/$A$P”.
21.Explain Library Dependency
Struts
1.1 now depends on a handful of libraries from other Jakarta subprojects (e.g.Commons-Logging,Commons-Collections,etc.).Some of these libraries may cause classloading conflicts in some servlet containers.So far,people have reported in the mailing list the classloading problem of commons-digester/jaxp
1.1,and commons-logging causing deployment difficulties in Struts
1.1 applications running on Weblogic
6.0.(The problems have been corrected in Weblogic
6.1 and
7.0.) .
22.What about JSTL and JavaServer Faces?
JSTL,the JavaServer Standard Tag Library,is a set of JSP tags that are designed to make it easier to develop Web applications.JavaServer Faces (JSF) is a specification for a new technology that promises to make it easier to write MVC applications,both for the Web and for the desktop.
The inventor of Struts,Craig McClanahan,is the specification co-lead for JavaServer Faces (JSR 127),and architect of the reference implemenation as well as Java Studio Creator.Both JSTL and JSF are complementary to Struts.
The mainstay of the Struts framework is the controller components,which can be used with any Java presentation technology.As new technologies become available,it is certain that new “glue” components will also appear to help these technologies work as well with Struts.
Struts originally came bundled with a set of custom JSP tags.Today,several extensions are available to help you use Struts with other popular presentation technologies,like XSLT and Velocity.Likewise,extensions for JSTL and JSF are now available as well.
The JSTL reference implementation is available through the Jakarta Taglibs site.A JSTL taglibs for Struts,Struts-El ,is available and distributed with Struts beginning with the
1.1 release.
The JSF specification and reference implementation is available through Sun’s The JSF specification and reference implementation is available through Sun’s Java ServerFaces page.
An early-release JavaServer Faces taglib for Struts,Struts-Faces,is also in early release and available through the nightly build.The Struts Faces taglib is expected to work with any compliant JSF implementation,including MyFaces.
23.Explain DynaBean and BeanUtils
Another major complaint usually heard amongst Struts
1.0 users is the extensive effort involved in writing the FormBean (a.k.a.ActionForm) classes.
Struts provides two-way automatic population between HTML forms and Java objects,the FormBeans.To take advantage of this however,you have to write one FormBean per HTML form.(In some use cases,a FormBean can actually be shared between multiple HTML forms.
But those are specific cases.) Struts’ FormBean standard follows faithfully the verbose JavaBean standard to define and access properties.Besides,to encourage a maintainable architecture,Struts enforces a pattern such that it is very difficult to ‘reuse’ a model-layer object (e.g.a ValueObject from the EJB tier) as a FormBean.Combining all these factors,a developer has to spend a significant amount of time to write tedious getters/setters for all the FormBean classes.
Struts
1.1 offers an alternative,Dynamic ActionForms,which are based on DynaBeans.Simply put,DynaBeans are type-safe name-value pairs (think HashMaps) but behave like normal JavaBeans with the help of the BeanUtils library.(Both the DynaBeans and the BeanUtils library were found to be useful and generic enough that they have been ‘promoted’ into Jakarta’s Commons project.)
With Dynamic ActionForms,instead of coding the tedious setters/getters,developers can declare the required properties in the struts-config.xml files.Struts will instantiate and initialize Dynamic ActionForm objects with the appropriate metadata.From then onwards,The Dynamic ActionForm instance is treated as if it is an ordinary JavaBean by Struts and the BeanUtils library.
24.Declarative Exception Handling
If you have developed web applications long enough,you will realize a recurring pattern emerges: when the backend (e.g.the EJB tier) throws you an exception,you nearly always need to display an error page corresponding to the type of that exception.
Sooner or later,you will come up with a mechanism to use a lookup table (e.g.an HashMap) to lookup an error page from the exception class.
Struts
1.1 now provides a similar but more powerful mechanism to declare exception handling.In Struts
1.1,you can declare in the struts-config.xml the associations between an exception class and an exception handler.Using the default
exception handler included in Struts,you can also specify the path of the error pages.With this information,Struts will automatically forward to the specified pages when an uncaught exception is thrown from an Action.Like other facilities in Struts,the exception handlers are pluggable.You can write and define your own handler classes if needed.
25.What’s the difference between Struts and Turbine? What’s the difference between Struts and Expresso?
If you are starting from scratch,packages like Turbine and Expresso can be very helpful since they try to provide all of the basic services that your team is likely to need.Such services include things like data persistence and logging.
If you are not starting from scratch,and need to hook up your web application to an existing infrastructure,then “plain vanilla” Struts can be a better choice.The core Struts framework does not presuppose that you are using a given set of data persistence,presentation,or logging tools.Anything goes =:0)
Compared to other offerings,Struts endeavors to be a minimalist framework.We try leverage existing technologies whenever we can and provide only the missing pieces you need to combine disparate technologies into a coherent application.This is great when you want to select your own tools to use with Struts.But,if you prefer a more integrated infrastructure,then packages like Turbine or Expresso (which uses Struts) are perfectly good ways to go.
26.Does Struts include its own unit tests?
Struts currently has two testing environments,to reflect the fact that some things can be tested statically,and some really need to be done in the environment of a running servlet container.
For static unit tests,we use the JUnit framework.The sources for these tests are in the “src/test” hierarchy in the source repository,and are executed via the “test.junit” target in the top-level build.xml file.Such tests are focused on the low-level functionality of individual methods,are particularly suitable for the static methods in the org.apache.struts.util utility classes.
In the test hierarchy,there are also some “mock object” classes (in the org.apache.struts.mock package) so that you can package up things that look like servlet API and Struts API objects to pass in as arguments to such tests.Another valuable tool is Struts TestCase which provides a useful harness for Action classes that can be used with JUnit or Cactus.
27.Action.execute() and Action.getResources()
In Struts
1.0,request handling logic is coded in Action.perform(); however,Action.perform() throws only IOException and SevletException.To facilitate the new declarative exception handling ,the request handling method needs to throw Exception,the superclass of all the checked exceptions.
Therefore,to both maintain backward compatibility and facilitate declarative exception handling,Action.perform() is now deprecated in favour of Action.execute().
You also have to be careful if you use DispatchAction in your existing applications.At the time of writing,the DispatchAction in Struts
1.1 beta has not yet been updated to use execute().(A bug report has been filed in Struts’ bug database.) Therefore,without modifying the DispatchAction class yourself,declarative exception handling will not work with DispatchAction subclasses.
In addition,Action.getResources() is now deprecated.Instead,you should call Action.getResources(HttpServletRequest) instead.This allows Struts to return to you the sub-application specific message resources.Otherwise,the message resources for the default sub-app will be used.
28.Explain Validator
The Validator is not exactly a new feature.The Validator has been in the contrib package in the distribution since Struts
1.0.
1.Since then,part of it has now been refactored and moved into the Jakarta-Commons subproject and renamed the Commons-Validator and the Struts specific portion is now called the Struts-Validator.However,since it is in the contrib package,people may overlook it and it is worthwhile to mention it here.
The Validator provides an extensible framework to define validation rules to validate user inputs in forms.What is appealing in the Validator is that it generates both the server-side validation code and the client-side validation code (i.e.Javascript) from the same set of validation rules defined in an XML configuration file.
The Validator performs the validation based on regular-expression pattern matching.While a handful of commonly used validators are shipped with the framework (e.g.date validator,range validator),you can always define your own ones to suit your need.
29.Can I use multiple HTML form elements with the same name?
Yes.Define the element as an array and Struts will autopopulate it like any oth er.
private String[] id= {};
public String[] getId() { return this.id; }
public void setItem(String id[]) {this.id = id;}
And so forth
30.Why are some of the class and element names counter-intuitive?
The framework grew in the telling and,as it evolved,some of the names drifted.The good thing about a nightly build,is that everything becomes available to the community as soon as it is written.The bad thing about a nightly build is that things like class names get locked down early and then become difficult to change.