Java struts interview questions – Top STRUTS Interview Questions in Java | STRUTS Framework in Java Interview Question and Answers

Java struts interview questions: We have compiled the most frequently asked Java J2EE Interview Questions which will help you with different expertise levels. You can check out the frequently asked STRUTS Interview Questions & Answers in this tutorial. The Java Interview Questions on Frameworks STRUTS are provided with answers too here. Students can find the Java Questions here based on different levels like for beginners and experienced candidates, etc. Try to test your knowledge of Java Framework STRUTS by answering the questions on your own and improvise on the areas of need.

Java J2EE Interview Questions on STRUTS

Question 1.
What is Struts Definition in Java?
Answer:
Struts are the premier framework for building Java-based Web applications. Using the Model-View-Controller (MVC) design pattern, Struts solves many of the problems associated with developing high-performance, business-oriented Web applications that use Java servlets and JavaServer Pages.

Although the Model-View-Controller architecture is a powerful means of organizing code, developing such code can be a painstaking process. This is where Struts comes in. Struts is a Web application framework that streamlines the building of Web applications based on the MVC design principles. But, what does it mean? Is Struts an MVC Web application that you just add on or extend? Are Struts just some libraries? Actually, Strut is a little bit of both. Struts provide the foundation, or framework, for building MVC-oriented applications along with libraries and utilities for making MVC development faster and easier.

Question 2.
Describe the advantages of using Struts by taking up a suitable analogy?
Answer:
Consider the following analogy. If you were to create a GUI application in Java, you wouldn’t write a text field widget and a drop-down widget yourself. You would instead use Java’s swing API that already has standardized, fully functional code that provides these controls. Not only are the Swing controls ready-to-use, but they are also understood by all Java programmers.

Struts provide the same type of advantages. Struts supply a standard way of implementing an MVC application, the Struts code is tried and true, and the techniques required to use Struts are well known and documented.

Besides, Struts is open source, and information for signing up to be a member of the Struts users mailing list can be found on the Struts Web site at http://struts.apache.org/.

Question 3.
What are the basic components of Struts?
Answer:
The Struts framework is a rich collection of Java libraries and can be broken down into the following major pieces/components:-

  1. The base framework which provides core MVC functionality
  2. JSP tag libraries which enable JSP authors to use HTML-like tags to represent functionality that is defined by a Java class
  3. Tiles plugin or framework which is a rich JSP templating framework that facilitates the reuse of presentation (HTML) code
  4. Validator plugin or framework which provides a rich framework for performing data validation on both the sever side and the client-side (browser),

Question 4.
According to you, is Struts an MVC framework, a collection of utilities, or a set of JSP custom tag libraries?
Answer:
All three viewpoints are legitimate. The answer depends upon what you are going to use it for. However, the most common way of looking at Struts is an MVC framework.

Question 5.
What are the steps involved in installing Struts?
Answer:
Basically, the following three steps are involved in installing and configuring Struts:-

(1) Downloading the Struts code: You can get the latest release build at http:// struts.apache.org/downloads.html. You can get the source code, example applications,
or the documentation, but all that is absolutely required is the struts-blank Web application.

(2) Updating your CLASSPATH: To compile your Struts-based Web application, add struts- core-1.3.5.jar to the CLASSPATH used by your compiler or IDE.

(3) Bookmarking the Struts documentation: You can access the documentation online at http://struts.apache.org/l.x/learning.html. Alternatively, you can download a copy of the documentation for local access.

STRUTS Interview Questions

Question 6.
Explain the Struts flow of control?
Answer:
STRUTS Interview Questions in Java chapter 8 img 1

The Struts flow of control is summarized as follows:-

(1) The user requests a form: The input form is built with the Struts him!: tags. These tags associate a bean with the form so that you can pre-populate the form with values from your application, so that you can redisplay incorrectly filled out forms, and so that Struts knows where to store the request parameters when the form is submitted.

(2) The form is submitted to a URL of the form something.do: The form contains an ACTION ending in something. do. The Struts system receives the request, where an action mapping in struts-config.xml associates the address with an Action object.

(3) The execute method of the Action object is invoked: One of the arguments to ‘execute’ is a form bean that is automatically created and whose properties are automatically populated with the incoming form data. Once it examines the user input data in the bean, the ‘execute’ method invokes business logic and data-access logic, placing the results in normal beans stored in the request, session, or application scope. Finally, the ‘execute’ method uses mapping. find forward to return various conditions that are mapped by struts-config.xml to JSP pages.

(4) Struts forwards the request to the appropriate JSP Page: Struts normally invokes the results page with RequestDispatcherforward, although request.sendRedirect is occasionally used (e.g., with session-scoped data). In most real applications, the results page uses bean: write or the JSP 2.0 EL to output bean properties.

Question 7.
Outline the steps involved in implementing Struts.
Answer:
The following six basic steps are involved in implementing and using Struts.-

(1) Modify struts-config.xml: For basic Struts usage, this file should contain three main sections, viz. a table that maps incoming addresses to Action objects (action entries), a table that maps return conditions to JSP pages (forward entries), and a list of the beans used to handle request parameters (form-bean entries).

(2) Define a form bean: Rather than requiring you to call request.getParameter for each incoming piece of query data, Struts can instantiate and populate a bean representing the incoming form data. This is one of the most useful features of the Struts framework.

(3) Create the results beans: If the JSP page needs data resulting from business logic, then create results beans to hold the data.

(4) Define an Action class to handle the request: Struts automatically invokes the ‘execute’ method of the Action object specified in the action entry of struts-config.xml. The ‘execute’ method typically uses form bean to access data submitted by the user, invokes business logic and data-access logic, stores results in beans based on that logic and calls mapping. find forward to designate the result condition.

(5) Create a form that invokes something.do: Create an HTML form for the user’s input. Use html: form to associate the form with a bean and to specify the address that should handle the form submission. Use HTML: text and related elements to gather user data and to associate bean properties with the input elements.

(6) Display the results in a JSP page: Write a JSP page to display the results. Depending upon the user input and business logic, you could have more than one possible results page. Use bean: write or the JSP EL to output bean properties.

Interview Questions on STRUTS in Java for Experienced

Question 8.
How and why do you lay out pages with Tiles?
Answer:
Many Web sites contain pages with a common header, footer, and menu. If you would like to build your site with a common look and feel, starting with Struts Tiles is an excellent choice. With the Tiles framework, you create a common page layout for your site, and then, based on the layout, use Struts tiles: tags to include JSP fragments and Strings for the main pages of the site. The Tiles approach is much more powerful than the traditional JSP: include for including files in a JSP page. In fact, some developers use Struts just for the sake of the Tiles capability as the servlet/JSP community has very few equivalents.
We outline three main reasons for using Tiles:-

(1) Reuse (not rewrite) repeated sections of pages: For similar content, you need only sections once and then use Tiles to easily add those sections to your JSP pages.

(2) Simplify the creation of similar pages: With templates, you can easily layout similar pages on your site. Then, with the layout template, provide specific content for the layout pieces. ,

(3) Increase flexibility and ease of maintenance: With Tiles, you get a lot more flexibility than with the traditional <jsp: include .. ./> tag. Plus, the supporting elements for Tiles (such as handling relative URLs, inheritance, etc.) eases the overall maintenance of the site. The four basic steps required for using Tiles are enumerated as follows:-

(1) Sketch out the desired page layout

(2) Make a template file that represents the layout using tiles: insert wherever a layout piece should go and tiles: getAsString wherever changeable text goes

(3) Create JSP pages that define the layout pieces

(4) Create JSP pages that populate the layout using tiles: insert to refer to the layout from step 1 and tiles: put to specify the layout pieces that apply to each specific page

Question 9.
How and why do you use the Struts Validation framework?
Answer:
Probably the most tedious and time-consuming part of developing a Web site is I validation of the input data. There is no easy way of getting around it – you have to check for i missing or poorly formatted input values. Unfortunately, the servlet and JSP APIs provide no, the mechanism for easily validating input data. To the rescue come the Struts Validation framework. This framework is incredibly flexible and easy to use, supporting both client-side and server-side validation. For validating form fields, Struts gives you the following options:-

(1) Do validation in the Action: This is the most powerful and flexible approach. Validation in the Action has access to business logic, databases, and so on. However, this validation [ may require repetition of code in multiple Actions and requires you to write the validation rues yourself. Also, you need to manually map conditions back to the input page-

(2) Do validation in the form bean: You can do validation in individual setter methods – not really validation, but useful to modify values. Better, you can use the ‘validate’ method in ActionForm. This approach does not require the repetition of code in multiple Actions but still requires you to write the validation rules. Based on the return value from ‘validate’, Struts can automatically redisplay the input, page.

(3) Use automatic validator: Automatic validation is the most powerful approach. The Struts Validation framework includes many rules for automatically checking the input values (missing data, phone number format, e-mail format, etc.). In addition, the automatic validator can incorporate client-side JavaScript for field validation.

Question 10.
Distinguish between manual and automatic validation?
Answer:
We briefly discuss the differences between manual and automatic validation:-.
“7 Manual Validation: Manual validation of input fields is the most flexible. You have full access to beans, business logic, and the application database. Unfortunately, manual validation often results in repeated logic and often runs only on the server. If you also do client-side validation, then most likely, you are writing lots of tedious JavaScript code embedded in JSP code. This approach violates the Struts strategy of placing as much as possible in editable files versus in code that must be compiled.

Automatic Validation: Automatic validation typically consolidates the amount of validation code needed and lets you use standard validation rules. Also, automatic validation runs on the server, but can optionally include JavaScript that runs on the client. With automatic validation in Struts, instead of building code that requires compilation, you can easily describe the validation rules in XML files.

STRUTS Interview Questions and Answers

Struts Interview Questions and Answers for Experienced in Java

Question 11.
Distinguish between client-side and server-side validation. Should you validate data on the server or the client?
Answer:
A common question for any new Web developer is – “Should I validate data on the client or the server?” For security reasons alone, you must always validate the data on the server – a hacker (or the user) can deliberately or accidentally bypass your input forms completely (especially JavaScript validation on the client). We briefly discuss the differences between client-side and server-side ‘validation:-‘

  • Client-side Validation: For client-side validation, JavaScript code verifies the format of input fields. If any fields are bad, a dialog box can warn the user of any illegal values. If the input is bad, JavaScript can block the submission to the server. Client-side validation with JavaScript tends to be very fast. The validation does not require a trip to the server. However, there are limitations. You cannot do validation that requires application logic, such as talking to the database server. Finally, with client-side validation, you need to consider the possibility of the user deliberately or accidentally bypassing JavaScript (i.e., turning it off).
  •  Server-side Validation: For server-side validation, Java code on the server can validate the fields. Most likely, server-side validation performs similar checks as client-side validation but can invoke stronger application logic for additional checks. Instead of a pop-up dialog box, you can display warning messages back on the input form. Unfortunately, server-side validation is slower, but you really have no choice here. You must do server-side validation regardless of whether or not you do client-side validation.

Question 12.
What do you understand by properties files? What are the advantages of using properties files in the Struts framework?
Answer:
In the simplest terms, properties files are nothing more than text files containing name-value pairs, or rather, key-message pairs. By using the bean: message from the Struts tag library, you can easily retrieve your properties files and display them on your JSP pages. Nearly, all Struts developers use properties files to simplify the development of their Web applications. We summarize the two greatest advantages of using properties files as follows:-

(1) Centralized updates: If the application uses a message in several JSP pages, with properties files you can easily update a message with a single change. This capability is consistent with the Struts philosophy of making as many changes in the configuration files, not in the Java or JSP code.

(2) Internationalization (I18N): If you use messages pervasively in your JSP pages, you can internationalize your application by having multiple properties files corresponding to the locale (as with standard I18N in Java). Simply create a file for each language that requires support. For instance, to support English, Japanese, and Spanish (Mexico dialect), you would create the following message resources properties files, respectively

MessageResouces.properties
MessageResouces_ja.properties
MessageResouces_es_mx.properties

(In the chapter on internationalization, we cover I18N in more detail)

Question 13.

What is the Difference between DispatchAction and LookupDispatchAction in Struts Framework?

Question 14.

What do the Validate () and reset () methods do?

Answer:

validate() method is used to validate properties once they have been populated whereas the reset() method is used to reset all of the ActionForm’s data members before the new request values are set.

Question 15.

What configuration files are used in Struts?

Answer:

There are two configuration files used in STRUTS between the Controller and Model like  ApplicationResources.properties and struts-config.xml