struts2 frequently asked interview questions,Know all strust2 interview questions asked in interviews on interceptors,struts2 framework. Struts 2 interview questions and answers. Let us discuss struts 2 interview questions and answers for freshers & experienced. What is MVC? MVC stands for Model-View-Controller design pattern which separates the business logic, presentation logic and navigation logic. Where.
|Language:||English, Spanish, Japanese|
|Genre:||Business & Career|
|ePub File Size:||17.85 MB|
|PDF File Size:||8.85 MB|
|Distribution:||Free* [*Regsitration Required]|
Recently I have wrote a lot of Struts2 Tutorials and in this post, I am listing down some of the important Struts2 interview questions with answers to help you in. Dear readers, these Struts2 Interview Questions have been designed especially Struts2 is not just the next version of Struts 1, but it is a complete rewrite of the. Searching for Apache Struts 2 job? Need interview questions to prepare well for the job interview. Then you are at the right place. Apache Struts 2 jobs are.
How to design your resume? The values configured in this file will override the default values configured in default. What is interceptor in Struts2? Your blog is very informative. Published in: Results have optional name and type attributes. This interceptor is part of basicStack and defaultStack interceptors stack.
Interceptors are conceptually the same as servlet filters or the JDKs Proxy class. Interceptors allow for crosscutting functionality to be implemented separately from the action as well as the framework. Creating a custom interceptor is easy; the interface that needs to be extended is the Interceptor interface. How Interceptor Works In Struts 2?
Actual action will be executed using the interceptor by invocation. So you can do some pre-processing and some post-processing based on your requirement. The framework itself starts the process by making the first call to the ActionInvocation object's invoke. Each time invoke is called, ActionInvocation consults its state and executes whichever interceptor comes next.
When all of the configured interceptors have been invoked, the invoke method will cause the action itself to be executed.
The Action class manages the application's state, and the Result Type manages the view. What Is Defeult Result Type?
Default result type is dispatcher, which is used to dispatch to JSP pages. The dispatcher result type is the default type, and is used if no other result type is specified.
It uses the RequestDispatcher. The redirect result type calls the standard response. What Is Value Stack? For example the current iteration value for a collection being looped over in a JSP tag. OGNL also helps in data transfer and type conversion. File uploading in Struts is possible through a pre-defined interceptor called FileUpload interceptor which is available through the org. FileUploadInterceptor class and included as part of the defaultStack.
Default is M. By default is jakarta. By default is javax. What Is Structs 2 Validation Framework? At Struts's core, we have the validation framework that assists the application to run the rules to perform validation before the action method is executed. Action class should extend the ActionSupport class, in order to get the validate method executed.
How Struts 2 Validation Works? When the user presses the submit button, Struts 2 will automatically execute the validate method and if any of the if statements listed inside the method are true, Struts 2 will call its addFieldError method.
If any errors have been added then Struts 2 will not proceed to call the execute method. Rather the Struts 2 framework will return input as the result of calling the action. So when validation fails and Struts 2 returns input, the Struts 2 framework will redisplay the view file. Since we used Struts 2 form tags, Struts 2 will automatically add the error messages just above the form filed. These error messages are the ones we specified in the addFieldError method call.
The addFieldError method takes two arguments. The first is the form field name to which the error applies and the second is the error message to display above that form field. The second method of doing validation is by placing an xml file next to the action class. Struts2 XML based validation provides more options of validation like email validation, integer range validation, form validation field, expression validation, regex validation, required validation, requiredstring validation, stringlength validation and etc.
The xml file needs to be named '[action-class]'-validation. What Is Internationalization? Internationalization i18n is the process of planning and implementing products and services so that they can easily be adapted to specific local languages and cultures, a process called localization.
The internationalization process is sometimes called translation or localization enablement. How Struts2 Supports Internationalization? Struts2 provides localization ie.
Here you can skip country part which is optional. Struts makes the exception handling easy by the use of the "exception" interceptor. The "exception" interceptor is included as part of the default stack, so you don't have to do anything extra to configure it. It is available out-of-the-box ready for you to use.
A Results annotation is a collection of results. Under the Results annotation, we can have multiple Result annotations. The result annotations have the name that correspond to the outcome of the execute method.
They also contain a location as to which view should be served corresponding to return value from execute.
This is used to decorate the execute method. The Action method also takes in a value which is the URL on which the action is invoked. The After annotation marks a action method that needs to be called after the main action method and the result was executed. Return value is ignored. The Before annotation marks a action method that needs to be called before the main action method and the result was executed. The BeforeResult annotation marks a action method that needs to be executed before the result.
This validation annotation checks if there are any conversion errors for a field and applies them if they exist. If neither min nor max is set, nothing will be done. This validation annotation checks that a field is a valid e-mail address if it contains a non-empty String. This non-field level validator validates a supplied regular expression. This validation annotation checks that a numeric field has a value within a specified range.
This validation annotation checks that a String field is not empty i.
This validator checks that a String field is of the right length. It assumes that the field is a String. If neither minLength nor maxLength is set, nothing will be done. This validator checks that a field is a valid URL. If you want to use several annotations of the same type, these annotation must be nested within the Validations annotation.
This annotation can be used for custom validators. Use the ValidationParameter annotation to supply additional params. This is a marker annotation for type conversions at Type level. The Conversion annotation must be applied at Type level. This annotation sets the CreateIfNull for type conversion. The CreateIfNull annotation must be applied at field or method level.
This annotation sets the Element for type conversion. The Element annotation must be applied at field or method level. This annotation sets the Key for type conversion.
The Key annotation must be applied at field or method level. This annotation sets the KeyProperty for type conversion. The KeyProperty annotation must be applied at field or method level.
This annotation annotation is used for class and application wide conversion rules. The TypeConversion annotation can be applied at property and method level. What Are Struts2 Core Components? Struts2 core components are: Action Classes B. Interceptors C. I18nInterceptor interceptor is responsible for i18n support in Struts2 applications.
We can implement Action interface to create our action classes. This interface has a single method execute that we need to implement.
ActionSupport class is the default implementation of Action interface and it also implements interfaces related to Validation and i18n support. We can override validate method of ActionSupport class to include field level validation login in our action classes. Depending on the requirements, we can use any of the approaches to create struts 2 action classes, my favorite is ActionSupport class because it helps in writing validation and i18n logic easily in action classes.
However sometimes we need these access in action classes such as checking HTTP method or setting cookies in response. Struts2 provide different ways to create action classes.
Struts2 provides execAndWait interceptor for long running action classes. We can use this interceptor to return an intermediate response page to the client and once the processing is finished, final response is returned to the client.
This interceptor is defined in the struts-default package and implementation is present in ExecuteAndWaitInterceptor class. Struts2 Action classes are thread safe because an object is instantiated for every request to handle it. One of the major problems with web applications is the double form submission.
If not taken care, double form submission could result in charging double amount to customer or updating database values twice. We can use token interceptor to solve the double form submission problem. Earlier versions of Struts2 uses org. FilterDispatcher as Front Controller class. What Are Different Struts2 Tags? How Can We Use Them? Struts2 provides a lot of custom tags that we can use in result pages to create views for client request.
These tags are broadly divided into three categories- Data tags, Control tags and UI tags. Struts2 support OGNL expression language and it performs two important tasks in Struts 2 — data transfer and type conversion. OGNL is flexible and we can easily extend it to create our own custom converter class. Creating and configuring custom type converter class is very easy, first step is to fix the input format for the custom class. Second step is to implement the converter class.
Type converter classes should implement com. TypeConverter interface. Since in web application, we always get the request in form of String and send response in the form of String, Struts 2 API provides a default implementation of TypeConverter interface, StrutsTypeConverter. Some of the benefits of interceptors are: What Is Valuestack And Ognl?
ValueStack is the storage area where the application data is stored by Struts2 for processing the client requests. The data is stored in ActionContext objects that use ThreadLocal to have values specific to the particular request thread. As you can see in architecture diagram, both interceptors and result pages can access data stored on ValueStack using OGNL. Interceptor interface defines three methods — init , destroy and intercept. Interceptors are Singleton classes and Struts2 initialize a new thread to handle each request.
It is advisable to extend this package while configuring our application package to avoid configuring interceptors again.
This is provided to help developers by eliminating the trivial task of configuring interceptor and result pages in our application. Some of the important annotations introduced in Struts2 are: What Is An Interceptor Stack?
An interceptor stack helps us to group together multiple interceptors in a package for further use. We can create our own interceptor stack at the start of the package and then configure our action classes to use it.
The default URI suffix for Struts2 action is.
We can change this suffix by defining struts. Some of the Struts2 constants that I have used are: File Upload is one of the common task in a web application.
Thats why Struts2 provides built in support for file upload through FileUploadInterceptor. This interceptor is configured in struts-default package and provide options to set the maximum size of a file and file types that can be uploaded to the server. Struts2 namespace configuration allows us to create modules easily. We can use namespace to separate our action classes based on their functionality, for example admin, user, customer etc.
ParametersInterceptor interceptor is responsible for mapping request parameters to the Action class java bean properties.
Struts2 is based on OpenSymphony WebWork framework. The core components of Struts2 are Action, Interceptors and Result pages. Struts2 provides many ways to create Action classes and configure them via struts. We can create our own interceptors for common tasks. Struts2 comes with a lot of tags and uses OGNL expression language. We can create our own type converters to render result pages. Interceptors are the backbone of Struts2 Framework. Struts2 interceptors are responsible for most of the processing done by the framework, such as passing request params to action classes, making Servlet API request, response, session available to Action classes, validation, i18n support, etc.
ActionInvocation is responsible to incapsulate Action classes and interceptors and to fire them in order. The most important method for use in ActionInvocation is invoke method that keeps track of the interceptor chain and invokes the next interceptor or action.
Struts2 interceptors are based on intercepting filters design pattern. The invocation of interceptors in interceptor stack closely resembles Chain of Responsibility design pattern. Struts2 Action classes are thread safe because an object is instantiated for every request to handle it. ActionInvocation is responsible to encapsulate Action classes and interceptors and to fire them in order. Earlier versions of Struts2 uses org.
FilterDispatcher as Front Controller class. A ValueStack is a place where all the data related to action and the action itself is stored. The role of Action are to execute business logic or delegate call to business logic by the means of action methods which is mapped to request and contains business data to be used by the view layer by means of setters and getters inside the Action class and finally helps the framework decide which result to render.
ValueStack is the storage area where the application data is stored by Struts2 for processing the client requests. The data is stored in ActionContext objects that use ThreadLocal to have values specific to the particular request thread. As you can see in architecture diagram, both interceptors and result pages can access data stored on ValueStack using OGNL.
Struts2 namespace configuration allows us to create modules easily. We can use namespace to separate our action classes based on their functionality, for example admin, user, customer etc. Which interceptor is responsible for mapping request parameters to action class Java Bean properties? ParametersInterceptor interceptor is responsible for mapping request parameters to the Action class java bean properties.
This interceptor is part of basicStack and defaultStack interceptors stack. I18nInterceptor interceptor is responsible for i18n support in Struts2 applications. What is the difference in using Action interface and ActionSupport class for our action classes, which one you would prefer?
We can implement Action interface to create our action classes. This interface has a single method execute that we need to implement. ActionSupport class is the default implementation of Action interface and it also implements interfaces related to Validation and i18n support. We can override validate method of ActionSupport class to include field level validation login in our action classes. Depending on the requirements, we can use any of the approaches to create struts 2 action classes, my favorite is ActionSupport class because it helps in writing validation and i18n logic easily in action classes.
However sometimes we need these access in action classes such as checking HTTP method or setting cookies in response. Posted in Uncategorized. You are commenting using your WordPress. You are commenting using your Google account. You are commenting using your Twitter account. You are commenting using your Facebook account.
Notify me of new comments via email. This site uses Akismet to reduce spam. Learn how your comment data is processed. Posted by: What is Struts2?