Tutorial of Simple JSP Tiles application without Struts

There are very few resources available on internet which explains step by step integration of tiles in simple jsp page.
Apache Tilesâ„¢ is a templating framework built to simplify the development of web application user interfaces.

Tiles allows authors to define page fragments which can be assembled into a complete page at runtime. These fragments, or tiles, can be used as simple includes in order to reduce the duplication of common page elements or embedded within other tiles to develop a series of reusable templates. These templates streamline the development of a consistent look and feel across an entire application.

Ste p1 : Download the Tiles jar file from here.

Step 2 : Add following jar files in the lib folder of the web application.

commons-beanutils-1.8.0.jar
commons-digester-2.0.jar
jcl-over-slf4j-1.5.8.jar
log4j-over-slf4j-1.6.1.jar
servlet-api.jar
slf4j-api-1.5.8.jar
slf4j-jdk14-1.5.8.jar
tiles-api-2.2.2.jar
tiles-core-2.2.2.jar
tiles-jsp-2.2.2.jar
tiles-servlet-2.2.2.jar
tiles-servlet-wildcard-2.2.2.jar
tiles-template-2.2.2.jar

Step 3: Create following jsp pages:
banner.jsp, common_menu.jsp, credits.jsp, cssPath.jsp, home_body.jsp and SideBar.jsp.

Step 4: Create a master layout page.

<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
<head>
<title><tiles:getAsString name="title" />
</title>
<tiles:insertAttribute name="cssFile"></tiles:insertAttribute>
</head>
<body>
	<div id="wrapper">
		<div id="menu">
			<tiles:insertAttribute name="menu" />
		</div>

		<div id="header">
			<tiles:insertAttribute name="header" />
		</div>

		<div id="page">
			<div id="page-bgtop">
				<div id="page-bgbtm">
					<div id="content">
						<tiles:insertAttribute name="body" />

						<div style="clear: both;">&nbsp;</div>
					</div>

					<div id="sidebar">
						<tiles:insertAttribute name="sideBar" />
					</div>

					<div style="clear: both;">&nbsp;</div>
				</div>
			</div>
		</div>

	</div>
	<div id="footer">
		<tiles:insertAttribute name="footer" />
	</div>
</body>
</html>

As you can see, tiles:insertAttribute tag is used. It will work as placeholder and will be replaced by actual pages at run time as per definition in “tiles-defs.xml

Read more on configuring tiles attributes.

Below output shows the layout.

 

Simple JSP Tiles without Struts
Simple JSP Tiles without Struts

Step 5: Now create a “tiles-defs.xml” in WEB-INF Folder, which have all the configuration.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 2.1//EN"
       "http://tiles.apache.org/dtds/tiles-config_2_1.dtd">
<tiles-definitions>
  <definition name="myapp.homepage" template="/layouts/classic.jsp">
    <put-attribute name="title" value="Simple Tiles application without Struts" />
    <put-attribute name="header" value="/tiles/banner.jsp" />
    <put-attribute name="menu" value="/tiles/common_menu.jsp" />
    <put-attribute name="body" value="/tiles/home_body.jsp" />
    <put-attribute name="footer" value="/tiles/credits.jsp" />
    <put-attribute name="cssFile" value="/tiles/cssPath.jsp" />
    <put-attribute name="sideBar" value="/tiles/SideBar.jsp" />
  </definition>
</tiles-definitions>

The attribute “name” defined in tag “tiles:insertAttribute” comes from above configuration file and replaced by the pages specified.

Step 6 : Now create the jsp page which uses the Tiles template defined above.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<tiles:insertDefinition name="myapp.homepage" />
</body>
</html>

Template name “myapp.homepage” is looked up into file “tiles-defs.xml” and configuration is applied at runtime as per definition.

Download the war file from here, change extension from “zip” to “war”.

Posted

in

by

Tags:


Related Posts

Comments

18 responses to “Tutorial of Simple JSP Tiles application without Struts”

  1. Helen Avatar
    Helen

    Hi
    Please help me change extension from “zip” to “war”.
    How do I can do?
    Thanks!

    1. jafar Avatar
      jafar

      Select the file . press F2 and and rename.

  2. Jitendra Zaa Avatar

    Simply rename it.

  3. Anoo Avatar
    Anoo

    I did all the configuration as mentioned above but I’m facing below exception. I use TilesListener to load the definitions.

    [18/11/11 12:14:42:985 GMT] 00000023 WebApp E [Servlet Error]-[test.dummy]: org.apache.tiles.definition.NoSuchDefinitionException: test.dummy
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:625)
    at org.apache.tiles.impl.BasicTilesContainer.render(BasicTilesContainer.java:321)
    at org.apache.tiles.template.InsertDefinitionModel.end(InsertDefinitionModel.java:104)
    at org.apache.tiles.jsp.taglib.InsertDefinitionTag.doTag(InsertDefinitionTag.java:278)
    at com.ibm._jsp._welcome._jspx_meth_tiles_insertDefinition_0(_welcome.java:92)
    at com.ibm._jsp._welcome._jspService(_welcome.java:68)

    1. Jitendra Zaa Avatar

      Hi Anoo,
      Please check that whether you are using any servlet or nay mapping of tiles with name “test.dummy”. Search your application for this text.

      Regards,
      Jitendra Zaa

  4. Anoo Avatar
    Anoo

    Hi Jitendra,

    The issue was because I used TilesListener which is deprecated. Issue got resolved when I used SimpleTilesListener. This listener automatically looks for tiles.xml under WEB-INF. No need to specify tiles xml location in web.xml. Thanks for your time in replying to my post.

    Regards
    Anoo

  5. Hemant Bhagwani Avatar
    Hemant Bhagwani

    Hi,I tried the above example by downloading the war and imported as TilesDemo.war and got the following error:
    java.lang.NullPointerException
    at org.apache.commons.digester.Digester.getXMLReader(Digester.java:1058)
    at org.apache.commons.digester.Digester.parse(Digester.java:1887)
    at org.apache.tiles.definition.digester.DigesterDefinitionsReader.read(DigesterDefinitionsReader.java:329)
    at org.apache.tiles.definition.dao.BaseLocaleUrlDefinitionDAO.loadDefinitionsFromURL(BaseLocaleUrlDefinitionDAO.java:276)
    at org.apache.tiles.definition.dao.CachingLocaleUrlDefinitionDAO.loadDefinitionsFromURLs(CachingLocaleUrlDefinitionDAO.java:251)

    I am using Oracle webLogic workshop 10.3
    Kindly help in tracing the error.

  6. Hemant Bhagwani Avatar
    Hemant Bhagwani

    Hi,thanks for such a quick response.
    I am a novice and gone through ur link(http://pwu-developer.blogspot….).I didnt really understand what is there in that link so i changed my IDE to eclipse,niw i m getting this error:

    Exception sending context initialized event to listener instance of class com.sun.faces.config.ConfigureListener
    com.sun.faces.config.ConfigurationException: CONFIGURATION FAILED!
    Source Document: jndi:/localhost/tilesTest/WEB-INF/faces-config.xml
    Cause: Unable to find class ‘org.apache.myfaces.tomahawk.application.jsp.JspTilesViewHandlerImpl’
    at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:214)
    at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:199)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)e problem 🙂 so i changed my IDE to eclipse now i m getting this error:

    kindly help.

  7. Jafar Ali Avatar
    Jafar Ali

    Hi what is the use of BasicTilesContainerFactory. What is the function of the class file that is in you WEB-INF/classes folder.

  8. Jafar Ali Avatar
    Jafar Ali

    I got my previous answer. I want to know how to define about the config in web.xml? Is it required? Is there any default config file name for which we don’t have to tell tiles about its config files location? Can you elaborate your tutorial more.

  9. Sharmi Avatar
    Sharmi

    Hi,I tried the above example using your war file. but its not working. i got error like this :

    org.apache.tiles.template.NoSuchAttributeException: Attribute ‘title’ not found.
    org.apache.tiles.template.DefaultAttributeResolver.computeAttribute(DefaultAttributeResolver.java:49)
    org.apache.tiles.template.GetAsStringModel.resolveAttribute(GetAsStringModel.java:178)
    org.apache.tiles.template.GetAsStringModel.start(GetAsStringModel.java:95)
    org.apache.tiles.jsp.taglib.GetAsStringTag.doTag(GetAsStringTag.java:306)
    org.apache.jsp.layouts.classic_jsp._jspx_meth_tiles_005fgetAsString_005f0(classic_jsp.java:146)
    org.apache.jsp.layouts.classic_jsp._jspService(classic_jsp.java:67)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

  10. Naveen Kumar Avatar
    Naveen Kumar

    I tried this example but when I start the project I got following error

    java.lang.ClassNotFoundException: org.apache.tiles.web.startup.simple.SimpleTilesInitializerServlet>

    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)

    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)

    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:527)

    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509)

    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:137)

    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)

    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)

    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)

    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)

    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)

    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)

    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)

    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)

    at java.util.concurrent.FutureTask.run(Unknown Source)

    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

    at java.lang.Thread.run(Unknown Source)

  11. Ravikant Avatar
    Ravikant

    Hi…I am getting NullPointer Exception..The stack trace is as follows

    java.lang.NullPointerException
    org.apache.tiles.template.InsertDefinitionModel.start(InsertDefinitionModel.java:74)
    org.apache.tiles.jsp.taglib.InsertDefinitionTag.doTag(InsertDefinitionTag.java:276)
    org.apache.jsp.Jsp.index_jsp._jspx_meth_tiles_005finsertDefinition_005f0(index_jsp.java:98)
    org.apache.jsp.Jsp.index_jsp._jspService(index_jsp.java:71)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

    Kindly help..

    1. Jitendra Zaa Avatar

      Can u confirm if all jar files are in place as explained above ?

      1. ravikant Avatar
        ravikant

        hi .I have used all the Jars stated above..still not working..same error message is displayed..do i need to configure tiles.xml in web.ml..An class listner is required….

      2. Ravikant Avatar
        Ravikant

        Do I need to specify the tiles config file in web.xml ? If not then how does application identify the tiles-defs.xml file?

      3. ravikant verma Avatar
        ravikant verma

        Hi Jitendra,

        I did the same as stated by you..but now I am getting

        org.apache.tiles.definition.NoSuchDefinitionException: firstPage

        .I have changed the name several times also..Still its not working..kindly help

        Regards’
        Ravikant

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading