• +91 9971497814
  • info@interviewmaterial.com

Struts Interview Questions Answers

Question 1 : Action.execute() and Action.getResources()

Answer 1 : In Struts 1.0, request handling logic is coded in Action.perform(); however, Action.perform() throws only IOException and SevletException. To facilitate the new declarative exception handling , the request handling method needs to throw Exception, the superclass of all the checked exceptions. Therefore, to both maintain backward compatibility and facilitate declarative exception handling, Action.perform() is now deprecated in favour of Action.execute(). You also have to be careful if you use DispatchAction in your existing applications. At the time of writing, the DispatchAction in Struts 1.1 beta has not yet been updated to use execute(). (A bug report has been filed in Struts' bug database.) Therefore, without modifying the DispatchAction class yourself, declarative exception handling will not work with DispatchAction subclasses. In addition, Action.getResources() is now deprecated. Instead, you should call Action.getResources(HttpServletRequest) instead. This allows Struts to return to you the sub-application specific message resources. Otherwise, the message resources for the default sub-app will be used.

Question 2 : ActionServlet Configurations

Answer 2 : With the introduction of sub-applications, a more flexible way is introduced to configure each sub-application independently. Many of the configuration entries (e.g. resource bundle location, maximum upload file size, etc) that used to be defined in web.xml have now been moved to struts-config.xml. The original entries in web.xml are deprecated but will still be effective.

Question 3 : Are the Struts tags XHTML compliant ?

Answer 3 : If you use an <html:html xhtml="true> or <html:xhtml/> element on your page, the tags will render as XHTML (since Struts 1.1).

Question 4 : Both JSF and Struts will continue to exist for a while. The Struts community is aware of JSF and is positioning itself to have strong support for JSF. See What about JSTL and JavaServer faces?

Answer 4 : From a tools perspective, if you look at the support for JSF versus Struts in WebSphere Studio, the Struts tools are focused around the controller aspects. The Web Diagram editor helps build your Struts configuration and the wizards/editors build Struts artifacts. The JSF tools are geared towards building pages, and in essence, hide the JSF framework from you. Expect WebSphere Studio to support both frameworks for a while. As JSF matures, expect to see some of the controller aspects in JSF to become toolable.

Question 5 : Can I have an Action without a form?

Answer 5 : Yes. If your Action does not need any data and it does not need to make any data available to the view or controller component that it forwards to, it doesn't need a form. A good example of an Action with no ActionForm is the LogoffAction in the struts example application: <action path="/logoff" type="org.apache.struts.webapp.example.LogoffAction"> <forward name="success" path="/index.jsp"/> </action> This action needs no data other than the user's session, which it can get from the Request, and it doesn't need to prepare any view elements for display, so it does not need a form. However, you cannot use the <html:form> tag without an ActionForm. Even if you want to use the <html:form> tag with a simple Action that does not require input, the tag will expect you to use some type of ActionForm, even if it is an empty subclass without any properties.

Question 6 : Can I use JavaScript to submit a form?

Answer 6 : You can submit a form with a link as below. BTW, the examples below assume you are in an block and 'myForm' is picked up from the struts-config.xml name field of the action. <a href='javascript:void(document.forms["myForm"].submit()>My Link</a> Now the trick in the action is to decode what action you intend to perform. Since you are using JavaScript, you could set a field value and look for it in the request or in the form. ... html/javascript part ... <input type='hidden' value='myAction' /> <input type='button' value='Save Meeeee' onclick='document.forms["myForm"].myAction.value="save"; document.forms["myForm"].submit();' /> <input type='button' value='Delete Meeeee' onclick='document.forms["myForm"].myAction.value="delete"; document.forms["myForm"].submit();' /> ... the java part ... class MyAction extends ActionForm implements Serializable { public ActionForward execute (ActionMapping map, ActionForm form, HttpServletRequest req, HttpServletResponse) { String myAction = req.getParameter("myAction"); if (myAction.equals("save") { // ... save action ... } else if (myAction.equals("delete") { // ... delete action ... } } } } This is just one of many ways to achieve submitting a form and decoding the intended action. Once you get used to the framework you will find other ways that make more sense for your coding style and requirements. Just remember this example is completely non-functional without JavaScript.

Question 7 : Can I use multiple HTML form elements with  the same name?

Answer 7 : Yes. The issue is that only one action class can be associated with a single form. So the real issue is how do I decode multiple submit types to a single Action class. There is more than one way to achieve this functionality. The way that is suggested by struts is right out of the javadoc for LookupDispatchAction . Basically, LookupDispatchAction is using the keys from ApplicationProperties.resources as keys to a map of actions available to your Action class. It uses reflection to decode the request and invoke the proper action. It also takes advantage of the struts <html:submit> tags and is straight forward to implement. You can roll your own with JavaScript events and javascript:void (document.forms["myform"].submit) on any html element. This gives you control of how you want your page to look. Again you will have to decode the expected action in the execute method of your action form if you choose this route.

Question 8 : Can I use multiple HTML form elements with the same name?

Answer 8 : Yes. Define the element as an array and Struts will autopopulate it like any other. private String[] id= {}; public String[] getId() { return this.id; } public void setItem(String id[]) {this.id = id;} And so forth

Question 9 : Can I use other beans or hashmaps with ActionForms?

Answer 9 : Yes. There are several ways that you can use other beans or hashmaps with ActionForms. * ActionForms can have other beansor hashmaps as properties * "Value Beans" or "Data Transfer Objects" (DTOs) can be used independently of ActionForms to transfer data to the view * ActionForms can use Maps to support "dynamic" properties (since Struts 1.1) ActionForms (a.k.a. "form beans") are really just Java beans (with a few special methods) that Struts creates and puts into session or request scope for you. There is nothing preventing you from using other beans, or including them in your form beans. Here are some examples: Collections as properties Suppose that you need to display a pulldown list of available colors on an input form in your application. You can include a string-valued colorSelected property in your ActionForm to represent the user's selection and a colorOptions property implemented as a Collection (of strings) to store the available color choices. Assuming that you have defined the getters and setters for the colorSelected and colorOptions properties in your orderEntryForm form bean, you can render the pulldown list using: <html:select property="colorSelected"> <html:options property="colorOptions" name="orderEntryForm"/> </html:select> The list will be populated using the strings in the colorOptions collection of the orderEntryForm and the value that the user selects will go into the colorSelected property that gets posted to the subsequent Action. Note that we are assuming here that the colorOptions property of the orderEntryForm has already been set. See How can I prepopulate a form? for instructions on how to set form bean properties before rendering edit forms that expect properties to be pre-set. Independent DTO An Action that retrieves a list of open orders (as an ArrayList of Order objects) can use a DTO independently of any form bean to transfer search results to the view. First, the Action's execute method performs the search and puts the DTO into the request: ArrayList results = businessObject.executeSearch(searchParameters); request.setAttribute("searchResults",results); Then the view can iterate through the results using the "searchResults" request key to reference the DTO: ` <logic:iterate id="orde

Question 10 : Can you compare the advantages and disadvantages of JSF vs. Struts. Both now, and from what you may know of futures, how and if JSF will evolve into a superior technology vs. Struts? Include how WSAD plays into the comparison if it will help differentiate the two.

Answer 10 : This is a very popular question these days. In general, JSF is still fairly new and will take time to fully mature. However, I see JSF being able to accomplish everything Struts can, plus more. Struts evolved out of necessity. It was created by developers who were tired of coding the same logic again and again. JSF emerged both from necessity and competition. Struts has several benefits: * Struts is a mature and proven framework. It has been around for a few years and deployed successfully on many projects. The WebSphere Application Server admin console is a Struts application. * Struts uses the Front Controller and Command patterns and can handle sophisticated controller logic. * In addition to the core controller function, it has many add-on benefits such as layouts with Tiles, declarative exception handling, and internationalization. There are disadvantages: * Struts is very JSP-centric and takes other frameworks to adapt to other view technologies. * Although Struts has a rich tag library, it is still geared towards helping the controller aspect of development and does not give a sense that you are dealing with components on a page. Therefore, it is not as toolable from a view perspective. * Struts requires knowledge of Java™. Its goal was to aid Java developers, but not to hide Java. It does not hide details of the Java language to Web developers that well. * ActionForms are linked programmatically to the Struts framework. Therefore, to decouple the model, you need to write transfer code or use utilities to move data from Action Forms to the Model on input. JSF is an evolution of a few frameworks, including Struts. The creator of Struts, Craig McClanahan, is one of the JSF specification leads. Therefore, it is not by accident to see some overlap between Struts and JSF. However, one of JSF's major goals is to help J2EE Web applications to be easily developed using RAD tools. As such, it introduces a rich component model. JSF has several advantages: * JSF is a specification from Sun® and will be included in future versions of the J2EE specification. All major vendors are pledging strong support for JSF. * JSF uses the Page Controller Pattern and therefore aids in Page rich applications. Components can respond to event from components on a page. * JSF

Struts Contributors


Share your email for latest updates


Our partners