Tiles tutorial

Tiles tutorial will cover the basics and advanced features of Tiles framework. And it explains how to integrate with struts and build effective reusable view components for your web application

Pre-requisite

  • Anyone who has good knowledge on JSP, Servlets, Struts can easily understand the steps described in this tutorial
  • The tutorials are based on “Tomcat” webserver and setting up the struts application on Tomcat is already explained. click here to see “setting up struts application on tomcat server”

Tiles Framework

Tiles are like individual visual component (For example :think of one web page with contains header, menu, body, footer. and each of them are viewed as seperate component or tile). And using Tiles Framework, you can develop a web page by including different tiles or component. Tile layout is a JSP page which defines the layout or defines the look and feel and where each tile (for example header, body, footer ) are placed.

With the understanding of Tiles and Tile Layout we can go ahead developing our first Tile application

Tile Layout in detail

Take a example of webpage , You can find components Header, Body, Footer (some cases you can find Menu as well), We had taken example of three components Header, Body, Footer for simplicity which is represented in following figure

Tiles

If you carefully view these you can easily figure out there are few common components for whole web application, Header and Footer and the only text varies will be in body component. Instead of writing header , and footer on each and evey JSP page, how good will it be having common tile for those components and include in all the pages. Which will save lots of effort of writing same HTML in every page and in case of any change in Header or Footer, it will be changed only one common component instead of touching each and every page.

Building Tile Layout

Lets take example of Product listing and we will be building Tiles framework for the same with header and footer which is defined as per the figure above.

Writting ItemColor Action

In previous slide we had seen how struts uses achieve MVC architecture and Action is the component where our business logic resides, in this slide we will be writing business logic for our application.

package com.strutstutorial.colors;import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;public class ItemColorAction extends Action {public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
ItemColorForm itemColorForm=(ItemColorForm)form;
String selectedColor=itemColorForm.getColor();
ArrayList itemsList= new ArrayList();
ActionErrors errors= new ActionErrors();
if(selectedColor!=null&&!selectedColor.equalsIgnoreCase("")){
if(selectedColor.equalsIgnoreCase("red")){
itemsList.add("Suitcase");
itemsList.add("Cup");
}else if(selectedColor.equalsIgnoreCase("blue")){
itemsList.add("Pen");
itemsList.add("Door");
}else if(selectedColor.equalsIgnoreCase("green")){
itemsList.add("Carpet");
itemsList.add("Table");
}else{
itemsList.add("item1");
itemsList.add("item2");
}
}
if(selectedColor!=null&&!selectedColor.equalsIgnoreCase("")){
request.setAttribute("selectedColor",selectedColor);
}else{
request.setAttribute("selectedColor","empty");
}
if(itemsList!=null){
request.setAttribute("itemsList",itemsList);
}
ActionForward forward= mapping.findForward("success");
return forward;
}
}

Writting Action Form

Your form may contain lot of information as an input from the user when a request is made. This data can be passed as an instance of ActionForm class to the serving Action class for processing. The ActionForm class is subclassed to create a Java bean that defines getter and setter method for the fields defined in the input form.

package com.strutstutorial.colors;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;public class ItemColorForm extends ActionForm {private String color=null;public void reset(){
color=null;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}

}

The ActionForm subclass follows standard Java Beans convention to name the methods for a property.