Interview Questions

Struts Tiles Integration

In this article let's discuss about integrating Tiles with Struts framework.

In a Struts based web application, Tiles framework is used for managing view (JSP) related information. In the Struts MVC architecture, when the request is returned from Action class, it is forwarded to Tiles framework with a defined name for the view specified in the tiles-defs.xml (tiles configuration file).

Have a look at the struts-config.xml file as follows.

struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<struts-config>
<form-beans>
        <form-bean name="loginForm"
           type="techdive.ui.web.action.form.LoginForm" />
<action-mappings>
        <action path="/login"
           type="techdive.ui.web.action.LoginAction" name="loginForm"
           parameter="method" scope="request" validate="false"
           input="/Index.jsp">
            <forward name="LOGON_SUCCESS" path="tdd.welcome"
               redirect="true" />
        </action>
</action-mappings>
<controller
       processorClass="org.apache.struts.tiles.TilesRequestProcessor" />
        <plug-in className="org.apache.struts.tiles.TilesPlugin">
        <set-property property="definitions-config"
           value="/WEB-INF/tiles-defs.xml" />
        <set-property property="moduleAware" value="true" />
        <set-property property="definitions-parser-validate"
           value="true" />
    </plug-in>
</struts-config>
 

When the request is returned from above action class techdive.ui.web.action.LoginAction, it is forwarded to Tiles framework with defined name "tdd.welcome". Now the Tiles framework looks for the name in tiles-defs.xml to display the JSP.

tiles-defs.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE tiles-definitions PUBLIC
      "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
      "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">

<tiles-definitions>

    <definition name="tdd.basicTemplate" path="jsp/Template.jsp">
        <put name="header" value="jsp/header.jsp" />
        <put name="body" value="" />
        <put name="footer" value="jsp/footer.jsp" />
        <put name="module" value=" " />
    </definition>
   
    <definition name="tdd.login" extends="tdd.basicTemplate">
        <put name="header" value=" " />
        <put name="body" value="jsp/login.jsp" />
          <put name="footer" value=" " />
    </definition>
    </tiles-definitions>

Using Tiles, we can create a layout for our web application. In the above file, the definition tag with name "tdd.basicTemplate" defines the layout for all the web pages with header, body and footer.

In header.jsp, enter JSP Code that will be common across all the web pages in your web application.

For example, Web application title, menus, user logged in, logout links, about us links, JSP imports, tag library imports etc.

In footer.jsp, provide information like, copyrights message, version etc.,

Since we have created, the layout template for our web page, the same can be extended for all the web pages in the application, providing only the body part.

In this way, the JSP related information is handled in tiles-defs.xml, and struts-config.xml refers to jsps using defined names. So, if the JSP names or the JSP file changes then the changes need to be done only in tiles-defs.xml, providing flexibility.

This is how Tiles framework is integrated with Struts.