Only Institute
to provide recording daily after class to students. you can practice whole day under faculty guidance. where test and mockup tests are taken regularly. provides all mobile nos of old students on website.

Tiles Basics and Integration

Review of Tiles

  • A free open-sourced templating framework for modern Java applications. Based upon the Composite pattern it is built to simplify the development of user interfaces.
  • For complex web sites it remains the easiest and most elegant way to work alongside any MVC technology.

Generic questions

  • What is Tiles?
  • Tiles is a view layer framework for Java EE applications that allows to separate pages into reusable pieces, according to the Composite View design pattern.
  • Who is the creator of Tiles?
  • Tiles has been created by Cédric Dumoulin, who had the idea and integrated it into Struts.
  • Why Tiles has this name?
  • We did not ask Cédric, but I think that he thought about tiling the pages. A tile here, a tile there, and you have your page complete
  • Where do I start learning Tiles?
  • First of all you have to get started. Then check out the Tutorial
  • How do I get support for Tiles?
  • You can subscribe to the users mailing list and ask your questions there.
  • How can I contribute?
Contributions and patches are easiest done via github.

Technical questions

  • I received an ""Attribute 'name' not found." exception. What's that?
    Probably you forgot to fill an attribute of a template, or you are forwarding directly to a template page with at least an attribute. Template with attributes cannot be forwarded alone without filling their attributes. Either create a definition, or fill manually the attributes in the attribute context.

  • I received a NoSuchDefinitionException, but I am sure that the definition is correct. What happened?
    Usually it is a problem in loading definitions. See the startup of your application: you'll probably see an exception stack trace. That is the "real" problem.

  • Can I have a "definition in a definition"? How can I do it?
    Yes. See Nesting and extending.

  • I filled an attribute of a template, but it did not get in an inner template. What happened?
    Attributes must be filled in the correct template. In other words, there are no "pass-through" attributes, that you fill in the master template and get filled in the inner ones.
    In this case, usually you have to create a new definition extending from an existing one, fill the attribute in the correct template and assign the new definition as an attribute to the main template.

  • How do I refresh a single tile without changing the other ones?
    You are getting confused between frames and tiles. Although frames and tiles can be combined, in general tiles are used in place of frames. In this case the server combines all the tiles into a single web page (without any frames), so you can't refresh just one tile. You have to refresh the whole page

Struts-Tiles relationship

Is Tiles2 compatible with the version of tiles I've been using with Struts?
No. Struts-Tiles is an ancestor of Tiles 2 which grew out of the Struts project. This version is a total redesign which makes the framework easier to embed within frameworks such as Struts and Shale.

If you're a casual user, you'll probably only notice the differences in the tag libraries and in Tiles definition files. Many of them have been renamed for clarity sake and you can upgrade to Tiles2 by simple doing a global find and replace.

If you're integrating Tiles into a framework or using it for more advanced features, then you'll probably notice a world of difference. Tiles now runs as a container which encapsulates it's feature set and there is a much cleaner API. If you find that this api doesn't suffice for you, please hop on the developer list and help us flush out the next generation of Tiles!

  • How can I migrate from Struts-Tiles?
    We have prepared a Migration guide.

  • Why should I move from Struts-Tiles to Tiles 2?
    Tiles 2 can be used outside of Struts: it has been integrated into Struts 1, Struts 2 and Shale. And Tiles 2 is supported by a passionate team.

  • Why did you change everything in Tiles 2?
    To improve learning of Tiles. After the refactoring, the APIs, the TLD and the DTD are clearer than before.

The Composite View Pattern

All websites have something in common: they are made of pages that share similar structures. The pages share the same layout, while each page is made of different independent pieces, but always placed in the same position across all the site.

The Composite View pattern formalizes this typical use, by allowing to create pages that have a similar structure, in which each section of the page vary in different situations.

  • How the pattern works
    To understand this pattern, let's take an example. In the following picture you can see a typical structure of a web page Java By Kiran

    This structure is called "Classic Layout". The template organizes the page according to this layout, putting each "piece" in the needed place, so that the header goes up, the footer down, etc.

    It can happen that, for example clicking on a link, it is needed to change only a part of the page, typically the body.

    Java By Kiran

    As you can see, the pages are different, but their difference is only in the body part. Note that, however, the pages are distinct, it is not like a refresh of a frame in a frameset!

    Using the composite view pattern, the other part of the page have been reused, and the layout consistence has been preserved.

  • The role of the View Helper
    Each piece of the composed page can have a "view helper". This pattern allows the preparation of the data to be displayed in a consistent way for the page piece itself, for example to create a menu

  • Composite View vs. Decorator
    Tiles is a composite view framework: it allows to reuse page pieces across the application. But another approach to achieve the same result is using the Decorator pattern. For example, Sitemesh is based on the Decorator pattern. Instead of creating a template and organizing the pieces together, the Decorator pattern (in this case) takes a simple HTML page, transforms it adding the missing pieces (in our example, adding header, footer and menu) and finally renders it.

  • Here you can find a comparison table between the two patterns.

Lets design struts 2 web application without tiles in eclipse.

  • jars required – add following jars into build path of ecipse

  • Java By Kiran

  • Project Preview in eclipse

  • Java By Kiran

  • login.jsp



  • 
    < %@ 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">
    < html >	
    < head >
    < meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    < title > LoginPage < / title >
    < / head >
    < body >
    < br > < br > < br > < br > < br > < br >
    	< center >
    		< form action="login.action" >
    		UserName : < input type="text" name="uname" > < br > < br >
    		Password : < input type="password" name="pass" > < br> < br >
    		< input type="submit" value="Login" >
    		< / form >
    	< / center >
    < / body >
    < / html >
    


  • header.jsp


  • 
    < H1 align="center">Application Title< / H1 >
     < hr >
    


  • footer.jsp


  • 
    < hr > < br >
    < h3 align="center">Copyrights© Coralsoft Technologies< / h3 >
    


  • menu.jsp


  • 
    < h3 > < a href="home.action">Home< / a > | < a href="profile.action" > Profile < / a > |	< / h3 >
    < hr >
    


  • success.jsp


  • 
    < % @ 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" >
    < html >
    < head >
    < meta http-equiv="Content-Type" content="text/ html; charset=ISO-8859-1" >
    < title>Home Page
    < / head>
    < body >
    < %@include file="header.jsp" % >
    < %@include file="menu.jsp" % >
    
    
    Welcome , User

    < br > < br > < br > < br > < br > < br > < %@include file="footer.jsp" % > < / body > < / html >


  • profile.jsp


  • 
    < %@ 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" >
    < html >
    < head >
    < meta http-equiv="Content-Type" content="text / html; charset=ISO-8859-1" >
    < title > Profile < / title >
    < / head >
    < body >
    < %@include file="header.jsp" % >
    < %@include file="menu.jsp" % >

    Name : ABC < br > UserName : arj@12334 < br > Password : admin321sdasd < br > < br > < br > < br > < br > < br > < br > < %@include file="footer.jsp" % > < / body > < / html >


  • web.xml


  • 
    < ? xml version="1.0" encoding="UTF-8"? >
    < web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation= " http : //java.sun.com/xml/ns/javaee http : //java.sun.com/xml/ns/javaee/web-app_3_0. xsd " 
    id = " WebApp_ID " version = " 3.0 " >
      < display-name > Struts2WithoutTiles < / display-name >
      < welcome-file-list >
            < welcome-file > login.jsp < / welcome-file >
      < / welcome-file-list >
      
      
      < filter >
      		< filter-name > f1 < / filter-name >
      		< filter-class > org.apache.struts2.dispatcher.FilterDispatcher < / filter-class >
      < / filter >
      
      < filter-mapping >
      		< filter-name > f1 < / filter-name >
      		< url-pattern > /* < / url-pattern >
      < / filter-mapping >
      
    < / web-app >
    


  • struts.xml


  • 
    < ? xml version="1.0" encoding="UTF-8"? >
    < ! DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http: //struts.apache.org/dtds/struts-2.0.dtd" >
    < struts >
    	
    	< package name="myPackage" extends="struts-default" >
    		
    		< action name="login" class="com.arjun.action.LoginAction" >
    			< result name="success">success.jsp < / result >
    			< result name="error">error.jsp < / result >
    		< / action >
    	    
    	    < action name="home" >
    	    	< result > success.jsp < / result >
    	    < / action >
    	    < action name="profile" >
    	    	< result >profile.jsp < / result >
    	    < / action >	
    	 
    	< / package >
    		
    
    < / struts >
    


  • LoginAction.java


  • 
    package com.arjun.action;
    
    public class LoginAction {
    
    	public String execute(){
    		
    		
    		return "success";
    		
    	}
    }
    


    Java By Kiran

    Lets design same above struts 2 web application with tiles in eclipse.

    ( just integrate tile in above application )
  • jars required –(struts 2 + tiles)
  • add following jars into build path of ecipse

  • Java By Kiran

  • login.jsp


  • 
    < %@ 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" >
    < html >	
    < head >
    < meta http-equiv="Content-Type" content="text / html; charset=ISO-8859-1">
    < title > LoginPage < / title >
    < / head >
    < body >
    < br > < br > < br > < br > < br > < br >
    	< center >
    		< form action="login.action" >
    		UserName : < input type="text" name="uname" > < br > < br >
    		Password : < input type="password" name="pass" > < br > < br >
    		< input type="submit" value="Login" >
    		< / form >
    	< / center >
    < / body >
    < / html >
    


  • header.jsp


  • 
    < H1 align="center">Application Title < / H1 >
     < hr >
    


  • footer.jsp


  • 
    < hr > < br >
    < h3 align="center">Copyrights© Coralsoft Technologies< / h3 >
    


  • menu.jsp


  • 
    < h3 > < a href="home.action">Home< / a > | < a href="profile.action">Profile < / a > |	< / h3 >
    < hr >
    


    success.jsp

    Welcome , User

    profile.jsp

    Name : ABC < br > UserName : arj@12334 < br > Password : admin321sdasd < br >

  • baselayout.jsp


  • 
    < %@ taglib uri="http: //tiles.apache.org/tags-tiles" prefix="tiles"  %>
    < %@ 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" >
    < html >
    < head >
    < meta http-equiv="Content-Type" content="text / html; charset=ISO-8859-1" >
    < title > < tiles:getAsString name="title" / > < / title >
    < / head >
    < body >
    < tiles:insertAttribute name="header" / >
    
    < tiles:insertAttribute name="menu" / >
    
    < tiles:insertAttribute name="body" / >
    
    < br > < br > < br > < br > < br > < br >
    < tiles:insertAttribute name="footer" / >
    < / body >
    < / html >
    


  • web.xml


  • 
    < ? xml version="1.0" encoding="UTF-8"? >
    < web-app xmlns:xsi="http:// www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation= " http:// java.sun.com/xml/ns/javaee http: //java.sun.com/xml/ns/javaee/web-app_3_0.xsd" i d="WebApp_ID" version="3.0">
      < display-name > Struts2WithTiles < / display-name >
      < welcome-file-list >
        < welcome-file > login.jsp < / welcome-file >
      < / welcome-file-list >
      
      < filter >
        < filter-name > f1 < / filter-name >
        < filter-class > org.apache.struts2.dispatcher.FilterDispatcher < / filter-class >
      < / filter >
      < filter-mapping >
        < filter-name > f1< / filter-name >
        < url-pattern > /*< / url-pattern >
      < / filter-mapping>
      
      < listener >
      		< description>used for tiles < / description >
      		< listener-class>org.apache.struts2.tiles.StrutsTilesListener< / listener-class >
      < / listener >
    < / web-app >
    


  • tiles.xml


  • 
    < ? 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="/baselayout.jsp" >
     	    	
     	    < put-attribute name="title" value="WebApplication" / >
     		< put-attribute name="header" value="/header.jsp" / >
    	 	< put-attribute name="menu" value="" / >
     		< put-attribute name="body" value="" / >
     		< put-attribute name="footer" value="/footer.jsp" / >
     		
     	< / definition >
     
     	< definition name="home.tiles" extends="Baselayout" >
     		< put-attribute name="title" value="Home Page" / >
     		< put-attribute name="menu" value="/menu.jsp" / >
     		< put-attribute name="body" value="/success.jsp" / >
     	< / definition >
     
     	< definition name="profile.tiles" extends="Baselayout" >
     		< put-attribute name="title" value="Profile Page" / >
     		< put-attribute name="menu" value="/menu.jsp" / >
     		< put-attribute name="body" value="/profile.jsp" / > 
     	< / definition >
     < / tiles-definitions >
    


  • struts.xml


  • 
    < ? xml version="1.0" encoding="UTF-8"? >
    < ! DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http: //struts.apache.org/dtds/struts-2.0.dtd" >
    
    < struts >
    	
    	< package name="myPackage" extends="struts-default" >
    		
    		< result-types >
    		 < result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
    		< / result-types >
    		
    		
    		< action name="login" class="com.arjun.action.LoginAction" >
    			< result name="success" type="tiles" > home.tiles < / result >
    			< result name="error" >error.jsp < / result >
    		< / action >
    	    
    	    < action name="home" >
    	    	< result type="tiles" > home.tiles < / result >
    	    < / action >
    	    < action name="profile" >
    	    	< result type="tiles" > profile.tiles < / result >
    	    < / action >	
    	 
    	< / package >
    		
    
    < / struts > 
    


  • LoginAction.java


  • 
    package com.arjun.action;
    
    public class LoginAction {
    
    	public String execute(){
    		
    		
    		return "success";
    		
    	}
    }
    


    Java By Kiran