Wednesday, 26 October 2011

Struts2

Ques: 01
What is the use of tag defined by Bean tag library in Struts framework?
Ans:The tag is used to retrieve and print the value of a named bean property. If the format attribute is encountered, then the value being written will be formatted based upon the format string represented by the format attribute.
The tag has no body and supports nine attributes:
bundle, filter, format, formatKey, ignore, locale, name, property, scope.


Ques: 02

Why we use Struts framework in project?
Ans:Struts framework follow the Model-View-Controller design pattern. Struts framework provides the centralised control to throughout the Web application. If the application is big like any banking application etc, then you should use Struts as it is very robust, makes the things simple, it also has lot of readily available security features.
Ques: 03
What is heirarchy of files in Struts?
Ans:The heirarchy of files depends upon Struts-config.xml that plays an important role in struts framework and it is used mainly to have an access to various Action classes using the Action-mapping element and then to forward it to appropriate jsp files. The struts tag libraries include Bean tag, Template tag, Tiles tag, Logic tag, and HTML tag libraries.
Ques: 04
why do we have only one ActionServlet in Struts?
Ans:The Struts Framework provides us the cetralised control throughout the web application. We use the ActionServlet as a centralised controller. The ActionServlet takes care of all the request and direct it to the requested resourse. Using the more than controller doing the same thing leads to the duplicacy and redudndancy in your code. Hence there is no need to use more than one controller in a web application. That is why Struts uses the concept of a single Servlet controller.
Ques: 05
What is the difference between Struts 1.x and Struts 2.x
Ans:The difference between Struts 1.x and Struts 2.x:

* Struts 1.x extends the abstract base class by its action class. The problem with struts1 is that it uses the abstract classes rather than interfaces. While in Struts 2.x, an Action class implements an Action interface, along with other interfaces use optional and custom services.
* Struts 1.x Actions are singletons therefore they must be thread-safe because only one instance of a class handles all the requests for that Action. Struts 2.x doesn't have thread-safety issues as Action objects are instantiated for each request. A servlet container generates many throw-away objects per request, and one more object does not impose a performance penalty or impact garbage collection.
* In Struts 1.x Actions are dependent on the servlet API because HttpServletRequest and HttpServletResponse is passed to the execute method when an Action is invoked. Container does not treat the Struts 2 Actions as a couple. Servlet contexts are typically represented as simple Maps that allow Actions to be tested in isolation. Struts 2 Actions can still access the original request and response, if required.
* Struts 1 binds objects into the page context by using the standard JSP mechanism. Struts 2 uses a ValueStack technology to make the values accessible to the taglibs without coupling the view to the object to which it is rendering.

Ques: 06
Why was perform() method changed as execute() method in Action class?
Ans:The Action.perform() is the method called by the ActionServlet defined in Struts 1.x and declares that it throws only IOException and ServletException. To support declarative exception handling, the method's signature changed in perform() and it becomes the execute() method in the Struts 2.x. This is typically where your business logic resides.
Ques: 07
What will happen if the mapping to actionform in the struts-config.xml is mentioned wrong?
Ans:An excetion will be thrown. If we try to cast the action form and using it, then it will throw nullpointer exception.

Ans:It will give an exception that, properties not found which are used in the front end(jsp)

Ques: 08

How can we communicate from struts to ejb?
Ans:The ActionClass overrides execute() method.
Then in body of execute() method:
* Get Initial Context
* Get home object using JNDI look up.
* Create the bean and call the business methods.
Ques: 09
What are the core classes of Struts?
Ans:The core classes of Struts are:
* Action,
* ActionForm,
* ActionServlet,
* ActionMapping,
* ActionForward, etc.
Ques: 10
Is Action class is Servlet or Not ? if yes why ?
Ans:Action class extents org.apache.struts.action.Action which has nothing to do with Servlet. Its a plain java class which is been called/Managed by a well defined framework called Struts.
Ques: 11
What is difference between Html tags and Struts specific HTML Tags
Ans:Struts tags bind the property value with the Formbean property defined. HTML tags are static and Struts tags are dynamic.

Ques: 12
What are the contents of web.xml and struts-config.xml? What is the difference between them? How to relate these tow xml files?
Ans:A web.xml file is the DD (deployment descriptor) for any web based application while struts-config.xml is for specifically struts based application. A web.xml file establishes connection between application and container whereas struts-config.xml bridges gap between view and controller. A web.xml file is read by container when we start the container. struts-config.xml file is used by init() method of ActionServlet.
Ques: 13
Should every html form property have a corresponding formbean get/set and variable.?
Ans:Yes! Every html form property must have a corresponding FormBean get/set methods. Otherwise you'll get an error.
Ques: 14
Can I have more than one struts-config.xml file for a single Struts application?


Ans:Yes! One can have more than one struts-config.xml file for a single Struts application. You can define different struts-conifg.xml file for different modules in a single web application. It is useful when you are developing a distributed application.
But you must enter those struts-config.xml files details in deployment descripter (i.e., web.xml) file.

Example:

config/WEB-INF/struts-first-config.xml,/WEB-INF/struts-second-config.xml,/WEB-INF/struts-third-config.xml


Here three struts configutation files are deployed in a single application.
Ques: 15
How the container will know this is a struts application?
Ans:The web container do not need to know about the design pattern we used in our application. Our Struts application is deployed in the web.xml file. The container reads the web.xml file, and acts accordingly.
Ques: 16
What is the super class of Action and ActionForm?
Ans:There is no immediate super class for Action and Action Form, Object class is the super class for Action and ActionForm.
Ques: 17
What is the difference between Apache struts and Jakarta Struts? why there are 2 names?
Ans:Both apache struts and jakarta struts are one and the same. Struts was originally created by Craig McClanahan and was donated to the Apache Foundation in May, 2000. Its evolve as a Jakarta Project, in early 2004 it become an official Apache Project.
Ques: 18
Explain about token feature in Struts?
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.
Ques: 19
What is the purpose of tiles-def.xml file, resourcebundle.properties file, validation.xml file?
Ans:A tiles-def.xml is used as a configuration file for an appliction during tiles development. You can define the layout, header, footer, body content for your View.
The validation.xml file is used to declare sets of validations that should be applied to Form Beans. Each Form Bean you want to validate has its own definition in this file. Inside that definition, you specify the validations you want to apply to the Form Bean's fields.
Instead of having hard-coded error messages in the framework, Struts Validator allows you to specify a key to a message in the ApplicationResources.properties (or resourcebundle.properties) file that should be returned if a validation fails.
Ques: 20
How you will save the data across different pages for a particular client request using Struts?
Ans:You can use the session object to save the data across different pages for a particular client request using Struts.
Create an appropriate instance of ActionForm that is form bean and store that form bean in session scope. So that it is available to all the pages that for a part of the request.

request.getSession()
session.setAttribute()

1 comment: