1. How you will enable front-end validation based on the xml in validation.xml?
Ans: 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.
2. What are Tag Libraries provided with Struts?
Ans: Struts provides a number of tag libraries that helps to create view components easily. These tag libraries are:
a) Bean Tags: Bean Tags are used to access the beans and their properties.
b) HTML Tags: HTML Tags provides tags for creating the view components like forms, buttons, etc..
c) Logic Tags: Logic Tags provides presentation logics that eliminate the need for scriptlets.
d) Nested Tags: Nested Tags helps to work with the nested context.
3. What are the core classes of the Struts Framework?
Ans: Core classes of Struts Framework are ActionForm, Action, ActionMapping, ActionForward, ActionServlet etc.
4. How you will handle errors and exceptions using Struts?
Ans:Struts provides ActionError object. Store each error msg by converting them to ActionErrors obj. Then save this obj in the request by calling saveError method of the ActionServlet.In the JSP use tag to show the error msg.
5. What is the purpose of tiles-def.xml file, resourcebundle.properties file, validation.xml file?
Ans:The Tiles Framework is an advanced version of that comes bundled with the Struts Webapp framework. Its purpose is reduce the duplication between jsp pages as well as make layouts flexible and easy to maintain. It integrates with Struts using the concept of named views or definitions.
resourcebundle.properties is used to maintian all the strings that are used in the application and thier corresponding equalents in different desired languages. All the strings/labels in the application using Struts will get it labels from this file only depending on locale. This is an i18n implementation of struts.
This validation.xml configuration file defines which validation routines that is used to validate Form Beans. You can define validation logic for any number of Form Beans in this configuration file. Inside that definition, you specify the validations you want to apply to the Form Bean's fields. The definitions in this file use the logical names of Form Beans from the struts-config.xml file along with the logical names of validation routines from the validator-rules.xml file to tie the two together.
6.Explain Struts navigation flow?
Ans: A client requests a path that matches the Action URI pattern.The container passes the request to the ActionServlet.If this is a modular application, the ActionServlet selects the appropriate module.The ActionServlet looks up the mapping for the path.If the mapping specifies a form bean, the ActionServlet sees if there is one already or creates one.If a form bean is in play, the ActionServlet resets and populates it from the HTTP request.If the mapping has the validate property set to true, it calls validate on the form bean.If it fails, the servlet forwards to the path specified by the input property and this control flow ends.If the mapping specifies an Action type, it is reused if it already exists or instantiated.The Action’s perform or execute method is called and passed the instantiated form bean (or null).The Action may populate the form bean, call business objects, and do whatever else is needed. The Action returns an ActionForward to the ActionServlet.If the ActionForward is to another Action URI, we begin again; otherwise, it’s off to a display page or some other resource. Most often, it is a JSP, in which case Jasper, or the equivalent (not Struts), renders the page.
7. Why is the action class is singleton in nature? Isn't this creates a bottleneck for the requests?
Ans:No it won't create a bottleneck as each request will be processed in separate thread. If would have created bottleneck it create separate instance for each request.
We just have to make sure that we are not using any instance variable in execute method.
8. What is the request processor in struts? How it works?
Ans:The RequestProcessor Class is the actual place where the request processing takes place in a Struts controller environment.When the request object first reaches the actionservlet class then it invokes the process method of the underlying RequestProcessor Class.This process method then looks into the struts-config.xml file and tries to locate the name of the action that has come with the request.Once it identifies the action in the xml file it continues the rest of the steps needed for request processing(for details on this pls go thru Wrox).
IN addition to whatever said in comment one, in Jakarta Struts request processor has most of the following responsibilities:
1. Determine path
2. Handle Locale
3. Process content and encoding type
4. Process cache headers
5. Pre Processing hook
6. Determine mapping
7. Determine roles,
8. Process and validate actionForm
9. Return a response
It is one instance per application module; it invokes proper Action instance. Of course processes all requests for a module.
9. Give the Details of XML files used in Validator Framework?
Ans:The Validator Framework uses two XML configuration files validator-rules.xml and validation.xml. The validation-rules.xml is provided with the Validator Framework. The vlaidation-rules.xml is used to declares and assigns the logical names to the validation routines. It also contains the client-side java-script code for each validation routine. The validation routines are java methods plugged into the system to perform specific validations.
The Validator plug-in is supplied with a predefined set of commonly used validation rules such as Required, Minimum Length, Maximum length, Date Validation, Email Address validation and more. This basic set of rules can also be extended with custom validators if required.
The validation.xml configuration file defines which validation routines that is used to validate Form Beans. You can define validation logic for any number of Form Beans in this configuration file. Inside that definition, you specify the validations you want to apply to the Form Bean's fields. The definitions in this file use the logical names of Form Beans from the struts-config.xml file along with the logical names of validation routines from the validator-rules.xml file to tie the two together.
Example of form in the validation.xml file:
<!-- An example form -->
<form name="logonForm">
<field property="username" depends="required">
<arg key="logonForm.username"/>
</field>
<field property="password" depends="required,mask">
<arg key="logonForm.password"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9a-zA-Z]*
10. What is LookupDispatchAction?
Ans: LookupDispatchAction is an abstract Action that dispatches to the subclass mapped execute 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.
11: What do you understand by DispatchAction?
Ans:DispatchAction is an action that comes with Struts 1.1 or later, that lets you combine Struts actions into one class, each with their own method. The org.apache.struts.action.DispatchAction class allows multiple operation to mapped to the different functions in the same Action class.
12. How to handle duplicate submits in struts?
Ans: The duplicate form submission occurs
-When a user clicks the Submit button more than once before the response is sent back or
- When a client accesses a view by returning to a previously bookmarked page.
It 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.
It can be done by loading JSP through an Action and before loading the JSP call saveToken() to save the token in the user session. When the form is submitted, check the token against that in the session by calling isTokenValid().
public ActionForward submitOrder(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
try
{
// check the token. Proceed only if token is valid
if(isTokenValid(request,true)) {
//implement order submit functionality here
} else {
return mapping.findForward("failure");
}
}
catch(Exception ex){//exception}
}
13. What is the life cycle of ActionForm?
Ans: The lifecycle of ActionForm invoked by the RequestProcessor is as follows:
14. In which method of Action class the business logic is executed ?
Ans: 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 ;
15.What is pagination technique? How can you design them in struts?
Ans:Pagination is the technique where the bulk of results are split into different pages and only the information where the user can conveniently see are displayed in a page. (Like in Gooogle). This can be achieved in many ways, but the simplest method is to have a query string (say http://www.testwebsite?pageNumber=2) would lead to information corresponding to resultset rows
from 11 to 20. Assuming that you want to display 10 related rows of information, you can set the formula as follows:
Starting row = (pageNumber-1) * + 1 which is equal to 11.
Ending row = Starting row + which is equal to 20.
16.How can you use Validator framework in struts?
Ans:Validator Frameworks are helpful when the application needs server-side validation such that the particular set of validations occur very frequently within the same application. This avoids writing complex code in validation() method in every form bean. Using validator framework, there are different pre-written validations in place. You can customize these validations in XML file.
17.What are client-side and server-side vaidations?
Ans:Client-side validations: These are the validations that id done using javascripts. There is always a danger involved that the user can get through (crack-through) these validations. But for some simple validations, like converting lower-case to upper-case or date validations can be done, you can use javascripts.
Server-side validations: These are the validations done in server-side using Java components (Form bean or in business logics) where the user has no chance to crookedly get through the system.
18.What are the advantages of having multiple struts-config in the same application?
Ans:The implementation with many struts-config is to organize the development work, so that many people may be involved and it is some organized way of doing things. But this would result in some compromise in performance(speed). Technically there is no any difference between single and multiple struts-config files.
19.What is ActionMapping in struts?
Ans:Action mapping defines the flow of one request. The possible sequence is
User -> request -> Form -> Validation -> Business Code -> Forward -> JSP -> response -> User.
The components involved are Action classes, Forms and JSP.
20.Difference between Struts 1.0 and 1.1
Ans:
21.Define the terms authentication and authorisation.
Ans:Authentication is the process/rule that validates if the user is allowed to access the system or not.
Authorization is the process/rule that validates if the user is allowed to access the particular part of the system or not. This occurs after user's successful authentication.
22.If I give you an assignment to design Shopping cart web application, how will you define the architecture of this application. You are free to choose any framework, tool or server?
Ans: Usually I will choose a MVC framework which will make me use other design patterns like Front Controller, Business Delegate, Service Locater, DAO, DTO, Loose Coupling etc. Struts 2 is very easy to configure and comes with other plugins like Tiles, Velocity and Validator etc. The architecture of Struts becomes the architecture of my application with various actions and corresponding JSP pages in place.
Ans: 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.
2. What are Tag Libraries provided with Struts?
Ans: Struts provides a number of tag libraries that helps to create view components easily. These tag libraries are:
a) Bean Tags: Bean Tags are used to access the beans and their properties.
b) HTML Tags: HTML Tags provides tags for creating the view components like forms, buttons, etc..
c) Logic Tags: Logic Tags provides presentation logics that eliminate the need for scriptlets.
d) Nested Tags: Nested Tags helps to work with the nested context.
3. What are the core classes of the Struts Framework?
Ans: Core classes of Struts Framework are ActionForm, Action, ActionMapping, ActionForward, ActionServlet etc.
4. How you will handle errors and exceptions using Struts?
Ans:Struts provides ActionError object. Store each error msg by converting them to ActionErrors obj. Then save this obj in the request by calling saveError method of the ActionServlet.In the JSP use tag to show the error msg.
5. What is the purpose of tiles-def.xml file, resourcebundle.properties file, validation.xml file?
Ans:The Tiles Framework is an advanced version of that comes bundled with the Struts Webapp framework. Its purpose is reduce the duplication between jsp pages as well as make layouts flexible and easy to maintain. It integrates with Struts using the concept of named views or definitions.
resourcebundle.properties is used to maintian all the strings that are used in the application and thier corresponding equalents in different desired languages. All the strings/labels in the application using Struts will get it labels from this file only depending on locale. This is an i18n implementation of struts.
This validation.xml configuration file defines which validation routines that is used to validate Form Beans. You can define validation logic for any number of Form Beans in this configuration file. Inside that definition, you specify the validations you want to apply to the Form Bean's fields. The definitions in this file use the logical names of Form Beans from the struts-config.xml file along with the logical names of validation routines from the validator-rules.xml file to tie the two together.
6.Explain Struts navigation flow?
Ans: A client requests a path that matches the Action URI pattern.The container passes the request to the ActionServlet.If this is a modular application, the ActionServlet selects the appropriate module.The ActionServlet looks up the mapping for the path.If the mapping specifies a form bean, the ActionServlet sees if there is one already or creates one.If a form bean is in play, the ActionServlet resets and populates it from the HTTP request.If the mapping has the validate property set to true, it calls validate on the form bean.If it fails, the servlet forwards to the path specified by the input property and this control flow ends.If the mapping specifies an Action type, it is reused if it already exists or instantiated.The Action’s perform or execute method is called and passed the instantiated form bean (or null).The Action may populate the form bean, call business objects, and do whatever else is needed. The Action returns an ActionForward to the ActionServlet.If the ActionForward is to another Action URI, we begin again; otherwise, it’s off to a display page or some other resource. Most often, it is a JSP, in which case Jasper, or the equivalent (not Struts), renders the page.
7. Why is the action class is singleton in nature? Isn't this creates a bottleneck for the requests?
Ans:No it won't create a bottleneck as each request will be processed in separate thread. If would have created bottleneck it create separate instance for each request.
We just have to make sure that we are not using any instance variable in execute method.
8. What is the request processor in struts? How it works?
Ans:The RequestProcessor Class is the actual place where the request processing takes place in a Struts controller environment.When the request object first reaches the actionservlet class then it invokes the process method of the underlying RequestProcessor Class.This process method then looks into the struts-config.xml file and tries to locate the name of the action that has come with the request.Once it identifies the action in the xml file it continues the rest of the steps needed for request processing(for details on this pls go thru Wrox).
IN addition to whatever said in comment one, in Jakarta Struts request processor has most of the following responsibilities:
1. Determine path
2. Handle Locale
3. Process content and encoding type
4. Process cache headers
5. Pre Processing hook
6. Determine mapping
7. Determine roles,
8. Process and validate actionForm
9. Return a response
It is one instance per application module; it invokes proper Action instance. Of course processes all requests for a module.
9. Give the Details of XML files used in Validator Framework?
Ans:The Validator Framework uses two XML configuration files validator-rules.xml and validation.xml. The validation-rules.xml is provided with the Validator Framework. The vlaidation-rules.xml is used to declares and assigns the logical names to the validation routines. It also contains the client-side java-script code for each validation routine. The validation routines are java methods plugged into the system to perform specific validations.
The Validator plug-in is supplied with a predefined set of commonly used validation rules such as Required, Minimum Length, Maximum length, Date Validation, Email Address validation and more. This basic set of rules can also be extended with custom validators if required.
The validation.xml configuration file defines which validation routines that is used to validate Form Beans. You can define validation logic for any number of Form Beans in this configuration file. Inside that definition, you specify the validations you want to apply to the Form Bean's fields. The definitions in this file use the logical names of Form Beans from the struts-config.xml file along with the logical names of validation routines from the validator-rules.xml file to tie the two together.
Example of form in the validation.xml file:
<!-- An example form -->
<form name="logonForm">
<field property="username" depends="required">
<arg key="logonForm.username"/>
</field>
<field property="password" depends="required,mask">
<arg key="logonForm.password"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9a-zA-Z]*
10. What is LookupDispatchAction?
Ans: LookupDispatchAction is an abstract Action that dispatches to the subclass mapped execute 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.
11: What do you understand by DispatchAction?
Ans:DispatchAction is an action that comes with Struts 1.1 or later, that lets you combine Struts actions into one class, each with their own method. The org.apache.struts.action.DispatchAction class allows multiple operation to mapped to the different functions in the same Action class.
12. How to handle duplicate submits in struts?
Ans: The duplicate form submission occurs
-When a user clicks the Submit button more than once before the response is sent back or
- When a client accesses a view by returning to a previously bookmarked page.
It 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.
It can be done by loading JSP through an Action and before loading the JSP call saveToken() to save the token in the user session. When the form is submitted, check the token against that in the session by calling isTokenValid().
public ActionForward submitOrder(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
try
{
// check the token. Proceed only if token is valid
if(isTokenValid(request,true)) {
//implement order submit functionality here
} else {
return mapping.findForward("failure");
}
}
catch(Exception ex){//exception}
}
13. What is the life cycle of ActionForm?
Ans: The lifecycle of ActionForm invoked by the RequestProcessor is as follows:
- Retrieve or Create Form Bean associated with Action
- "Store" FormBean in appropriate scope (request or session)
- Reset the properties of the FormBean
- Populate the properties of the FormBean
- Validate the properties of the FormBean
- Pass FormBean to Action
14. In which method of Action class the business logic is executed ?
Ans: 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.
15.What is pagination technique? How can you design them in struts?
Ans:Pagination is the technique where the bulk of results are split into different pages and only the information where the user can conveniently see are displayed in a page. (Like in Gooogle). This can be achieved in many ways, but the simplest method is to have a query string (say http://www.testwebsite?pageNumber=2) would lead to information corresponding to resultset rows
from 11 to 20. Assuming that you want to display 10 related rows of information, you can set the formula as follows:
Starting row = (pageNumber-1) * + 1 which is equal to 11.
Ending row = Starting row + which is equal to 20.
16.How can you use Validator framework in struts?
Ans:Validator Frameworks are helpful when the application needs server-side validation such that the particular set of validations occur very frequently within the same application. This avoids writing complex code in validation() method in every form bean. Using validator framework, there are different pre-written validations in place. You can customize these validations in XML file.
17.What are client-side and server-side vaidations?
Ans:Client-side validations: These are the validations that id done using javascripts. There is always a danger involved that the user can get through (crack-through) these validations. But for some simple validations, like converting lower-case to upper-case or date validations can be done, you can use javascripts.
Server-side validations: These are the validations done in server-side using Java components (Form bean or in business logics) where the user has no chance to crookedly get through the system.
18.What are the advantages of having multiple struts-config in the same application?
Ans:The implementation with many struts-config is to organize the development work, so that many people may be involved and it is some organized way of doing things. But this would result in some compromise in performance(speed). Technically there is no any difference between single and multiple struts-config files.
19.What is ActionMapping in struts?
Ans:Action mapping defines the flow of one request. The possible sequence is
User -> request -> Form -> Validation -> Business Code -> Forward -> JSP -> response -> User.
The components involved are Action classes, Forms and JSP.
20.Difference between Struts 1.0 and 1.1
Ans:
- Perform() method was replaced by execute()
- DynaActionForms are introduced.
- Tiles Concept is introduced
- We can write our own Controller by Inheriting RequestProcessor
21.Define the terms authentication and authorisation.
Ans:Authentication is the process/rule that validates if the user is allowed to access the system or not.
Authorization is the process/rule that validates if the user is allowed to access the particular part of the system or not. This occurs after user's successful authentication.
22.If I give you an assignment to design Shopping cart web application, how will you define the architecture of this application. You are free to choose any framework, tool or server?
Ans: Usually I will choose a MVC framework which will make me use other design patterns like Front Controller, Business Delegate, Service Locater, DAO, DTO, Loose Coupling etc. Struts 2 is very easy to configure and comes with other plugins like Tiles, Velocity and Validator etc. The architecture of Struts becomes the architecture of my application with various actions and corresponding JSP pages in place.