Ans: Struts are nothing but open source framework mostly used for making web application whenever we use the term framework means it comprises JSP, servlet, custom tags message resources all in one bundle which makes developer task very easy. Its is based on MVC pattern which is model view Controller pattern.
if we go with servlet all HTML code which is related to design part mostly will come inside java code which makes code unmaintainable and complex similarly if use JSP, all java code related to business come inside design part which again make code complex, that’s why MVC pattern come into existence and which separate the business, design and controller and struts were made based on this pattern and easy to develop web application.
Ans:
1 Validator Framework
2 Tiles
Ans: Main classes in Struts Framework are:
Ans: ActionForm will maintains the session state for web application and the its object is automatically populated on the server side with data entered from a form on the client side
Ans: The Front Controller Pattern is a software design pattern listed in several pattern catalogs. The pattern relates to the design of web applications. It “provides a centralized entry point for handling requests.
Ans: This Struts interview question is pretty straight forward and I have put the differences in tabular format to make it easy to read.
Dispatch Action | LookupDispatchAction |
It’s a parent class of LookupDispatchAction | Subclass of Dispatch Action |
DispatchAction provides a mechanism for grouping a set of related functions into a single action, thus eliminating the need to create separate actions for each function. | An abstract Action that dispatches to the subclass mapped executes method. This is useful in cases where an HTML form has multiple submit buttons with the same name. The button name is specified by the parameter property of the corresponding ActionMapping. |
If not using Internalization functionality then dispatch action is more useful. | Lookup Dispatch Action is useful when we are using Internalization functionality |
DispatchAction selects the method to execute depending on the request parameter value which is configured in the XML file. | LookupDispatchAction looks into the resource bundle file and finds out the corresponding key name. We can map this key name to a method name by overriding the getKeyMethodMap() method. |
DispatchAction is not useful for I18N | LookupDispatchAction is used for I18N |
Ans: Struts Plugins are modular extensions to the Struts COntroller. They are defined by the org.apache.struts.action.Plugin interface.Struts Plugins are useful are useful when you are allocating resources or preparing connections to the databases or even JNDI resources. This interface defines two lifecycle mathods: init() and desstroy().
Ans: Subclass your Action class and Override execute() method.
Then in body of execute() method do this:
We can call EJB from struts by using the service locator design pattern or by Using initial context with create home object and getting return remote referenc object.
Ans: validate(): validate method is Used to validate properties after they have been populated, and this , method is Called before FormBean is passed to Action. Returns a collection of ActionError as ActionErrors. Following is the method signature for the validate() method.
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ( StringUtils.isNullOrEmpty(username) && StringUtils.isNullOrEmpty(password)){
errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("error.usernamepassword.required"));
}
return errors;
}
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.
Example :
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.password = null;
this.username = null;
}
Set null for every request.
Ans: The SwitchAction cl 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 cl can be used as is, without extending.
Ans:
Ans: 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.
Ans: ApplicationResources.properties and struts-config.xml these two files are used to between the Controller and the Model.
Ans: This is little tough Struts interview question though looks quite basic not every candidate knows about it. Below is my answer of this interview questions on Struts:
There are two ways of handling exception in Struts:
<exception
key="stockdataBase.error.invalidCurrencyType"
path="/AvailbleCurrency.jsp"
type="Stock.account.illegalCurrencyTypeException">
</exception>
The programmatic and Declarative way is sometimes also asked as follow-up questions are given candidate’s response on knowledge on Struts.
Key: The key represents the key present in MessageResource.properties file to describe the exception.
Type: The class of the exception occurred.
Path: The page where the controls are to be followed is case exception occurred
Ans: The problem of duplicate form submission arises when a user clicks the Submit button more than once before the response is sent back. This may result in inconsistent transactions and must be avoided. In Struts this problem can be handled by using the saveToken() and isTokenValid() methods of Action class. saveToken() method creates a token (a unique string) and saves that in the user’s current session, while isTokenValid() checks if the token stored in the user’s current session is the same as that was passed as the request parameter.
Use the Action Token methods to prevent duplicate submits
There are methods built into the Struts action to generate one-use tokens. A token is placed in the session when a form is populated and also into the HTML form as a hidden property. When the form is returned, the token is validated. If validation fails, then the form has already been submitted, and the user can be apprised.
saveToken(request)
on the return trip,
isTokenValid(request)
resetToken(request)
Ans: 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.
Ans: The Struts is not only thread-safe but thread-dependent(instantiates each Action once and allows other requests to be threaded through the original object.
Ans: Action, ActionForm, ActionServlet, ActionMapping, ActionForward are basic classes of Structs.
Ans:
--struts-html.tld
--struts-bean.tld
--struts-logic.tld