1:23 am - Wednesday January 25, 2017

Struts InterviewQuestions2

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

1.            How to handle Errors and Exceptions in struts.

Exceptions in Struts are handled in two ways:

Programmatic exception handling :

Explicit try/catch blocks in any code that can throw exception. It works well when custom value (i.e., of variable) needed when error occurs.

Declarative exception handling :You can either define <global-exceptions> handling tags in your struts-config.xml or define the exception handling tags within <action></action> tag. It works well when custom page needed when error occurs. This approach applies only to exceptions thrown by Actions.

<global-exceptions>

<exception key=”some.key”

type=”java.lang.NullPointerException”

path=”/WEB-INF/errors/null.jsp”/>

</global-exceptions>

or

<exception key=”some.key”

type=”package.SomeException”

path=”/WEB-INF/somepage.jsp”/>

2.            What are the core classes of Struts.

The Core classes of Struts Framework are

  • org.apache.struts.action.ActionForm
  • org.apache.struts.action.Action
  • org.apache.struts.action.ActionMapping
  • org.apache.struts.action.ActionForward
  • org.apache.struts.action.ActionServlet
  • org.apache.struts.action.ActionError
  • org.apache.struts.action.ActionErrors

 

The core classes of Struts are:

  •            Action,
  •           ActionForm,
  •              ActionServlet,
  •            ActionMapping,
  •           ActionForward, etc.

 

3.     What are the components of Struts.

1. Action Servlet

2. Action Classes

3. Action Form

4. PlugIn Classes

4.1.Validator Framework

4.2.Tiles

5. Message Resources

6.Action Mapping Implemention

7. Struts Configuration XML Files

8.Exception Handler

 

4.            What are config files in struts.

  •            Action mappings
  •              Global forwards
  •              Form beans
  •              Data sources
  •              Global exceptions
  •              Controller
  •             Message resources
  •             Plugins

5.            What is Dform.

A specialized subclass of ActionForm that allows the creation of form beans with dynamic sets of properties (configured in configuration file), without requiring the developer to create a Java class for each type of form bean.

6.            Difference between Actionform and dform.

•  An ActionForm represents an HTML form that the user interacts with over one or more pages. You will provide properties to hold the state of the form with getters and setters to access them. Whereas, using DynaActionForm there is no need of providing properties to hold the state. Instead these properties and their type are declared in the struts-config.xml

•  The DynaActionForm bloats up the Struts config file with the xml based definition. This gets annoying as the Struts Config file grow larger.

•  The DynaActionForm is not strongly typed as the ActionForm. This means there is no compile time checking for the form fields. Detecting them at runtime is painful and makes you go through redeployment.

•  ActionForm can be cleanly organized in packages as against the flat organization in the Struts Config file.

•  ActionForm were designed to act as a Firewall between HTTP and the Action classes, i.e. isolate and encapsulate the HTTP request parameters from direct use in Actions. With DynaActionForm, the property access is no different than using request.getParameter( .. ).

•  DynaActionForm construction at runtime requires a lot of Java Reflection (Introspection) machinery that can be avoided.

7.            What is the use of Dispatch Action.

The DispatchAction class is used to group related actions into one class. Using this class, you can have a method for each logical action compared than a single execute method. The DispatchAction dispatches to one of the logical actions represented by the methods. It picks a method to invoke based on an incoming request parameter. The value of the incoming parameter is the name of the method that the DispatchAction will invoke.

8.            What is Mapping Dispatch.

An abstract Action that dispatches to a public method that is named by the request parameter whose name is specified by the parameter property of the corresponding ActionMapping. This Action is useful for developers who prefer to combine many similar actions into a single Action class, in order to simplify their application design.

Struts MappingDispatch Action (org.apache.struts.actions.MappingDispatchAction) is one of the Built-in Actions provided along with the struts framework.

The org.apache.struts.actions.MappingDispatchAction class is a subclass oforg.apache.struts.actions.DispatchAction class. This class enables a user to collect related functions into a single action class. It  needs to create multiple independent actions for each function.

9.            What is Lookup Dispatch.

LookupDispatch is similar to ActionDispatch. It distinguishes the ActionDispatch that uses the request parameter€™s values which performs a reverse lookup from resource bundle which uses the value of parameters and match it to a method in the class.

10.          How to upload files?

Org.apache.struts.upload.Formfile is known to be foremost interface for the upload function. This represents the files which have been uploaded by the client. Struts application directly references this interface. This class helps you in uploading files to the database. Here we uploaded a Formfile.

 

Step 1.

Create a form bean


public class FileUploadForm extends ActionForm

{

private FormFile file;

public FormFile getFile() {

return file;

}

public void setFile(FormFile file) {

this.file = file;

}

}

Step 2.

 

In the struts-config.xml file add

<form-bean

name=”FileUploadForm”

type=”com.techfaq.form.FileUploadForm”/>

 Step 3. 

add action mapping entry in the struts-config.xml file:

<action

path=”/FileUploadAndSave”

type=”com.techfaq.action.FileUploadAndSaveAction”

name=”FileUploadForm”

scope=”request”

validate=”true”

input=”/pages/fileupload.jsp”>

<forward name=”success” path=”/jsp/success.jsp”/>

</action>

Step 4.

In the JSP


<html:form action="/FileUploadAndSave" method="post" enctype="multipart/form-data">

File Name

<html:file property="file"/>

<html:submit>Upload File</html:submit>

</html:form>

Step 5.

In the Action class write the code


public ActionForward execute(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response) throws Exception{

FileUploadForm myForm = (FileUploadForm)form;

// Process the FormFile

FormFile file = myForm.getFile();

String contentType = file.getContentType();

//Get the file name

String fileName = file.getFileName();

int fileSize = file.getFileSize();

byte[] fileData = file.getFileData();

//Get the servers upload directory real path name

String filePath = getServlet().getServletContext().getRealPath("/") +"uploadfile";

/* Save file on the server */

if(!fileName.equals("")){

System.out.println("Server path:" +filePath);

//Create file

File fileToCreate = new File(file, fileName);

//If file does not exists create file

if(!fileToCreate.exists()){

FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);

fileOutStream.write(file.getFileData());

fileOutStream.flush();

fileOutStream.close();

}

}

return mapping.findForward("success");

}

File will be oploaded to “uploadfile” directory og your server.

 

2 3 4

 

Filed in: Z-A Interview Questions

No comments yet.

Leave a Reply