Common Interview Questions On Struts:

Q. What are the methods in a Struts Action Form?

An action form defines properties that have a one to one mapping with the html form elements in the JSP page. The Action Form needs to implement public getters and setters for these properties. Later versions of Struts do not require both a getter and setter for html form elements
Optional methods:
1. public ActionErrors validate(ActionMapping, HttpServletRequest req):
This method can be implemented when the ActionForm extends ValidatorForm. It is usually used with Java based validation and when complex validations that cannot be handled by validation.xml are involved. If a form extends ValidatorForm and does not provide the validate() method, the superclass validate() is inherited from the parent ValidatorForm that allows validation using validation.xml to be implemented. For validation involving both validation.xml and code within validate() one needs to implement the validate() method and configure validation.xml as well.
2. The reset() method:
The reset method allows one to set the values of all properties in the ActionForm to default values. It also allows one to initialize ActionForm properties within the body of the method. However with DynaActionForms reset() sets all properties to default values and does not allow one to initialize ActionForm properties.

Q What is the sequence of calls when an ActionForm is called by the ActionServlet?

The reset() method is called before the ActionForm is populated. Following this the setter methods are called to populate the ActionForm. Validate() is called if the ActionForm extends ValidatorForm.

Q. Describe the control flow when a request is submitted to a Struts application.

1. When the ActionServlet receives a request, the first thing it does is to check the ActionMappings defined in struts-config.xml to check for a matching path. ActionMappings are JavaBeans that Struts creates when reading the struts-config.xml at application startup.
2. Next the ActionServlet checks for the name attribute in the ActionMapping for the FormBean mapped to this ActionMapping. The corresponding ActionForm is instantiated and its setter methods are called to populate the ActionForm.
3. The type attribute is checked to instantiate the Action class and the perform()/execute() method is called on it. A reference to the ActionForm is passed as a parameter to the perform()/execute() method.
4. Depending on the outcome of perform()/execute() ActionMapping.findForward() is called which uses a single String attribute that matches one of the “name” attributes of the nested “forward” tags within the ActionMapping described in struts-config.xml. The “path” attribute of the “forward” tag determines the url to which the flow of control is altered to generate the response. An ActionForward can also be created dynamically within the Action class to specify the url to which the flow of control needs to be passed.

Q. Can an ActionMapping in struts-config.xml not have an Action class associated with it at all?

Yes. When an ActionMapping needs to directly transfer the flow of control to a JSP page without any processing to be done, the ActionForm need not be specified. In that case the “forward” attribute of the ActionMapping is used. “type” (which associates an Action class to the Action Mapping) and “forward” elements of an ActionMapping are mutually exclusive.

Q. Can a Struts application have multiple ActionServlets?

No. A single Struts application can have only a single ActionServlet associated with it. This is the Front Controller pattern. When the ActionServlet that acts as the FrontController in the Struts framework, receives a request it routes it to a handler class that performs business operations, usually using helper classes. The ActionServlet determines which business operations are related to which request by looking up the ActionMappings in the struts-config.xml. The multi-threaded handler class (Action class) handles business operations by calling helper classes. The handler class is passed the specifics of each request used in the operation.
When the Action class returns, the ActionServlet may have to lookup an ActionForward associated with the related mapping and pass the response to the url specified in it.

Q. What are the changes to be made to web.xml to configure Struts? Compare this with how the web.xml is modified to configure JSF.

A snippet in web.xml to configure the ActionServlet looks like this:

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.matty</url-pattern>
    <!--Could also be <url-pattern>/matty/*</url-pattern -->
</servlet-mapping>

When the parameter 1 is specified in the load-on-startup tag, the ActionServlet is the first servlet to be loaded on application startup. When 2 is specified other servlets with a values of 1 are loaded initially. Such servlets may perform initialization operations needed to be done before the ActionServlet is invoked by the container.

web.xml also has to define taglibs used by your Struts application. A snippet follows:

<taglib>
    <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>

Additional configuration for nested taglib, tiles taglib may also be defined in web.xml when needed.

A typical configuration in web.xml for JSF looks like this:

<context-param>
    <param-name>javax.faces.CONFIG_FILES</param-name>
    <param-value>/WEB-INF/faces-config.xml</param-value>
</context-param>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>

Q. Can a Struts application have multiple struts-config.xml files?

Yes. The following is an example of how separate struts-config.xml may be configured in web.xml for “matt” and “tizz” modules in addition to the default module.

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
    <param-name>config</param-name>
    <param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
 
<init-param>
    <param-name>config/matt</param-name>
    <param-value>/WEB-INF/struts-config-matt.xml</param-value>
</init-param>
<init-param>
    <param-name>config/tizz</param-name>
    <param-value>/WEB-INF/struts-config-tizz.xml</param-value>
</init-param>
……
</servlet-mapping>

Q How do you handle errors in Struts?

The validate() method of the ActionForm returns an ActionErrors object.
A snippet:

ActionErrors errors = new ActionErrors();
errors add(ActionErrors.GLOBAL_ERROR), new ActionError(my.error));
return errors;

An Action class can also generate ActionErrors.
A snippet:

ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR), new ActionError(my.error));
saveErrors(request, errors);
return new ActionForward(mappings.getInput());

The errors in the ActionErrors object are displayed using the html:errors tag in the forwarded JSP.

Q What are DynaActionForms and DynaValidatorForms?

DynaActionForms allow FormBean creation by specifying them directly in the struts-config.xml. They cannot have reset() or validate() methods. All FormBean properties are set to default values before the FormBean is populated, without allowing initialization of FormBean properties.
DynaValidatorForms are similar to DynaActionForms but they use the validator framework by allowing properties to be validated using validation.xml settings.

Q Describe some common methods used to populate custom JavaBeans (Value Objects) from ActionForms.

1. The BeanUtils calss has a describe() method that returns a Map of a JavaBean’s public properties. It also offers a corresponding populate() method that will set the public properties of a JavaBean from a Map.

public class TestVO
{
private Map map;
//getters and setters for map
}

When we go from FormBean form to VO testVO

Map map = BeanUtils.describe(form);
testVO.setMap(map);

When we go from VO testVO to FormBean form

Map map = testVO.getMap();
BeanUtils.populate(form, map);

2. You can use copyProperties() of BeanUtils class to populate your FormBean from any other bean using the Reflection API:

BeanUtils.copyProperties(myBean,myForm);
myOperation(myBean);
BeanUtils.copyProperties(myForm, MyBean);

The code in this snippet transfers the String properties in myForm to native types in myBean. It then transfers the native types in myBean back to the all String properties in myForm after business logic processing.
Converting non-native types such as Dates may requires a bit more effort.

3. Maps can be used as ActionForm properties. But a values from a Map is treated differently from a regular JavaBean property.

public class MyForm extends ActionForm {
	private Map map = null;

	// setters and getters for map
	public void setValue(String key, Object value) {
		getMap().put(key, value);
	}

	public void getValue(String key) {
		return getMap().get(key);
	}
}

One can access the elements of the map in the jsp page thus:

<html:text property=”value(someProperty)”/>

OR

<bean:write name=”myForm” property= “value(someProperty)”/>

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: