This fourth dimension its Struts interview questions, After writing Spring interview questions few weeks dorsum I was thinking what to selection for my interview serial in addition to so I idea nearly whatsoever spider web framework, in addition to on that struts is my favorite. Struts are opened upwardly source framework used for spider web application. These Struts interview questions are based on my sense equally good equally collected past times friends in addition to colleague in addition to they are non exclusively expert for interview exercise but equally good shows a novel administration of learning for anyone who is non real familiar alongside struts. The best way to usage these interview questions does revise earlier going for whatsoever Struts interview or whatsoever Java or J2EE interview. I accept equally good provided answers to these struts interview questions but you lot tin sack equally good inquiry to a greater extent than on Google but these answers of struts are sufficient on interview perspective.
Struts Interview Questions Answer
Question 1: What is Struts? Why you lot accept used struts inwards your application or project.
Ans: This is the commencement interview questions anyone asks inwards Struts to acquire the interview rolling. Most usually asked during the less senior level. Struts are nada but opened upwardly source framework by in addition to large used for making spider web application whenever nosotros usage the term framework way it comprises JSP, servlet, custom tags message resources all inwards 1 parcel which makes developer chore real easy. Its is based on MVC pattern which is model stance Controller pattern.
Now why nosotros usage Struts? So top dog argue is if nosotros become alongside servlet all HTML code which is related to pattern part by in addition to large volition come upwardly within coffee code which makes code unmaintainable in addition to complex similarly if usage JSP, all coffee code related to line of piece of job organisation come upwardly within pattern part which in 1 lawsuit again brand code complex, that’s why MVC pattern come upwardly into existence in addition to which split upwardly the business, pattern in addition to controller in addition to struts were made based on this pattern in addition to tardily to prepare spider web application.
The keyword to respond this Struts interview questions is MVC pattern pattern, Front Controller Pattern in addition to amend catamenia administration which by in addition to large interviewer are looking to hear. You tin sack read to a greater extent than pattern pattern interview inquiry on my post service 10 Interview inquiry on Singleton Pattern inwards Java
The keyword to respond this Struts interview questions is MVC pattern pattern, Front Controller Pattern in addition to amend catamenia administration which by in addition to large interviewer are looking to hear. You tin sack read to a greater extent than pattern pattern interview inquiry on my post service 10 Interview inquiry on Singleton Pattern inwards Java
Question 2: What are the top dog classes which are used inwards struts application?
Ans 2: This is unopen to other beginner’s score Struts interview question which is used to depository fiscal establishment check how familiar candidate is alongside Struts framework in addition to API. Main classes inwards Struts Framework are:
Action servlet: it’s a backbone of spider web application it’s a controller shape responsible for treatment the entire request.
Action class: using Action classes all the line of piece of job organisation logic is developed us telephone yell upwardly model of the application also.
Action Form: it’s a coffee edible bean which represents our forms in addition to associated alongside activeness mapping. And it equally good maintains the session state its object is automatically populated on the server side alongside information entered from a shape on the customer side.
Action Mapping: using this shape nosotros do the mapping betwixt object in addition to Action.
ActionForward: this shape inwards Struts is used to forwards the effect from controller to destination.
Question 3: How exceptions are handled inwards Struts application?
Ans: This is fiddling tough Struts interview question though looks quite basic non every candidate knows nearly it. Below is my respond of this interview questions on Struts:
There are ii ways of treatment exception inwards Struts:
Programmatically handling: using endeavor {} grab block inwards code where an exception tin sack come upwardly in addition to catamenia of code is equally good decided past times programmer .its a normal coffee linguistic communication concept.
Declarative handling: There are ii ways in 1 lawsuit again either nosotros define <global-Exception> tag within struts-config.XML file
<exception
key="stockdataBase.error.invalidCurrencyType"
path="/AvailbleCurrency.jsp"
type="Stock.account.illegalCurrencyTypeException">
key="stockdataBase.error.invalidCurrencyType"
path="/AvailbleCurrency.jsp"
type="Stock.account.illegalCurrencyTypeException">
</exception>
The programmatic in addition to Declarative way is sometimes equally good asked equally follow-up questions are given candidate’s response on noesis on Struts.
Key: The key represents the key introduce inwards MessageResource.properties file to depict the exception.
Type: The shape of the exception occurred.
Path: The page where the controls are to live followed is representative exception occurred.
See Struts inwards Action for to a greater extent than details:
Path: The page where the controls are to live followed is representative exception occurred.
See Struts inwards Action for to a greater extent than details:
Question 4: How validation is performed inwards struts application?
Ans: Another classic Struts interview inquiry it’s higher on a score than previous interview questions because it’s related to of import validation concept on a spider web application. In struts validation is performed using validator framework, Validator Framework inwards Struts consist of ii XML configuration files.
1. validator-rules.xml file: which contains the default struts pluggable validator definitions. You tin sack add together novel validation rules past times adding an entry inwards this file. This was the master copy beauty of struts which makes it highly configurable.
2. Validation.xml files which comprise details regarding the validation routines that are applied to the unlike Form Beans.
These ii configuration files inwards Struts should live placed somewhere within the /WEB-INF folder of the application to proceed it prophylactic from the customer in addition to arrive available inwards Classpath.
<!-- 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>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
Now the side past times side measuring towards validation is created fault messages within the message resources holding file which are used past times validator framework.
Message resources Contain:
1. CurrencyConverterForm.fromCurrency = From Currency
2. CurrencyConverterForm.toCurrency=To currency
3. errors.required={0} is required.
Then validation rules are defined inwards validation.xml for the fields of shape on which nosotros wishing want validation
Form edible bean code that extend DynaValidatorForm
Eg; <form-beans>
<form-bean name="CurrencyConverterForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="fromCurrency" type="java.lang.double" />
<form-property name="toCurrecny" type="java.lang.double" />
</form-bean>
</form-beans>
Validation.xml file contains
<form-validation>
<formset>
<form name=" CurrencyConverterForm ">
<field property=" fromCurrency " depends="required">
<arg key=" CurrencyConverterForm. fromCurrency "/>
</field>
<field property=" toCurrecny " depends="required ">
<arg key=" CurrencyConverterForm.toCurrency "/>
</field>
</form>
</formset>
</form-validation>
To associate to a greater extent than than 1 validation dominion to the holding nosotros tin sack specify a comma-delimited listing of values. The commencement dominion inwards the listing volition live checked commencement in addition to so the side past times side dominion in addition to so on. Answer of this Struts questions gets flake longer but it’s of import to comport on these of import concepts to arrive useful.
Question 5: What is the Difference betwixt DispatchAction in addition to LookupDispatchAction inwards Struts Framework?
This Struts interview inquiry is pretty withdraw forwards in addition to I accept set the differences inwards tabular format to arrive tardily to read.
Dispatch Action | LookupDispatchAction |
It’s a bring upwardly shape of LookupDispatchAction | Subclass of Dispatch Action |
DispatchAction provides a machinery for grouping a laid of related functions into a unmarried action, thence eliminating the demand to create split upwardly actions for each function. | An abstract Action that dispatches to the subclass mapped executes method. This is useful inwards cases where an HTML shape has multiple submit buttons alongside the same name. The push clit cite is specified past times the parameter holding of the corresponding ActionMapping. |
If non using Internalization functionality so dispatch activeness is to a greater extent than useful. | Lookup Dispatch Action is useful when nosotros are using Internalization functionality |
DispatchAction selects the method to execute depending on the asking parameter value which is configured inwards the XML file. | LookupDispatchAction looks into the resources parcel file in addition to finds out the corresponding key name. We tin sack map this key cite to a method cite past times overriding the getKeyMethodMap() method. |
DispatchAction is non useful for I18N | LookupDispatchAction is used for I18N |
Question 6: How you lot tin sack recollect the value which is laid inwards the JSP Page inwards the representative of DynaActionForm?
Ans: DynaActionForm is a pop theme inwards Struts interview questions. DynaActionForm is a subclass of ActionForm that allows the creation of shape beans alongside dynamic sets of properties, without requiring the developer to create a Java shape for each type of shape bean. DynaActionForm eliminates the demand of FormBean shape in addition to instantly the shape edible bean definition tin sack live written into the struts-config.XML file. So, it makes the FormBean declarative in addition to this helps the programmer to cut the evolution time.
For Example, nosotros accept a CurrencyConverterForm in addition to nosotros don't wishing a coffee class.
CurrencyConverterForm has properties fromCurrency, toCurrency
in the struts-config.xml file, declare the shape edible bean
CurrencyConverterForm has properties fromCurrency, toCurrency
in the struts-config.xml file, declare the shape edible bean
<form-bean name=" CurrencyConverterForm "
type="org.apache.struts.action.DynaActionForm">
<form-property name=" fromCurrency " type="java.lang.String"/>
<form-property name=" toCurrency " type="java.lang. String "/>
</form-bean>
Add action mapping inwards the struts-config.xml file:
<action path="/convertCurrency" type="com.techfaq.action.ConvertCurrencyAction"
name=" CurrencyConverterForm "
scope="request"
validate="true"
input="/pages/ currencyConverterform.jsp">
<forward name="success" path="/jsp/success.jsp"/>
<forward name="failure" path="/jsp/error.jsp" />
</action>
In the Action class.
type="org.apache.struts.action.DynaActionForm">
<form-property name=" fromCurrency " type="java.lang.String"/>
<form-property name=" toCurrency " type="java.lang. String "/>
</form-bean>
Add action mapping inwards the struts-config.xml file:
<action path="/convertCurrency" type="com.techfaq.action.ConvertCurrencyAction"
name=" CurrencyConverterForm "
scope="request"
validate="true"
input="/pages/ currencyConverterform.jsp">
<forward name="success" path="/jsp/success.jsp"/>
<forward name="failure" path="/jsp/error.jsp" />
</action>
In the Action class.
public shape ConvertCurrencyAction extends Action
{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
DynaActionForm currencyConverterForm = (DynaActionForm)form;
// past times this way nosotros tin sack recollect the value which is laid inwards the JSP Page
String fromCurrency = (String) currencyConverterForm.get("fromCurrency ");
String toCurrency = (String) currencyConverterForm.get("toCurrency ");
return mapping.findForward("success");
}
}
}
In the JSP page
<html:text property=" fromCurrency " size="30" maxlength="30"/>
<html:text property=" toCurrency " size="30" maxlength="30"/>
Question 7: what the Validate () in addition to reset () method does?
Ans: This is 1 of my personal favorite Struts interview questions. validate(): validate method is Used to validate properties later on they accept been populated, in addition to this , method is Called earlier FormBean is passed to Action. Returns a collection of ActionError equally ActionErrors. Following is the method signature for the validate() method.
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = novel ActionErrors();
if ( StringUtils.isNullOrEmpty(username) && StringUtils.isNullOrEmpty(password)){
errors.add(ActionErrors.GLOBAL_ERROR, novel ActionError("error.usernamepassword.required"));
}
return errors;
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = novel ActionErrors();
if ( StringUtils.isNullOrEmpty(username) && StringUtils.isNullOrEmpty(password)){
errors.add(ActionErrors.GLOBAL_ERROR, novel ActionError("error.usernamepassword.required"));
}
return errors;
}
reset(): reset() method is called past times Struts Framework alongside each asking that uses the defined ActionForm. The purpose of this method is to reset all of the ActionForm's information members prior to the novel asking values beingness set.
Example :
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.password = null;
this.username = null;
}
Set zero for every request.
Example :
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.password = null;
this.username = null;
}
Set zero for every request.
Question 8: How you lot volition brand available whatsoever Message Resources Definitions file to the Struts Framework Environment?
Ans: Message Resources Definitions file are uncomplicated .properties files in addition to these files comprise the messages that tin sack live used inwards the struts project. Message Resources Definitions files tin sack live added to the struts-config.xml file through < message-resources / > tag. Example: < message-resources parameter= MessageResources / >
Message resources definition files tin sack available to the struts environs inwards ii ways
1. using web.xml as
<servlet>
<servlet-name>action<servlet-name>
servlet-class>org.apache.struts.action.ActionServlet<servlet-class>
<init-param>
<param-name>application<param-name>
<param-value>resource.Application<param-value>
</servlet>
2.
<message-resource key="myResorce" parameter="resource.Application" null="false">
1. using web.xml as
<servlet>
<servlet-name>action<servlet-name>
servlet-class>org.apache.struts.action.ActionServlet<servlet-class>
<init-param>
<param-name>application<param-name>
<param-value>resource.Application<param-value>
</servlet>
2.
<message-resource key="myResorce" parameter="resource.Application" null="false">
Question 9: What configuration files are used inwards Struts?
Ans: ApplicationResources.properties in addition to struts-config.xml these ii files are used to betwixt the Controller in addition to the Model.
Question 10: Explain Struts piece of job Flow?
Ans: Sometime this Struts interview questions asked equally commencement questions but I recall this instantly J . Here is the answer to this Struts interview questions
1) Influenza A virus subtype H5N1 asking comes inwards from a Java Server Page into the ActionServlet.
2) The ActionServlet having already read the struts-config.xml file knows which shape edible bean relates to this JSP, in addition to delegates piece of job to the validate method of that shape bean.
3) The shape edible bean performs the validate method to determine if all required fields accept been entered, in addition to performs whatever other types of land validations that demand to live performed.
4) If whatsoever required land has non been entered, or whatsoever land does non exceed validation, the shape edible bean generates ActionErrors, in addition to later on checking all fields returns dorsum to the ActionServlet.
5) The ActionServlet checks the ActionErrors that were returned from the shape beans validate method to determine if whatsoever errors accept occurred. If errors accept occurred, it returns to the originating JSP displaying the appropriate errors.
6) If no errors occurred inwards the validate method of the shape bean, the ActionServlet passes command to the appropriate Action class.
7) The Action shape performs whatsoever necessary line of piece of job organisation logic, in addition to so forwards to the side past times side appropriate activeness (probably unopen to other JSP).
That’s all on Struts interview inquiry answers, for now, in that location are lots many interview questions on Struts which I accept non covered which you lot guys tin sack contribute in addition to I volition include it here. If you lot are looking to uncovering the respond to whatsoever inquiry asked on Struts interview then delight set it hither in addition to I volition endeavor to uncovering an respond to those questions.
Further Learning
Java Web Fundamentals By Kevin Jones
Struts 2 Framework for Beginners
Spring Framework Master Class - Beginner to Expert