• +91 9971497814
  • info@interviewmaterial.com

Struts Interview Questions Answers

Question 1 : What configuration files are used in Struts?

Answer 1 : ApplicationResources.properties struts-config.xml These two files are used to bridge the gap between the Controller and the Model.

Question 2 : Why are my checkboxes not being set from ON to OFF?

Answer 2 : A problem with a checkbox is that the browser will only include it in the request when it is checked. If it is not checked, the HTML specification suggests that it not be sent (i.e. omitted from the request). If the value of the checkbox is being persisted, either in a session bean or in the model, a checked box can never unchecked by a HTML form -- because the form can never send a signal to uncheck the box. The application must somehow ascertain that since the element was not sent that the corresponding value is unchecked. The recommended approach for Struts applications is to use the reset method in the ActionForm to set all properties represented by checkboxes to null or false. The checked boxes submitted by the form will then set those properties to true. The omitted properties will remain false. Another solution is to use radio buttons instead, which always submit a value. It is important to note that the HTML specification recommends this same behavior whenever a control is not "successful". Any blank element in a HTML form is not guaranteed to submitted. It is therefor very important to set the default values for an ActionForm correctly, and to implement the reset method when the ActionForm might kept in session scope.

Question 3 : Resources under WEB-INF

Answer 3 : According to the Servlet specification, resources (e.g. JSP files) stored under WEB-INF are protected and cannot be accessed directly by the browsers. One design idiom for Struts 1.0 is to put all the JSP files under WEB-INF and front them by Actions so that clients cannot illegally access the JSPs. With the introduction of sub-application prefixes in Struts 1.1, mapping resources under WEB-INF gets complicated. Extra configuration steps utilizing the pagePattern and forwardPattern attributes of the element in struts-config.xml is required to inform Struts to construct the paths correctly. More specifically, you need to set these attributes to the pattern "/WEB-INF/$A$P".

Question 4 : What are the core classes of Struts?

Answer 4 : Action, ActionForm, ActionServlet, ActionMapping, ActionForward are basic classes of Structs.

Question 5 : Do ActionForms  have to be true JavaBeans?

Answer 5 : ActionForms are added to a servlet scope (session or request) as beans. What this means is that, for certain functionality to be available, your ActionForms will have to follow a few simple rules. First, your ActionForm bean must have a zero-arguments constructor. This is required because Struts must be able to dynamically create new instances of your form bean class, while knowing only the class name. This is not an onerous restriction, however, because Struts will also populate your form bean's properties (from the request parameters) for you. Second, the fields of your form bean are made available to the framework by supplying public getter and setter methods that follow the naming design patterns described in the JavaBeans Specification. For most users, that means using the following idiom for each of your form bean's properties: private {type} fieldName; public {type} getFieldName() { return (this.fieldName); } public void setFieldName({type} fieldName) { this.fieldName = fieldName; } NOTE - you MUST obey the capitalization conventions shown above for your ActionForm properties to be recognized. The property name in this example is "fieldName", and that must also be the name of the input field that corresponds to this property. A bean property may have a "getter" method and a "setter" method (in a form bean, it is typical to have both) whose name starts with "get" or "set", followed by the property name with the first character capitalized. (For boolean properties, it is also legal to use "is" instead of "get" as the prefix for the getter method.) Advanced JavaBeans users will know that you can tell the system you want to use different names for the getter and setter methods, by using a java.beans.BeanInfo class associated with your form bean. Normally, however, it is much more convenient to follow the standard conventions. WARNING - developers might be tempted to use one of the following techniques, but any of them will cause your property not to be recognized by the JavaBeans introspection facilities, and therefore cause your applications to misbehave: * Using getter and setter method names that do not match - if you have a getFoo() method for your getter, but a setBar() method for your setter, Java will not recognize these methods as r

Question 6 : What is an “ActionForm”?

Answer 6 : An “ActionForm” is a JavaBean that extends org.apache.struts.action.ActionForm. ActionForm maintains the session state for web application and the “ActionForm” object is automatically populated on the server side with data entered from a form on the client side.  What is Struts Validator Framework? The “Struts Framework” provides the functionality to validate the form data. It can be used to validate the data in the user’s browser as well as on the server side. Struts Framework checks the JavaScript code and it can be used to validate the form data on the client browser. Server side validation of form data can be accomplished by subclassing your “form” Bean with DynaValidatorForm class. The “Validator” framework was developed by David Winterfeldt as a third-party “add-on” to Struts. Now the Validator framework is part of the “Jakarta Commons” project and it can be used with or without Struts. The Validator framework comes integrated with the Struts Framework and can be used without any making any additional settings. Describe the details of XML files used in the “Validator Framework”? The Validator Framework uses two XML configuration files 1) validator-rules.xml and 2) validation.xml. The validator-rules.xml defines the standard validation routines. These are reusable and used in validation.xml to define the form specific validations. The validation.xml defines the validations applied to a form bean.

Question 7 : What is ActionForm?

Answer 7 : An ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm. ActionForm maintains the session state for web application and the ActionForm object is automatically populated on the server side with data entered from a form on the client side.

Question 8 : DynaBean and BeanUtils

Answer 8 : Another major complaint usually heard amongst Struts 1.0 users is the extensive effort involved in writing the FormBean (a.k.a. ActionForm) classes. Struts provides two-way automatic population between HTML forms and Java objects, the FormBeans. To take advantage of this however, you have to write one FormBean per HTML form. (In some use cases, a FormBean can actually be shared between multiple HTML forms. But those are specific cases.) Struts' FormBean standard follows faithfully the verbose JavaBean standard to define and access properties. Besides, to encourage a maintainable architecture, Struts enforces a pattern such that it is very difficult to 'reuse' a model-layer object (e.g. a ValueObject from the EJB tier) as a FormBean. Combining all these factors, a developer has to spend a significant amount of time to write tedious getters/setters for all the FormBean classes. Struts 1.1 offers an alternative, Dynamic ActionForms, which are based on DynaBeans. Simply put, DynaBeans are type-safe name-value pairs (think HashMaps) but behave like normal JavaBeans with the help of the BeanUtils library. (Both the DynaBeans and the BeanUtils library were found to be useful and generic enough that they have been 'promoted' into Jakarta's Commons project.) With Dynamic ActionForms, instead of coding the tedious setters/getters, developers can declare the required properties in the struts-config.xml files. Struts will instantiate and initialize Dynamic ActionForm objects with the appropriate metadata. From then onwards, The Dynamic ActionForm instance is treated as if it is an ordinary JavaBean by Struts and the BeanUtils library.

Question 9 : How can one make any “Message Resources” definitions file available to the “Struts Framework” environment?

Answer 9 : Answer: “Message Resources” definitions file are simple .properties files and these files contain the messages that can be used in the struts project. “Message Resources” definition files can be added to the struts-config.xml file through <message-resources /> tag. Example: <message-resources parameter="MessageResources" />

Question 10 : What's the best way to deal with migrating a large application from Struts to JSF? Is there any tool support that can help?

Answer 10 : Answer: This is a complicated task depending on your Struts application. Because the two frameworks have different goals, there are some challenges. Migrate your response pages first. Keep the Struts controller and place and forward to JSF pages. Then you can configure Struts forwards to go through the Faces servlet. Consider looking at the Struts-Faces framework from Apache. See the framework chapter in JSF in Action.

Struts Contributors


Share your email for latest updates


Our partners