• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

Struts Interview Questions Answers

Question - 1 : - Where can I get a copy of Struts?

Answer - 1 : - The best place to download Struts is at struts.apache.org. The nightly builds are very stable, and recommended as the best place to start today.

Question - 2 : - How can I create a wizard workflow?

Answer - 2 : - The basic idea is a series of actions with next, back, cancel and finish actions with a common bean. Using a LookupDispatchAction is reccomended as it fits the design pattern well and can be internationalized easily. Since the bean is shared, each choice made will add data to the wizards base of information. A sample of struts-config.xml follows: < form-beans> <form-bean name="MyWizard" type="forms.MyWizard" /> </form-beans> <!-- the first screen of the wizard (next action only available) --> <!-- no validation, since the finish action is not available --> <actions> <action path="/mywizard1" type="actions.MyWizard" name="MyWizard" validate="false" input="/WEB-INF/jsp/mywizard1.jsp"> <forward name="next" path="/WEB-INF/jsp/mywizard2.jsp" /> <forward name="cancel" path="/WEB-INF/jsp/mywizardcancel.jsp" /> </action> <!-- the second screen of the wizard (back, next and finish) --> <!-- since finish action is available, bean should validated, note validation should not necessarily validate if back action requested, you might delay validation or do conditional validation --> <action path="/mywizard2" type="actions.MyWizard" name="MyWizard" validate="true" input="/WEB-INF/jsp/mywizard2.jsp"> <forward name="back" path="/WEB-INF/jsp/mywizard1.jsp" /> <forward name="next" path="/WEB-INF/jsp/mywizard3.jsp" /> <forward name="finish" path="/WEB-INF/jsp/mywizarddone.jsp" /> <forward name="cancel" path="/WEB-INF/jsp/mywizardcancel.jsp" /> </action> <!-- the last screen of the wizard (back, finish and cancel only) --> <action path="/mywizard3" type="actions.MyWizard" name="MyWizard" validate="true" input="/WEB-INF/jsp/mywizard3.jsp"> <forward name=&q

Question - 3 : - Why doesn't the focus feature on the <html:form> tag work in every circumstance?

Answer - 3 : - Unfortunately, there is some disagreement between the various browsers, and different versions of the same browser, as to how the focus can be set. The <html:form> tag provides a quick and easy JavaScript that will set the focus on a form for most versions of most browsers. If this feature doesn't work for you, then you should set the focus using your own JavaScript. The focus feature is a convenient "value-add" -- not a core requirement of the tag. If you do come up with a JavaScript that provides the final solution to this project, please post your patch to this Bugzilla ticket.

Question - 4 : - Resources under WEB-INF

Answer - 4 : - 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 - 5 : - Does Struts include its own unit tests?

Answer - 5 : - Struts currently has two testing environments, to reflect the fact that some things can be tested statically, and some really need to be done in the environment of a running servlet container. For static unit tests, we use the JUnit framework. The sources for these tests are in the "src/test" hierarchy in the source repository, and are executed via the "test.junit" target in the top-level build.xml file. Such tests are focused on the low-level functionality of individual methods, are particularly suitable for the static methods in the org.apache.struts.util utility classes. In the test hierarchy, there are also some "mock object" classes (in the org.apache.struts.mock package) so that you can package up things that look like servlet API and Struts API objects to pass in as arguments to such tests. Another valuable tool is Struts TestCase which provides a useful harness for Action classes that can be used with JUnit or Cactus.

Question - 6 : - Will the Struts tags support other markup languages such as WML  ?

Answer - 6 : - Struts itself is markup neutral. The original Struts taglibs are only one example of how presentation layer components can access the framework. The framework objects are exposed through the standard application, session, and request contexts, where any Java component in the application can make use of them. Markup extensions that use Struts are available for Velocity and XLST, among others. A new Struts tag library for Java Server Faces is also in development. For more about using WAP/WML with Struts see the article WAP up your EAserver.

Question - 7 : - How you will enable front-end validation based on the xml in validation.xml?

Answer - 7 : - The < html:javascript > tag to allow front-end validation based on the xml in validation.xml. For example the code: < html:javascript formName=logonForm dynamicJavascript=true staticJavascript=true / > generates the client side java script for the form logonForm as defined in the validation.xml file. The < html:javascript > when added in the jsp file generates the client site validation script.

Question - 8 : - Dynamic pages using struts

Answer - 8 : - Is it possible to create the elements of a page(jsp) dynamically based on the results of a data base query, when using struts framework? If you are talking about rendering a report, then sure. The Action iteracts with the business layer/data access objects to acquire the data, and then passes it to the presentation page bundled up as a JavaBean or a collection of JavaBeans. The JSP tags (and other systems) all use reflection, so you can use whatever JavaBean you like. If you are talking about creating a dynamic data-entry form, then "not so much". Struts 1.1 supports map-backed ActionForms, but the page still needs to know what input fields are going to be needed. For a truly dynamic input form, I guess the key would be some type of tag that took a map and then generated a column of input fields. (Wouldn't work for everyone, since a lot of forms must be designed just so.) For extra credit, the entry names could (optionally) be resource keys that were used to find the label text. Text fields would be easy. Others would need some type of JavaBean with properties to tell the tag what to output. A bit of work, but obviously doable. Of course, you'd probably want to validate the form before passing it back to the database. I imagine it's possible to use the validator in a non-declarative way, but I don't know anyone whose doing that. If you can do a db query to get the information about the form, I imagine you could also do a query to get the information about validations for the form. It would probably be easier to write your own engine than adopt the validator. (It's not really that complicated to do.) People often ask about "dynamic input forms", but most of us just can't get our head around the use case. It's hard to understand what you do with the dynamic data when it comes back. Most application don't allow you to input or update an arbitrary (e.g. dynamic) set of fields.

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

Answer - 9 : - 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 - 10 : - How do I install Struts?

Answer - 10 : - To develop applications with Struts, you can usually just add the Struts JAR file to your Java development environment. You can then start using the Struts classes as part of your own application. A blank Struts application (in the webapps directory, open struts-blank.war) is provided, which you can just copy to get a quick-start on your own brainchild. Since the full source code for Struts is available, we also provide complete instructions for compiling your own Struts JAR from scratch. (This is actually easier than it looks!) Your Struts application can usually be deployed using a standard WAR file. In most cases, you simply deposit the WAR file on your application server, and it is installed automatically. If not, step-by-step installation instructions for various servlet containers are available.

Computer Contributors

Rajeev Katiyar
Yes Baroda

Share your email for latest updates


Our partners