Struts

  Home  Java Programing  Struts


“Struts Interview Questions and Answers will guide us now that Apache Struts is an open-source web application framework for developing Java EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt a model-view-controller (MVC) architecture. So learn Struts or get preparation for the job of Apache Struts by this Apache Struts Interview Questions with Answers guide”



41 Struts Questions And Answers

21⟩ How you will enable front-end validation based on the xml in validation.xml?

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.

 182 views

22⟩ What design patterns are used in Struts?

Struts is based on model 2 MVC (Model-View-Controller) architecture. Struts controller uses the command design pattern and the action classes use the adapter design pattern. The process() method of the RequestProcessor uses the template method design pattern. Struts also implement the following J2EE design patterns.

► Service to Worker

► Dispatcher View

► Composite View (Struts Tiles)

► Front Controller

► View Helper

► Synchronizer Token

 184 views

23⟩ Give the Details of XML files used in Validator Framework?

The Validator Framework uses two XML configuration files validator-rules.xml and validation.xml. The validator-rules.xml defines the standard validation routines, 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.

 183 views

25⟩ Can we have more than one struts-config.xml file for a single Struts application?

Yes, we can have more than one struts-config.xml for a single Struts application. They can be configured as follows:

<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-admin.xml,

/WEB-INF/struts-config-forms.xml

</param-value>

</init-param>

.....

<servlet>

 204 views

26⟩ How is the Action Mapping specified?

We can specify the action mapping in the configuration file called struts-config.xml. Struts framework creates ActionMapping object from <ActionMapping> configuration element of struts-config.xml file

<action-mappings>

<action path="/submit"

type="submit.SubmitAction"

name="submitForm"

input="/submit.jsp"

scope="request"

validate="true">

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

<forward name="failure" path="/error.jsp"/>

</action>

</action-mappings>

 212 views

27⟩ In which method of Action class the business logic is executed?

In the execute() method of Action class the business logic is executed.

public ActionForward execute(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response)

throws Exception ;

execute() method of Action class:

► Perform the processing required to deal with this request

► Update the server-side objects (Scope variables) that will be used to create the next page of the user interface

► Return an appropriate ActionForward object

 199 views

28⟩ What is ActionMapping?

Action mapping contains all the deployment information for a particular Action bean. This class is to determine where the results of the Action will be sent once its processing is complete.

 197 views

29⟩ Describe validate() and reset() methods?

validate() : Used to validate properties after they have been populated; Called before FormBean is handed to Action. Returns a collection of ActionError as ActionErrors. Following is the method signature for the validate() method.

reset(): reset() method is called by Struts Framework with each request that uses the defined ActionForm. The purpose of this method is to reset all of the ActionForm's data members prior to the new request values being set.

 174 views

31⟩ What is role of Action Class?

An Action Class performs a role of an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request.

 188 views

32⟩ How the exceptions are handled 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"/>

 172 views

33⟩ What is SwitchAction?

The SwitchAction class provides a means to switch from a resource in one module to another resource in a different module. SwitchAction is useful only if you have multiple modules in your Struts application. The SwitchAction class can be used as is, without extending.

 182 views

35⟩ What is LookupDispatchAction?

The LookupDispatchAction is a subclass of DispatchAction. It does a reverse lookup on the resource bundle to get the key and then gets the method whose name is associated with the key into the Resource Bundle.

 176 views

37⟩ What is DynaActionForm?

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.

 166 views

38⟩ What is the use of LookupDispatchAction?

LookupDispatchAction is useful if the method name in the Action is not driven by its name in the front end, but by the Locale independent key into the resource bundle. Since the key is always the same, the LookupDispatchAction shields your application from the side effects of I18N.

 180 views

39⟩ What is role of ActionServlet?

ActionServlet performs the role of Controller:

► Process user requests

► Determine what the user is trying to achieve according to the request

► Pull data from the model (if necessary) to be given to the appropriate view,

► Select the proper view to respond to the user

► Delegates most of this grunt work to Action classes

► Is responsible for initialization and clean-up of resources

 170 views