Search This Blog

Monday 18 August 2014

Steps to Configure Tiles in Spring & Struts2 Applications



  Steps to Configure Tiles in Spring Application
  

Step 1: Add tiles Jars to lib folder
Step 2:  Create tiles.xml file

<?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="baseLayout" template="/jsp/baselayout.jsp">
        <put name="title" value="" />
        <put name="header" value="/jsp/header.jsp" />
        <put name="leftPannel" value="/jsp/leftPannel.jsp" />
        <put name="body" value="" />
        <put name="footer" value="/jsp/footer.jsp" />
    </definition>
    <definition name="homepage" extends="baseLayout">
        <put name="title" value="Welcome" />
        <put name="body" value="/jsp/welcome.jsp" />
    </definition>
    <definition name="searchpage" extends="baseLayout">
        <put name="title" value="Welcome" />
        <put name="body" value="/jsp/searchpage.jsp" />
    </definition>
</tiles-definitions>


Step 3: Spring beans configuration

 <!-- Configures the tiles defintions file -->
    <bean id="tilesConfigurer"
        class="org.springframework.web.servlet.view.tiles.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tile/tiles-def.xml</value>
            </list>
        </property>
    </bean>

    <!-- view mappings from localized classpath files -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass">
            <value>
                org.springframework.web.servlet.view.tiles.TilesJstlView
            </value>
        </property>
    </bean>

Step 4: Create baselayout.jsp and create all required jsp pages

<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><tiles:insert name="title" ignore="true" /></title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%" align="center">
    <tr height="15%">
        <td><tiles:insert name="header" />
        </td>
    </tr>
    <tr height="75%">
    <td>
        <TABLE border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
        <TR>
            <td height="80%" width="20%"><tiles:insert name="leftPannel" /></td>
            <td width="80%">
                <table>
                    <tr  height="100%">
                        <td>
                            <tiles:insert name="body" />
                        </td>
                    </tr>
                </table>
            </td>
        </TR>
        </TABLE>
    </td>       
    </tr>
    <tr height="10%">
        <td><tiles:insert name="footer" />
        </td>
    </tr>
</table>
</body>
</html>







Step 5: Create & Return model and view object with tiles definition in controller class

            return new ModelAndView("homepage");

Steps to Configure Tiles in Struts 2 Application

Step 1: Add tiles Jars to lib folder


Step 2: web.xml configuration

   <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>

    <context-param>
        <param-name>tilesDefinitions</param-name>
        <param-value>/WEB-INF/tiles.xml</param-value>
    </context-param>

Step 3: Struts 2 action configuration 

<package name="default" namespace="/" extends="struts-default">
<result-types>
    <result-type name="tiles"
        class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name="ajaxRequestForJsp">
            <result name="successtype="tiles">/ajaxRequestForJsp</result>
</action>
</package>

Step 4:  Create tiles.xml file

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE tiles-definitions PUBLIC       
"-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"       
"http://tiles.apache.org/dtds/tiles-config_2_0.dtd">
<tiles-definitions>
    <definition name="baseLayout" template="/jsp/baselayout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/jsp/header.jsp" />
        <put-attribute name="leftPannel" value="/jsp/leftPannel.jsp" />
        <put-attribute name="contentbody" value="" />
        <put-attribute name="footer" value="/jsp/footer.jsp" />
    </definition>
    <definition name="/ajaxRequestForJsp" extends="
baseLayout">
        <put-attribute name="
contentbody" value="/jsp/ajaxJspResponse.jsp" />
    </definition>
</tiles-definitions>  


 Step 5: Create baselayout.jsp and create all required jsp pages


<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="css/ajaxdemocss.css" rel="stylesheet" type="text/css" />
<SCRIPT LANGUAGE="JavaScript" src="javascript/ajax_javascript.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" src="javascript/jquery-1.8.1.js"></SCRIPT>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%" align="center">
    <tr height="15%">
        <td><tiles:insertAttribute name="header" />
        </td>
    </tr>
    <tr height="75%">
    <td>
        <TABLE border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
        <TR>
            <td height="80%" width="10%"><tiles:insertAttribute name="leftPannel" /></td>
            <td width="90%">
                <table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
                <tr height="70%"><td><tiles:insertAttribute name="contentbody" /></td></tr>
                </table>
            </td>
        </TR>
        </TABLE>
    </td>       
    </tr>
    <tr height="10%">
        <td><tiles:insertAttribute name="footer" />
        </td>
    </tr>
</table>
</body>
</html>