Struts

1. What is Struts ?

Struts is open source software used to develop java based web page.

Struts uses Jakarta Packages, Java Servlets, JavaBeans, ResourceBundles, and XML.

Struts takes the help of Model View Controller (MVC) architecture. Where Model is referring to business or database, View is referring to the Page Design Code, and Controller is referring to navigational code.

2. Why use struts ?

Struts is robust architecture and can be used for the development of application of any size. Using struts you can easy design any size of application easily.

3. What is Action Class ?

An Action class in the struts application is used to handle the request.

It acts as interface or communication medium between the HTTP request coming to it and business logic used to develop the application.

Action class consists of RequestProcessor which act as controller. This controller will choose the best action for each incoming request, generate the instance of that action and execute that action.

This should be in thread-safe manner, because RequestProcessor uses the same instance for numbar of requests at same time.

4. What are the different kinds of actions in Struts ?

  • Forward Action
  • Include Action
  • Dispatch Action
  • LookupDispatch Action
  • Switch Action

5. What are the core classes of the struts Framework ?

Struts is a set of cooperating classes, servlets, and JSP tags that make up a reusable MVC 2 design.

6. What is Struts Validator Framework ?

Struts Validator Framework enables us to validate the data of both client side and server side.

When some data validation is not present in the Validator framework, then programmer can generate own validation logic, this User Defined Validation logic can be bind with Validation Framework.

Validation Framework consist of two XML configuration Files:
1. Validator-Rules.xml file
2. Validation.xml file

7. What is ActionServlet ?

ActionServlet is a simple servlet which is the backbone of all Struts applications. It is the main Controller component that handles client requests and determines which Action will process each received request.

8. What are the classes used in Struts ?

Action Servlets: Used to control the response for each incoming request.

Action Class: Used to handle the request.

Action Form: It is java bean, used to referred to forms and associated with action mapping.

Action Mapping: Used for mapping between object and action.

Action Forward: Used to forward the result from controller to destination

9. What is the 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
  • It is responsible for initialization and clean-up of resources

10. What is MVC ?

Model View Controller (MVC) is a design pattern used to perform changes in the application.

Model: Model is referring to business or database. It stores the state of the application. Model has no knowledge of the View and Controller components.

View: View is referring to the Page Design Code. It is responsible for the showing the result of the user’s query. View modifies itself when any changes in the model happen.

Controller: Controller is referring to navigational code. Controller will chose the best action for each incoming request, generate the instance of that action and execute that action.

11. Describe validate() and reset() method.

validate(): It is used to validate properties after they have been populated; Called before FormBean is handed to Action. Method signature for the validate() method is; public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)

Syntax: public ActionErrors validate(ActionMapping mapping,HttpServletRequest request)

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. public void reset() {}.

Syntax: public void reset() {}

12. 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.

13. What is DispatchAction ?

The DispatchAction class is used to group related actions into one class. Using DispatchAction class, you can have a method for each logical action compared than a single execute method.

14. What is the use of ForwardAction ?

The ForwardAction class is useful when you are trying to integrate Struts into an existing application that uses Servlets to perform business logic functions.

15. What is IncludeAction ?

The IncludeAction class is useful when you want to integrate Struts into an application that uses Servlets. Use the IncludeAction class to include another resource in the response to the request being processed.

16. What is LookupDispatch Action ?

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.

17. 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.

18. Give various Struts tag libraries.

  • HTML Tags
  • Bean Tags
  • Logic Tags
  • Template Tags
  • Nested Tags
  • Tiles Tags

19. What design patterns are used in Struts ?

  • Service to Worker
  • Dispatcher View
  • Composite View (Struts Tiles)
  • Front Controller
  • View Helper
  • Synchronizer Token

20. What is the life cycle of ActionForm?

  • 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

21. How to display validation error on JSP page ?

Validation error are those error which arises when user or client enters the invalid format data into the form. For this validation of data struts enables the programmer with the Validator() method which validates both the data from client side and the server side.

We can display all error in the JSP page by using the following syntax in the code.

<html:error/>

22. How to combine the Struts with Velocity Template ?

  • Set classpath to Velocity JARs.
  • Make web.xml file to identify the Velocity servlet.
  • Select Velocity toolbox.xml in WEB-INF directory.
  • Modify struts-config to point its views to Velocity templates instead of JSPs.
  • Create a Velocity template for each page you want to render.

23. In how many ways duplicate form submission can occurs ?

  • Using refresh button.
  • By clicking submit button more than once before the server sent back the response.
  • By clicking back navigation button present in browser.
  • The browser is restores to submit the form again.
  • By clicking multiple times on a transaction that is delayed than usual.

24. What are the steps used to setup dispatch action ?

  • Create a subclass for DispatchAction.
  • Create method for logical action and their related actions.
  • Request Parameter is created for each action.
  • Define ActionMapping.
  • The JSP takes on the subclass defined for dispatch actin method names as their values.

25. What are Custom tags ?

Custom Tags are User Defined Tags, which means that user can create those tags depending upon their need in the application.

When a JSP page consisting of user- defined or custom tag is translated into a Servlet, the custom is also get translated into operation.


Help in fast development of the application due to custom tag reusability.

26. What are inner class and anonymous class ?

Inner class: classes that are defined within other classes.

  • The nesting is a relationship performed between two different classes.
  • An inner class can access private members and data.
  • Inner classes have clearly two benefits:
    • Name control
    • Access control

Anonymous class: Anonymous class is a class defined inside a method without a name. It is instantiated and declared in the same method. It does not have explicit constructors.

27. What is struts.devMode ?

The struts.devMode is used to make sure that framework is running in development mode or production mode by setting true or false. struts.devMode is set to false in production phase to reduce impact of performance. By default it is “false”. It is used because of the following reasons:

Resource Reloading: Resource bundle reload on every request
Modification: struts.xml can be modified without restarting or redeploying the application
Error Handling: The error occurs in the application will be reported, as oppose to production mode.

28. What are action errors ?

Action error: when user or client submits the incorrect or invalid data in the application, then these errors are known as Action error.

Action errors are generated by the clients.
Action error should be determined as soon as possible.

The impacts of such Action Error are:

Wastage of server time and resources.
Negative impact on code quality.

29. Difference between HTTP Forward and HTTP indirect ?

HTTP Forward:

HTTP Forward is the process used for displaying a page when requested by the end user. The user requests for a resource by clicking on a hyperlink or submitting a form and the next page is displayed to the user as a response. Under Servlet container, HTTP Forward is done by invoking the following command:

RequestDispatcher dispatcher = httpServletRequest.getRequestDispatcher(url);
Dispatcher.forward(httpServletRequest, httpServletResponse);

HTTP Redirect:

HTTP Redirect is more complex as compared to HTTP direct. In this a user requests a resource and the responses are first sent to the user, which is not the requested resource.
It is a response with HTTP code “302” and contains the URL of the requested resource. This URL can be same or different from original requested URL. The client browser sends the request for the resource again with the new URL, this time, the requested resource is sent to the user. In web tier HTTP redirect can be done by using the simple API, sendRedirect() on the HttpServletResponse instance.

30. What is the procedure of operation of a form tag ?

The FormTag might comprise other tags in its body. Like a submit tag whose example is given below the form tag operates as follows:

<input name=”firstName” type=”text” value=”” />
The servlet container passes the JSP to display the HTML.
<html>
<head>
<html:base/>
</head>
<body>
<form name=”CityForm” action=”/submitForm.do”>
<input type=”text” name=”firstName” value=”” />
<input type=”text” name=”lastName” value=”” />
<input type=”submit” name=”Submit” value=”” />
</form>
</body>
</html>

On encountering a form tag a container activates the doStartTag() method which performs the same function as of the Request Processor in the execute() method.

1. The Form Tag in its path attributes checks for an Action Mapping with /submitForm.

2. On finding the action mapping the search for city form is generated under request for session scope.

3. Failing to find the one the form tag creates and puts a new value for the one in the related context. If the value is found it is used which makes form name available.

4. The action tag is accessed by form field tags from page context to retrieve the values from action form attributes in accordance to names.

31. How to work with error tags?

Working with error tags is not as complex as it looks. Error tags can be put in place by adding <html:errors/> in to JSP. Unlike others the error tag does not have a body it also does not consist of any attributes. The only function the error tag performs is to display the error in place where the error tag is placed using three features – the header, the body and the footer. The list of the error text to be displayed or written is represented by the error body.

32. How can link tag’s action attribute be used ?

There are various ways in which Link tag can be used with the help of conjunction with forward action because link tag itself has several variations. Another way of using link tag can be out forward as declaring of Page 1 hyperlink that navigates to Page 2 by using the following method :

<html:link action=”gotoPage2”>Go to Page 2</html:link>

After this link the action mapping of /gotopage 2 by struts configuration file :

<action path=”/gotoPage2”
parameter=”/Page2.jsp”
type=”org.apache.struts.actions.ForwardAction” />

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.

34. Difference between struts1 and struts2.

Struts1Struts2
Action class is not POJO. You need to inherit abstract class.Action class is POJO. You don’t need to inherit any class or implement any interface.
Front controller is ActionServlet.Front Controller is StrutsPrepareAndExecuteFilter.
It uses the concept of RequestProcessor class while processing request.It uses the concept of Interceptors while processing the request.
It has only JSP for the view component.It has JSP, Freemarker, Valocity etc. for the view component.
Configuration file name can be [anyname].xml and placed inside WEB-INF directory.Configuration file must be struts.xml and placed inside classes directory.
Action and Model are separate.Action and Model are combined within action class.

35. What is interceptor?

Interceptor is an object that is invoked at the preprocessing and postprocessing of a request. In Struts 2, interceptor is used to perform operations such as validation, exception handling, internationalization, displaying intermediate result etc.

36. What is OGNL ?

The Object Graph Navigation Language (OGNL) is an expression language. It simplifies the accessibility of data stored in the ActionContext.

37. What is jsonValidation Inceptor ?

The AJAX validation is performed by jsonValidation interceptor. It is not found in the default stack so we need to define it explicitly. It doesn’t perform any validation itself that is why it must be used with validation interceptor. It is found in the jsonValidationWorkflowStack, that includes jsonValidation, validation and workflow interceptors and basicstack.

Related Articles

Core Java

1. What are primitive types in Java ? byte, short, int, long, float, double, char, boolean… 2. What are tokens? Name the 5 types of tokens available in Java with an example…

Spring

1. Hello World Program HelloWorld.java MainApp.java 2. How to create Spring applcation. Create Java Class Create XML File Create test class Load jar files required…

Advance Java

  1. What is JDBC? JDBC technology is an API that provides  connectivity to a wide range of SQL databases and access to other tabular…

Hibernate

1. Hibernate Example using XML in Eclipse. First we need to add jar files. Creating Persistent Class Student.java Creating mapping file for persistent class student.hbm.xml…

HIBERNATE

1. Hibernate Example using XML in Eclipse. First we need to add jar files. Creating Persistent Class Student.java public class Student { private int id;…

Responses

Your email address will not be published. Required fields are marked *

×