Saturday 5 May 2012

Struts interview questions 7


31.What is DynaActionForm?
A specialized subclass of ActionForm that allows the creation of form beans with dynamic sets of properties (configured in configuration file), without requiring the developer to create a Java class for each type of form bean.

32.What are the steps need to use DynaActionForm?
Using a DynaActionForm instead of a custom subclass of ActionForm is relatively straightforward. You need to make changes in two places:
  • In struts-config.xml: change your <form-bean> to be an org.apache.struts.action.DynaActionForm instead of some subclass of ActionForm
<form-bean name="loginForm"type="org.apache.struts.action.DynaActionForm" >
   
<form-property name="userName" type="java.lang.String"/>
 
   <form-property name="password" type="java.lang.String" />
</form-bean>

In your Action subclass that uses your form bean:
    • import org.apache.struts.action.DynaActionForm
    • downcast the ActionForm parameter in execute() to a DynaActionForm
    • access the form fields with get(field) rather than getField()

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;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;


import org.apache.struts.action.DynaActionForm;

public class DynaActionFormExample extends Action {
 
public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
            throws Exception {            
 
DynaActionForm loginForm = (DynaActionForm) form;
                ActionMessages errors = new ActionMessages();       
       
if (((String) loginForm.get("userName")).equals("")) {
            errors.add(
"userName", new ActionMessage(
                           
"error.userName.required"));
        }
       
if (((String) loginForm.get("password")).equals("")) {
            errors.add(
"password", new ActionMessage(
                           
"error.password.required"));
        }
        ...........

33.How to display validation errors on jsp page?
<html:errors/> tag displays all the errors. <html:errors/> iterates over ActionErrors request attribute.

34.What are the various Struts tag libraries?
The various Struts tag libraries are:
  • HTML Tags
  • Bean Tags
  • Logic Tags
  • Template Tags
  • Nested Tags
  • Tiles Tags
35.What is the use of <logic:iterate>?
<logic:iterate> repeats the nested body content of this tag over a specified collection.

<table border=1>
 
<logic:iterate id="customer" name="customers">
  
 <tr>
     
<td><bean:write name="customer" property="firstName"/></td>
     
<td><bean:write name="customer" property="lastName"/></td>
     
<td><bean:write name="customer" property="address"/></td>
  
</tr>
 
 </logic:iterate>
</table> 

No comments:

Post a Comment