Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Struts Interview Questions and Answers

Test your skills through the online practice test: Struts Quiz Online Practice Test

Related differences

Related differences

Struts vs JSFStruts vs SpringStruts 1 vs Struts 2
Struts 1.1 vs Struts 1.2Struts 1.2 vs Struts 1.3

Ques 36. Why are my checkboxes not being set from ON to OFF?

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.

Is it helpful? Add Comment View Comments
 

Ques 37. Can't I just create some of my JavaBeans in the JSP using a scriptlet?

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.

Is it helpful? Add Comment View Comments
 

Is it helpful? Add Comment View Comments
 

Ques 39. How do I use JavaScript to struts?

.
Struts is mainly a server-side technology. We bundled in some JSP tags to expose the framework components to your presentation page, but past that, the usual development process applies.
Interactive pages require the use of JavaScript. (That's why it was invented.) If you want things popping up or doing this when they click that, you are outside the scope of Struts and back into the web development mainstream.
You use JavaScript with Struts the same way you use with any presentation page. Since JavaScript is a client-side technology, you can use simple relative references to your scripts. If you need to fire a JavaScript from a HTML control, the Struts HTML tags have properties for the JavaScript events.
A very good JavaScript resource is Matt Kruse's site at http://www.mattkruse.com/javascript/ Do I need to implement reset and set all my form properties to their initial values?
No. You need to set checkbox properties to false if the ActionForm is being retained in session scope. This is because an unchecked box does not submit an attribute. Only checked boxes submit attributes. If the form is in session scope, and the checkbox was checked, there is no way to turn it back off without the reset method. Resetting the properties for other controls, or for a request scope form, is pointless. If the form is in request scope, everything already just started at the initial value.

Is it helpful? Add Comment View Comments
 

Ques 40. Can I use other beans or hashmaps with ActionForms?


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="order" name="searchResults" type="com.foo.bar.Order">
<tr><td><bean:write name="order" property="orderNumber"/><td>
<td>..other properties...</td></tr>
</logic:iterate>

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook