Tuesday, February 14, 2017

Struts Interview Questions and answers

In this post we will learn frequently asked Struts Interview Questions.As a senior developer,must have strong knowledge on Struts Framework. This is one of the Core Framework to develop web application efficient and effective manner.

1) What is Struts?


Ans: Struts is a open source framework used to develop Java based web page.Struts takes the help of Model View Controller(MVC) architecture. Here framework means"It is a reusable, "semi-complete"application that can be specialized to produce custom applications.


2) What is the need of Struts?


Ans: We need Struts in Java because of following reasons:



  1. It helps in creation and maintenance of the application
  2. Make use of Model View Controller(MVC) architecture. Where model is referring to business or database.View is referring to the page Design code and Controller is referring to navigational code
  3. Struts combines Java Servlets, Java ServerPages, custom tags, and message resources into a unified framework. The end result is a cooperative, synergistic platform, suitable for development teams, independent developers, and everyone in between.
3) What is the difference between Model2 and MVC models?

Ans: In model2, we have client tier as jsp, controller is servlet, and business logic is java bean. Controller and business logic beans are tightly coupled. And controller receives the UI tier parameters. But in MVC, Controller and business logic are loosely coupled and controller has nothing to do with the project/ business logic as such. Client tier parameters are automatically transmitted to the business logic bean, commonly called as ActionForm.

So Model2 is a project specific model and MVC is project independent

4) What is MVC?


Ans: Model View Controller(MVC) is a design pattern used to perform changes in the application. For more details about MVC click on the following link: MVC Architecture


5) What is Action Class?

Ans: The Action Class is part of the Model and is a wrapper around the business logic. The purpose of Action Class is to translate the HttpServletRequest to the business logic. To use the Action, we need to Subclass and overwrite the execute() method. In the Action Class all the database/business processing are done. It is advisable to perform all the database related stuffs in the Action Class. The ActionServlet (commad) passes the parameterized class to Action Form 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 as per the value of the returned ActionForward object.

Example:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BillingAdviceAction extends Action 
{
 public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception 
{
BillingAdviceVO bavo = new BillingAdviceVO();
BillingAdviceForm  baform = (BillingAdviceForm)form;
DAOFactory factory = new MySqlDAOFactory();
BillingAdviceDAO badao = new MySqlBillingAdviceDAO();    
ArrayList projects = badao.getProjects();
request.setAttribute("projects",projects);
return (mapping.findForward("success"));
}
}

6) What is Action Form?

Ans: 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

Example:


public class BillingAdviceForm extends ActionForm 
{
private String projectid;
private String projectname;   
 public String getProjectid() 
 return projectid;  
}
public void setProjectid(String projectid) 
{  
this.projectid = projectid;  
}    
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) 
{
ActionErrors errors = new ActionErrors();
if(getProjectName() == null || getProjectName().length() < 1)
{
errors.add(“name”, new ActionError(“error.name.required”));
}         
return errors;
}
public void reset(ActionMapping mapping, HttpServletRequest request)
{
this.roledesc = "";
this.currid      = "";
}
}

7)How to Configure Struts?


Ans: Before being able to use Struts, you must set up your JSP container so that it knows to map all appropriate requests with a certain file extension to the Struts action servlet. This is done in the web.xml file that is read when the JSP container starts. When the control is initialized, it reads a configuration file (struts-config.xml) that specifies action mappings for the application while it's possible to define multiple controllers in the web.xml file, one for each application should suffice

8) What is validate() and reset() methods? Explain?

Ans: 

Validate() method: It is used to validate the properties after they are explored by the application. validate method is called before FormBean is handled to Action. This method returns a collection of ActionError.

Syntax:
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)

reset(): This method is called by the Struts Framework with each request that uses the defined ActionForm. The main purpose of this method is to reset all the data from the ActionForm

Syntax:
public void reset()
{}



9)How struts validation is working?
Ans: Validator uses the XML file to pickup the validation rules to be applied to a form. In XML validation requirements are defined applied to a form. In case we need special validation rules not provided by the validator framework, we can plug in our own custom validations into Validator.

The Validator Framework uses two XML configuration files validator-rules.xml & validation.xml. The validator-rules.xml defines the standard validation routines; such as Required, Minimum Length, Maximum length, Date Validation, Email Address validation and more. 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.

10)What is Tiles?
Ans:  Tiles is a framework for the development user interface, Tiles is enables the developers to develop the web applications by assembling the reusable tiles.
1. Add the Tiles Tag Library Descriptor (TLD) file to the web.xml. 
2. Create layout JSPs. 
3. Develop the web pages using layouts. 


11)How you will handle errors & exceptions using Struts?
Ans: 
  • To handle "errors" server side validation can be used using ActionErrors classes can be used. 
  • The "exceptions" can be wrapped across different layers to show a user showable exception. 
  • Using validators
12)How you will save the data across different pages for a particular client request using Struts?

Ans: If the request has a Form object, the data may be passed on through the Form object across pages. Or within the Action class, call request.getSession and use session.setAttribute(), though that will persist through the life of the session until altered.
(Or) Create an appropriate instance of ActionForm that is form bean and store that form bean in session scope. So that it is available to all the pages that for a part of the request

13)What is the difference between ActionForm and DynaActionForm?

Ans:
 1. In struts 1.0, action form is used to populate the html tags in jsp using struts custom tag.when the java code changes, the change in action class is needed. To avoid the chages in struts 1.1 dyna action form is introduced.This can be used to develop using xml. The dyna action form bloats up with the struts-config.xml based definetion.
2. There is no need to write actionform class for the DynaActionForm and all the variables related to the actionform class will be specified in the struts-config.xml. Where as we have to create Actionform class with getter and setter methods which are to be populated from the form
3.if the formbean is a subclass of ActionForm, we can provide reset(),validate(),setters(to hold the values),gettters whereas if the formbean is a subclass to DynaActionForm we need not provide setters, getters but in struts-config.xml we have to configure the properties in using .basically this simplifies coding

DynaActionForm which allows you to configure the bean in the struts-config.xml file. We are going to use a subclass of DynaActionForm called a DynaValidatorForm which provides greater functionality when used with the validation framework.

<struts-config>
    <form-beans>
        <form-bean name="employeeForm" type="org.apache.struts.validator.DynaValidatorForm">
            <form-property name="name" type="java.lang.String"/>
            <form-property name="age" type="java.lang.String"/>
            <form-property name="department" type="java.lang.String" initial="2" />
            <form-property name="flavorIDs" type="java.lang.String[]"/>
            <form-property name="methodToCall" type="java.lang.String"/>
        </form-bean>
    </form-beans>
</struts-config>

This DynaValidatorForm is used just like a normal ActionForm when it comes to how we define its use in our action mappings. The only 'tricky' thing is that standard getter and setters are not made since the DynaActionForms are backed by a HashMap. In order to get fields out of the DynaActionForm you would do: 

String age = (String)formBean.get("age"); 
Similarly, if you need to define a property: 
formBean.set("age","30");

14)Can you write your own methods in Action class other than execute() and call the user method directly?
Ans:Yes, we can create any number of methods in Action class and instruct the action tag in struts-config.xml file to call that user methods.

Public class StudentAction extends DispatchAction
{
      public ActionForward read(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) throws Exception 
      { 
Return some thing;
      }
public ActionForward update(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) throws Exceptio 
      { 
Return some thing;
      }
If the user want to call any methods, he would do something in struts-config.xml file.
<action path="/somepage" 
      type="com.odccom.struts.action.StudentAction"
      name="StudentForm"
      <b>parameter=”methodToCall”</b>
      scope="request"
      validate="true">
<forward name="success" path="/Home.jsp" />
</action>

15)Can I have more than one struts-config.xml?
A) Yes you can I have more than one.
<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</param-value>
</init-param>
<! — module configurations -- > 
<init-param>
<param-name>config/exercise</param-name>
<param-value>/WEB-INF/exercise/struts-config.xml</param-value>
 </init-param>
  </servlet>

No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...