Showing posts with label Struts FAQs. Show all posts
Showing posts with label Struts FAQs. Show all posts

Top 50 Struts Interview Questions with Answers

  1. What is and Why Struts ? 
  2. Why is Struts so Useful? 
  3. How is the MVC design pattern used in Struts Framework? 
  4. Explain Struts Workflow ? 
  5. What are the Core classes of Struts Framework ? 
  6. What does Controller do? 
  7. What is ActionServlet ? 
  8. Why ActionServlet is singleton in Struts ? 
  9. What Does ActionServlet Do ? 
  10. What are the Disadvantages of Struts Framework ? 
  11. Explain about Struts Config File(struts-config.xml) ? 
  12. Is Struts compatible with other Java technologies? 
  13. What is struts Config File? 
  14. What is the importance of 'action-mappings' in Struts ? 
  15. What is form-bean struts Config File? 
  16. What is the "global-forwards" in Struts Config File ? 
  17. What are the JavaBeans Scopes ? 
  18. What is ActionForm ? 
  19. What is Struts Validator Framework ? 
  20. What is ActionMapping Class in Struts ? 
  21. What is ForwardAction ? 
  22. What is IncludeAction ? 
  23. What is DispatchAction ? 
  24. Give the Details of XML files used in Validator Framework ? 
  25. How you will display validation fail errors on jsp page ? 
  26. How you will enable front-end validation based on the xml in validation.xml? 
  27. How to get data from the velocity page in a action class? 
  28. How RequestProcessor decides that validation is success or fail ? 
  29. What is the difference between ActionForm and DynaActionForm ? 
  30. Explain about token feature in Struts ? 
  31. What is the difference between DynaActionForm and DynaValidatorForm? 
  32. Is struts threadsafe? 
  33. What is the difference between perform() and execute() methods ? 
  34. What are the different types of Actions available in Struts ? 
  35. What is switch action and forwardAction in struts ? 
  36. What are the difference between bean:message and bean:write ? 
  37. Explain life cycle of ActionForm ? 
  38. Differences between web.xml and sturts-config.xml ? 
  39. How to prevent multi-click using struts tokens ? 
  40. What are the Validator Built-in Rules in Struts ? 
  41. Explain about the library tag ? 
  42. What is the purpose of Execute method of action class? 
  43. Explain about Tiles ? 
  44. Describe the basic steps used to create a tiles application ? 
  45. Give an example where struts tiles may be used? 
  46. What’s the use of Struts.xml configuration file ? 
  47. Can we have multiple struts-config.xml files in a single Struts application ? 
  48. What’s declarative exception handling in Struts ? 
  49. What is the use of resourcebundle.properties file in Struts Validation framework ? 
  50. Explain about logic match tag ?

What is the use of resourcebundle.properties file in Struts Validation framework ?

resourcebundle.properties file is used to define specific error messages in key value pairs for any possible errors that may occur in the code. This approach helps to keep the code clean as developer doesn’t need to embed all error messages inside code.

Explain about logic match tag ?

This tag is used whenever the requested variable has a value which is present in the sub string. To evaluate the contents present in the nested tag, logic match is used. This tag is used when the requested value is present in the tag of the variable in which this value is present.

Why ActionServlet is singleton in Struts ?

In Struts framework, actionServlet acts as a controller and all the requests made by users are controlled by this controller. ActionServlet is based on singleton design patter as only one object needs to be created for this controller class. Multiple threads are created later for each user request.

What are the Core classes of Struts Framework ?

The core classes provided by Struts Framework
  • Action Class
  • ActionForm Class
  • ActionMapping Class
  • ActionForward Class
  • ActionServlet Class

Can we have multiple struts-config.xml files in a single Struts application ?

We can have any number of Struts-config.xml files for a single application.We need following configurations for this:

<servlet>
 <servlet-name>action</servlet-name>
 <servlet-class> org.apache.struts.action.ActionServlet
 </servlet-class>
 <init-param>
 <param-name>config</param-name>
 <param-value>
  /WEB-INF/struts-config.xml,/WEB-INF/struts-config_user.xml,
  /WEB-INF/struts-config_admin.xml
 </param-value>
</init-param>
</servlet>

What’s the use of Struts.xml configuration file ?

Struts.xml file is one the key configuration files of Struts framework which is used to define mapping between URL and action. When a user’s request is received by the controller, controller uses mapping information from this file to select appropriate action class.

What is declarative exception handling in Struts ?

When logic for exception handling is defined in struts-config.xml or within the action tag, it’s known as declarative exception handling in Struts. In the following example, we have defined exception in struts-config.xml file for NullPointerException:
<global-exceptions>
    <exception key="test.key"   Type="java.lang.NullPointerException"
      Path="/WEB-INF/errors/error_page.jsp" />
 </global-exceptions>

What’s the purpose of Execute method of action class?

Execute method of action class is responsible for execution of business logic. If any processing is required on the user’s request, it’s performed in this method. This method returns actionForward object which routes the application to appropriate page.

Give an example where struts tiles may be used?

Tiles help the developer to create a template and a validator framework. If a developer is planning to develop a website where there would be almost 500 pages of static content and additional dynamic content then it is recommended for him to use template designed with the help of Tiles because it would help him in making necessary changes.

Describe the basic steps used to create a tiles application ?

Tiles are useful in creating web applications. Some of the steps are
  1. Adding the TLD to the web.xml file so that the application know about the librarys we are using.
  2. A layout should be created with the help of JSP.
  3. Designed and developed layouts should be used in creating pages.
  4. Make the whole application adhere to the rules present and test it for any errors. Don’t forget to repackage the application.

Explain about Tiles ?

Tiles allow template mechanism to be present in your components. You can compose a presentation layer from different components such as footer, header and content components. This form of composition helps in validating web forms much easier.

Explain about the library tag ?

Library tag is a very powerful tag which is used to write code for Java beans programs. This tag gives a special permission to read and write the content of beans without any special form of embedded java code in the application.

What is DispatchAction ?

Basically Dispatch action is used when you have multiple submit buttons in a single form.The Dispatch action is a class which extends the Struts DispatchAction , and encpsulates all the action methods (similar to execute method in Action class)in one single class.These actions methods will get executed based on the parameter that you pass in a jsp page.In the Struts-Config.xml you need to define the DispatchAction class and the parameter name that you passing in the URL.Based on the parameter the Controller will invoke the action method.

struts-config.xml
parameter – attribute is used to delegate the request to corresponding method of the BookAction class. So define this value with appropriate name.




 
BookAction.java
It eliminates the execute method entirely.  Because it directly implements the desired methods, those custom methods have same signature as execute method.
The class BookAction extends org.apache.struts.actions.DispatchAction
   import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.actions.DispatchAction;

public class BookAction extends DispatchAction
{
public ActionForward addBook(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Add Book Page");
return mapping.findForward("addBook");
}

public ActionForward editBook(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Edit Book Page");
return mapping.findForward("editBook");
}

public ActionForward saveBook(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Save Book Page");
return mapping.findForward("saveBook");
}

public ActionForward deleteBook(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
System.out.println("Delete Book Page");
return mapping.findForward("deleteBook");
}
} 
bookdetails.jsp
This page has four different links for doing different actions say Add/Edit/Save/Delete books. Every individual action has to be uniquely represented by the request parameter actionMethod.
 


Book Details

Add Book
Edit Book
Save Book
Delete Book



What is IncludeAction ?

An Action that includes the context-relative URI specified by the parameter property of our associated ActionMapping. This can be used to integrate Struts with other business logic components that are implemented as servlets (or JSP pages), but still take advantage of the Struts controller servlet's functionality

    


What is ForwardAction ?

The struts framework ForwardAction class provides a mechanism for forwarding to a specified URL. In an MVC Web application, all requests to the application are supposed to flow through the Controller servlet. 

This will ensure that the Controller layer of the application has an opportunity to prepare any resources that may be needed to handle the request meaning selecting the correct module and so on. The struts framework ForwardAction is provided as a simple utility action that can be used for scenarios in which you simply want to link to a JSP page.

Example:
 


What is ActionMapping Class in Struts ?

  • ActionMappings is a collection of ActionMapping objects
  • The ActionMapping contains the knowledge of how a specific event maps to specific Actions.
  • The ActionServlet (Command) passes the ActionMapping to the Action class via the perform() method.
  • This allows Action to access the information to control flow.
  • The struts-config.xml determines what Action class the Controller calls.
  • The struts-config.xml configuration information is translated into a set of ActionMapping, which are put into container of ActionMappings.
  • Classes that end with s are containers.


Why is Struts so Useful?

  • structural separation of data presentation and business logic
    • easy separation of development tasks (web design, database, …)
    • increases maintainability and extendibility (new views!)
    • increases reusability of code
  • Struts provides a Controller that manages the control flow
    • changes in the flow can all be done in struts-config.xml
    • abstraction from (hard coded) filenames (forwards)
  • easy localization (internationalization is more important than ever)
  • based on standard Java technologies (JSP, Servlets, JavaBeans)
    • thus running on all kinds of JSP/Servlet containers
  • open-source
    • affordable
    • no dependence on external companies
    • robustness (due to freely accessible source code)
  • open-source project with growing developer community
     

What are the Validator Built-in Rules in Struts ?

  1. required : field data provided
  2. minlength : more than min length
  3. maxlength : less than max length
  4. range : in range of values
  5. mask : correct format (takes regexp)
  6. date : validates correct date format
  7. email : validates correct E-Mail fromat
  8. primitives: byte, integer, float, double, etc.

How to prevent multi-click using struts tokens ?

Mutli-click prevention using struts tokens- Prevent Duplicate Submission :
saveToken() : generate the token key and save to request/session attribute.
isTokenValid() : validate submitted token key against the 1 store in request/session.
resetToken() : reset the token key

Follow the steps to setup Mutli-click prevention using struts tokens.

Step 1: Action Class where saveToken() before JSP Page

First saveToken() then forward to your jsp.
Upon loading the form, invokes saveToken() on the action class to create and store the token key.
Struts will store the generated key in request/session.
public class LoadAction extends Action 
{
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
{ ActionForward forward;
saveToken(request);
forward=mapping.findForward("empformpage");
// this is the jsp page where you want to struts tokens.
return forward;
}
}
Step 2:Store Token key as a hidden field ( empform.jsp) 
In the browser if you type : http://localhost:8080/testApp/loadActio.do 
This will call execute() method of LoadAction. Then saveToken(request);//create and store the token key and mapping.findForward("empformpage"); forward to empform.jsp (below code is for empform.jsp)
<%@ page import="org.apache.struts.action.Action"%>
<%@ page import="org.apache.struts.taglib.html.Constants"%>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>



Save

Step 3. Check Token is Valid ?
Once the form submitted, invokes isTokenValid() on the action class, it will validate the submitted token key(hidden field) with the token key stored previously on request/session. If match, it will return true.
public class SubmitAction extends Action
{
public ActionForward execute(ActionMapping mapping ,ActionForm form ,HttpServletRequest request,
HttpServletResponse response)
{
EmpForm frm=(EmpForm)form;
if(isTokenValid(request))
{ // This is Not Duplicate Submission of the form
// You can add your logic here 
resetToken(request);
return mapping.findForward("sucess");
}
else
{ // This is Duplicate Submission of the form
// Return to the JSP to display the error message ( This is Duplicate Submission);
return mapping.findForward("duplicatesubmitpage");
}
}
}


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email