Friday 27 April 2018

Strut Interview Questions

1. What are the components of Struts Framework?


Struts framework is comprised of following components:
Java Servlets
JSP (Java Server Pages)
Custom Tags
Message Resources

2. What’s the role of a handler in MVC based applications?


It’s the job of handlers to transfer the requests to appropriate models as they are bound to the model layer of MVC architecture. Handlers use mapping information from configuration files for request transfer.

3. What’s the flow of requests in Struts based applications?


Struts based applications use MVC design pattern. The flow of requests is as follows:
User interacts with View by clicking any link or by submitting any form.
Upon user’s interaction, the request is passed towards the controller.
Controller is responsible for passing the request to appropriate action.
Action is responsible for calling a function in Model which has all business logic implemented.
Response from the model layer is received back by the action which then passes it towards the view where user is able to see the response.

4. Which file is used by controller to get mapping information for request routing?


Controller uses a configuration file “struts-config.xml file to get all mapping information to decide which action to use for routing of user’s request.

5. What’s the role of Action Class in Struts?


In Struts, Action Class acts as a controller and performs following key tasks:
After receiving user request, it processes the user’s request.
Uses appropriate model and pulls data from model (if required).
Selects proper view to show the response to the user.

6. How an actionForm bean is created?

Surrogate

actionForm bean is created by extending the class org.apache.struts.action.ActionForm
In the following example we have created an actionForm bean with the name 'testForm':

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class testForm extends ActionForm {
private String Id=null;
private String State=null;
public void setId(String id){
this.Id=id;
}
public String getId(){
return this.Id;
}
public void setState(String state){
this.State=state;
}
public String getState(){
return this.State;
}

7. What are the two types of validations supported by Validator FrameWork?


Validator Framework is used for form data validation. This framework provides two types of validations:

Client Side validation on user’s browser

Server side validation

8. What are the steps of Struts Installation?


In order to use Struts framework, we only need to add Struts.Jar file in our development environment. Once jar file is available in the CLASSPATH, we can use the framework and develop Strut based applications.

9. How client side validation is enabled on a JSP form?


In order to enable client side validation in Struts, first we need to enable validator plug-in in struts-config.xml file. This is done by adding following configuration entries in this file:
<!--  Validator plugin -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

Then Validation rules are defined in validation.xml file. If a form contains email field and we want to enable client side validation for this field, following code is added in validation.xml file:
<form name="testForm">
<field  property="email"
depends="required">
<arg key="testForm.email"/>
</field>
</form>

10. How action-mapping tag is used for request forwarding in Struts configuration file?


In Struts configuration file (struts-config.xml), forwarding options are defined under action-mapping tag.
In the following example, when a user will click on the hyperlink test.do, request will be forwarded to /pages/testing.jsp using following configurations from struts-config.xml file:
 <action  path="/test" forward="/pages/testing.jsp">

This forwarding will take place when user will click on following hyperlink on the jsp page:
 <html:link</strong> page="/test.do</strong>">Controller Example</html:link>

11. How duplicate form submission can be controlled in Struts?

In Struts, action class provides two important methods which can be used to avoid duplicate form submissions.
saveToken() method of action class generates a unique token and saves it in the user’s session. isTokenValid() method is used then used to check uniqueness of tokens.

12. In Struts, how can we access Java beans and their properties?

Bean Tag Library is a Struts library which can be used for accessing Java beans.

13. Which configuration file is used for storing JSP configuration information in Struts?

For JSP configuration details, Web.xml file is used.

14. 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.
In the following example, execute method will return an object of actionForward defined in struts-config.xml with the name “exampleAction”:
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 actionExample extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
return mapping.findForward("exampleAction");
}
}

15. What’s the difference between validation.xml and validator-rules.xml files in Struts Validation framework?

In Validation.xml, we define validation rules for any specific Java bean while in validator-rules.xml file, standard and generic validation rules are defined.

No comments:

Post a Comment