• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

Struts Interview Questions Answers

Question - 61 : - How can I 'chain' Actions?

Answer - 61 : - Chaining actions can be done by simply using the proper mapping in your forward entries in the struts-config.xml file. Assume you had the following two classes: /* com/AAction.java */ ... public class AAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Do something return mapping.findForward("success"); } }   /* com/BAction.java */ ... public class BAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Do something else return mapping.findForward("success"); } } Then you can chain together these two actions with the Struts configuration as shown in the following excerpt: ... <action-mappings type="org.apache.struts.action.ActionMapping"> <action path="/A" type="com.AAction" validate="false"> <forward name="success" path="/B.do" /> </action> <action path="/B" type="com.BAction" scope="session" validate="false"> <forward name="success" path="/result.jsp" /> </action> </action-mappings> ... Here we are assuming you are using a suffix-based (.do) servlet mapping, which is recommended since module support requires it. When you send your browser to the web application and name the action x.do (i.e. http://localhost:8080/app/x.do) it will execute AAction.execute(), which will then forward to the "success" mapping. This causes the execution of BAction.execute() since the entry for "success" in the configuration file uses the .do suffix. Of course it is also possible to chain actions programmatically, but the power and ease of being able to "reroute" your web application's structure using the XML configuration file is much easier to maintain. A

Question - 62 : - Why is ActionForm a base class rather than an interface?

Answer - 62 : - The MVC design pattern is very simple to understand but much more difficult to live with. You just need this little bit of Business Logic in the View logic or you need just that little bit of View logic in the Business tier and pretty soon you have a real mess. Making ActionForm a class takes advantage of the single inheritance restriction of Java to it makes it more difficult for people to do things that they should not do. ActionForms implemented as interfaces encourage making the property types match the underlying business tier instead of Strings, which violates one of the primary purposes for ActionForms in the first place (the ability to reproduce invalid input, which is a fundamental user expectation). ActionForms as an interface would also encourage using existing DAO objects as ActionForms by adding ‘implements ActionForm’ to the class. This violates the MVC design pattern goal of separation of the view and business logic. Since the goal of struts is to enforce this separation, it just makes more sense for Struts to own the ActionForm. DynaActionForms relieve developers of maintaining simple ActionForms. For near zero maintenance, try Niall Pemberton's LazyActionForm

Question - 63 : - 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 - 63 : - 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

Question - 64 : - What helpers in the form of JSP pages are provided in Struts framework?

Answer - 64 : - --struts-html.tld --struts-bean.tld --struts-logic.tld

Question - 65 : - How is the MVC design pattern used in Struts framework?

Answer - 65 : - In the MVC design pattern, application flow is mediated by a central Controller. The Controller delegates requests to an appropriate handler. The handlers are tied to a Model, and each handler acts as an adapter between the request and the Model. The Model represents, or encapsulates, an application's business logic or state. Control is usually then forwarded back through the Controller to the appropriate View. The forwarding can be determined by consulting a set of mappings, usually loaded from a database or configuration file. This provides a loose coupling between the View and Model, which can make an application significantly easier to create and maintain. Controller--Servlet controller which supplied by Struts itself; View --- what you can see on the screen, a JSP page and presentation components; Model --- System state and a business logic JavaBeans.

Question - 66 : - Write code of any Action Class?

Answer - 66 : - Here is the code of Action Class that returns the ActionForward object. import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; public class TestAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward(\"testAction\"); } }

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

Answer - 67 : - 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 - 68 : - Why was reload removed from Struts (since 1.1)?

Answer - 68 : - The problem with ReloadAction was that Struts was trying to act like a container, but it couldn't do a proper job of it. For example, you can't reload classes that have been modified, or (portably) add new classes to a running web application (even if the container supported it). Meanwhile, as Struts 1.1 was being developed, work progressed on things like Tomcat's reload command via the Manager webapp. This feature allows you to quickly reload-on-demand, complete with saving and restoring your session). It started to make even less sense for Struts to half-implement a feature that containers are implementing fully. A more minor point is that freezing the configuration information at application startup time allows Struts to safely access the mapping information without bothering with synchronization. The "startup-only" strategy creates a modest but real improvement in performance for all users. So, ReloadAction is not supported since Struts 1.1 for two reasons: * It never did let you reload everything that you would really want to -- particularly changed classes -- so many people ended up having to reload the webapp anyway. * Containers are starting to offer reload-on-demand features which does the same thing as the Struts ReloadAction, only better. * Not supporting ReloadAction lets Struts avoid doing synchronization locks around all the lookups (like figuring out which action to use, or the destination of an ActionForward) so applications can run a little faster. Of course, if someone came up with an implementation that solved these problems without creating any others, we would not be opposed to including a new ReloadAction.

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

Answer - 69 : - 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 - 70 : - How can one enable front-end validation based on the xml in validation.xml?

Answer - 70 : - The <html:javascript>  tag allows front-end validation based on the xml in validation.xml. For example the code: generates the client side JavaScript for the form "logonForm" as defined in the validation.xml file. The <html:javascript> when added in the JSP file generates the client side validation script.  

Computer Contributors

krishan
Rajeev Katiyar
Yes Baroda

Share your email for latest updates

Name:
Email:

Our partners