STRUTS Interview Questions



Q) What is the advantage of DispatchAction class?
A)
Using DispatchAction class you can overcome writing one Action class for one business entity. Instead you can write multiple business entities in one Action class and call the business  method that is required by the application.

Q) Can you write your own method in Action class other than execute() and call that user method directly?
A)
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 method. This is possible by using DispatchAction class. A sample code would look like this

public class StudentAction extends DispatchAction
{
public ActionForward create(ActionMapping mapping, ActionForm form,
HttpServletRequest req,
HttpServletResponse res) throws Exception
{
return something;
}
public ActionForward read(ActionMapping mapping, ActionForm form,
HttpServletRequest req,
HttpServletResponse res) throws Exception
{
return something;
}
public ActionForward update(ActionMapping mapping, ActionForm form,
HttpServletRequest req,
HttpServletResponse res) throws Exception
{
return something;
}

}
In the above Action class if the user wants to call any method, he would do something like this in my struts-config.xml file
<action path=”/somePage”
type=”StudentAction”
name=”studentForm”
<b>parameter=”methodToCall”</b>
scope=”request”
validate=”true”
input=”/student.jsp”>

<forward name=”success” path=”/success.jsp”/>
</action>
In this configuration file the value of the parameter methodToCall determines the method in the StudentAction class to be invoked. For example, the JSP from which you are calling this action, can set the methodToCall parameter to any of the methods it wants to.

Q) How you will enable front-end validation based on the xml in validation.xml?
A)
The <html:javascript> tag to allow front-end validation based on the xml in validation.xml. For example the code:
<html:javascript formName=”logonForm” dynamicJavascript=”true” staticJavascript=”true” /> generates the client side java script for the form “logonForm” as defined in the validation.xml file. The <html:javascript> when added in the jsp file generates the client site validation script.

Q) What happens when the user has not provided required information that is needed by the struts validator framework? Explain how struts handles to display the validation messages?
A)
When the user has not provided any required information, the validation messages are displayed to the user and this block of code does this job.

<logic:messagesPresent>
<bean:message key=”errors.header.login”/>
<ul>
<html:messages id=”error”>
<li><bean:write name=”error”/></li>
</html:messages>
</ul><hr />
</logic:messagesPresent>

Q) Brief on how struts validation is working  ?
A)
Struts uses 2 XML files to validate the user form. validator-rules.xml and validation.xml  files. The validator-rules.xml defines a set of standard validations and these validations  are used in validation.xml file.
Generally the rules defined in validator-rules.xml file are typical java class files that does the validation in the backend.

Q) Explain about ActionForm class??
A)
ActionForm class defines get and set methods for information provided by the client in the  HTML form. It has reset (for reseting the defined variables to its initital state) and  validate method for validating the user input.

Q) Explain about Action Class  ??
A)
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 (command) passes the  parameterize 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.

Q) Do you write an ActionForm class for every form that you need to process?
A)
No, I define DynaValidatorForm as this class takes care of defining get/set methods and doing the validation for me. And I need to define this class in struts-config.xml file. An e.g. would be

<form-beans>
<form-bean name=”categoryForm” type=”org.apache.struts.validator.DynaValidatorForm”>
<form-property name=”category” type=”java.lang.String”/>
</form-bean>

</form-beans>

Q) How would struts handle “messages” required for the application?
A)
Messages are defined in a .properties file as name value pairs. To make these messages available to the application, you need to place the .properties file in WEB-INF/classes  folder. And define the following tag in struts-config.xml file

<message-resources parameter=”ApplicationResources” />
And in order to display a message in a jsp you would use
<bean:message key=”title.mytitle”/>

Q) What is the name of the XML file which handles requests and gives responses? And acts as a controller?
A)

struts-config.xml file

Q) Explain about ActionServlet in Apache Struts  ??
A)
ActionServlet acts as a controller in Struts. There can be only one ActionServlet defined
for a context. ActionServlet is defined in web.xml file of a context e.g. WEB-INF/web.xml.
Can I have more than one struts-config.xml file?
Yes. A sample configuration in web.xml file would look like 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</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>
<init-param>
<param-name>config/upload</param-name>
<param-value>/WEB-INF/upload/struts-config.xml</param-value>
</init-param>
</servlet>

Q) What is Struts Validator Framework?
A)
Struts Framework provides the functionality to validate the form data. It can be use to validate the data on the users browser as well as on the server side. Struts Framework emits  the java scripts and it can be used to validate the form data on the client browser. Server side  validation of form can be accomplished by sub classing your From Bean with DynaValidatorForm class.

Q) What is the role of ActioMapping object in Struts Action class?
A)
An ActionMapping knows about the mapping of a particular request to an instance of a particular Action class.

Q) What happens internally when actionMappings.findForward(“target”) method is called in Action class. ?
A)
actionMappings.findForward(“target”) method returns ActionForward object. An ActionForward represents a destination to  which the controller, RequestProcessor, might be directed to perform a RequestDispatcher.forward or  HttpServletResponse.sendRedirect to, as a result of processing activities of an Action class.

Q) Expalin JSP,Servlet Combination VS Struts Which is better and how?
A)
Struts is a framework based on Model-View-Controller (MVC) design paradigm. The Model represents the business or database code, the View represents the page design code, and the Controller represents the navigational code. The Struts framework is designed to help developers create web applications that utilize a MVC architecture. Struts framework internally uses Servlet Technology for processing the request and response.
Sun Microsystems has provided the JSP / Servlet API specifications, and its the web server or application server vendor who implements the logic for the API.

Q) Explain ActionForward against JSP:forward  ?
A)
jsp:forward is part of JSTL and will be used in JSP pages. It internally uses RequestDispatcher to forward the request to new jsp or servlet page .
ActionForward is part of Struts framework and is used in Action class. It internally uses RequestDispatcher to forward the request to ActionServlet.

Random Posts

  • Difference between session.save() and session.saveOrUpdate()
  • Jsp Example: File Uploading
  • Define ServletOutputStream
  • How application creates a connection
  • Java RMI Interview questions

Post a Comment