• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

Struts Interview Questions Answers

Question - 51 : - How can I avoid validating a form before data is entered?

Answer - 51 : - The simplest way is to have two actions. The first one has the job of setting the form data, i.e. a blank registration screen. The second action in our writes the registration data to the database. Struts would take care of invoking the validation and returning the user to the correct screen if validation was not complete. The EditRegistration action in the struts example application illustrates this: < action path="/editRegistration"> type="org.apache.struts.webapp.example.EditRegistrationAction" attribute="registrationForm" scope="request" validate="false"> <forward name="success path="/registration.jsp"/> </action> When the /editRegistration action is invoked, a registrationForm is created and added to the request, but its validate method is not called. The default value of the validate attribute is true, so if you do not want an action to trigger form validation, you need to remember to add this attribute and set it to false.

Question - 52 : - Why does the <html:link> tag URL-encode javascript and mailto links?

Answer - 52 : - The <html:link> tag is not intended for use with client-side references like those used to launch Javascripts or email clients. The purpose of link tag is to interject the context (or module) path into the URI so that your server-side links are not dependent on your context (or module) name. It also encodes the link, as needed, to maintain the client's session on the server. Neither feature applies to client-side links, so there is no reason to use the <html:link> tag. Simply markup the client-side links using the standard tag.

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

Answer - 53 : - 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 - 54 : - Can't I just create some of my JavaBeans in the JSP using a scriptlet?

Answer - 54 : - Struts is designed to encourage a Model 2/MVC architecture. But there is nothing that prevents you from using Model 1 techniques in your JavaServer Pages, so the answer to the question is "Yes, you can". Though, using Model 1 techniques in a Struts application does go against the grain. The approach recommended by most Struts developers is to create and populate whatever objects the view may need in the Action, and then forward these through the request. Some objects may also be created and stored in the session or context, depending on how they are used. Likewise, there is nothing to prevent you from using scriptlets along with JSP tags in your pages. Though, many Struts developers report writing very complex scriplet-free applications and recommend the JSP tag approach to others. For help with Model 1 techniques and scriptlets, you might consider joining the Javasoft JSP-interest mailing list, where there are more people still using these approaches.

Question - 55 : - What is the design role played by Struts?

Answer - 55 : - The role played by Structs is controller in Model/View/Controller(MVC) style. The View is played by JSP and Model is played by JDBC or generic data source classes. The Struts controller is a set of programmable components that allow developers to define exactly how the application interacts with the user.

Question - 56 : - Why do we need Struts?

Answer - 56 : - Java technologies give developers a serious boost when creating and maintaining applications to meet the demands of today's public Web sites and enterprise intranets. Struts combines Java Servlets, Java ServerPages, custom tags, and message resources into a unified framework. The end result is a cooperative, synergistic platform, suitable for development teams, independent developers, and everyone in between.

Question - 57 : - How you will make available any Message Resources Definitions file to the Struts Framework Environment?

Answer - 57 : - Message Resources Definitions file are simple .properties files and these files contains the messages that can be used in the struts project. Message Resources Definitions files can be added to the struts-config.xml file through < message-resources / > tag. Example: < message-resources parameter= MessageResources / >

Question - 58 : - Why aren't the Struts tags maintained as part of the Jakarta Taglibs project ?

Answer - 58 : - Development of both products began about the same time. Leading up to the release of 1.0, it was thought better to continue to develop the taglibs alongside the controller. Now that 1.0 is out, the JavaServer Pages Standard Taglib is in active development. Once work on JSTL stabilizes, the Struts taglibs will be revisited. Tags which are not linked directly to the framework may be hosted at Jakarta Taglibs instead.

Question - 59 : - What is an “Action Class”?

Answer - 59 : - The “Action Class” is part of the “Model” and is a wrapper around the business logic. The purpose of the “Action Class” is to translate the HttpServletRequest to the business logic. To use the “Action”, we need to subclass and overwrite the execute() method. All the database and business processing is done in the “Action” class. It is advisable to perform all the database related work in the “Action” class. The ActionServlet (command) passes the parameterized class to ActionForm using the execute() method. The return type of the execute method is ActionForward which is used by the Struts Framework to forward the request to the file according to the value of the returned ActionForward object.

Question - 60 : - How can I prepopulate a form?

Answer - 60 : - The simplest way to prepopulate a form is to have an Action whose sole purpose is to populate an ActionForm and forward to the servlet or JSP to render that form back to the client. A separate Action would then be use to process the submitted form fields, by declaring an instance of the same form bean name. The struts-example example application that is shipped with Struts illustrates this design pattern nicely. Note the following definitions from the struts-config.xml file: ... <form-beans> ... <-- Registration form bean --> <form-bean name="registrationForm" type="org.apache.struts.webapp.example.RegistrationForm"/> ... </form-beans> ... <action-mappings> ... <-- Edit user registration --> <action path="/editRegistration" type="org.apache.struts.webapp.example.EditRegistrationAction" name="registrationForm" scope="request" validate="false"/> ... <-- Save user registration --> <action path="/saveRegistration" type="org.apache.struts.webapp.example.SaveRegistrationAction" name="registrationForm" input="registration" scope="request"/> ... </action-mappings> Note the following features of this approach: * Both the /editRegistration and /saveRegistration actions use the same form bean. * When the /editRegistration action is entered, Struts will have pre-created an empty form bean instance, and passed it to the execute() method. The setup action is free to preconfigure the values that will be displayed when the form is rendered, simply by setting the corresponding form bean properties. * When the setup action completes configuring the properties of the form bean, it should return an ActionForm that points at the page which will display this form. If you are using the Struts JSP tag library, the action attribute on your <html:form> tag will be set to /saveRegistration in order for the form to be submitted to the processing action. * Note that the setup action (/editRegistration) turns off validation on the form that is being set up. You will normally want to include this attribute in the configuration

Computer Contributors

krishan
Rajeev Katiyar
Yes Baroda

Share your email for latest updates

Name:
Email:

Our partners