In this tutorial we will discuss about the tiles and build a simple SpringMVC application that utilizes templates using the Apache Tile 3 framework. Now we will create a template version of our pages, and compare it with non-template versions of the same pages. We will split the content, style, and template of these pages logically.
What is Apache Tiles 3.0.1?
Apache Tiles is a templating framework built to simplify the development of web application user interfaces.
Apache Tiles is a popular and mostly used templating framework for java based web application. Tiles became more popular because Struts 1.x uses Tiles as its default templating framework. Spring3MVC which is an MVC framework, like Struts, also supports integration of Tiles as its templating framework.
Tiles allows developer to define page fragments(or parts) which can be assembled into a complete page at run-time. 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.
Let us see how we can integrate Spring3MVC and Tiles.
You can download Tiles binaries from here
Application Layout
A web portal have many reusable templates like header, footer, menu etc. These elements remains same in every web page to give a uniform feel and look to improve presentation of portal. But difficult part is when you need to alter these common items.
The Tiles framework solve this problem by using templatization mechanism. We create a common Header, Footer, Menu page and include this in each page. A common layout of website is defined in a central configuration file and this layout can be extended across all the web pages of the web application.
Add the following required tiles jars to WEB-INF/lib folder.
- 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-template-2.2.2.jar
In the previous chapter we run an application of CRUD operation on the Employee table using Spring3MVC and Hibernate3. Now same we will build same application using tiles configuration (or Tiles View Resolver) as view in stead of JstlView (or JSP View resolver).
Updated view of our application with using the tiles configuration look like as below diagram.
Application Structure:
EmployeeBean.java
- package com.dineshonjava.bean;
-
- /**
- * @author Dinesh Rajput
- *
- */
- public class EmployeeBean {
- private Integer id;
- private String name;
- private Integer age;
- private Long salary;
- private String address;
-
- public Long getSalary() {
- return salary;
- }
- public void setSalary(Long salary) {
- this.salary = salary;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getAge() {
- return age;
- }
- public void setAge(Integer age) {
- this.age = age;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- }
Employee.java
- package com.dineshonjava.model;
-
- import java.io.Serializable;
-
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.Table;
-
- /**
- * @author Dinesh Rajput
- *
- */
- @Entity
- @Table(name=”Employee”)
- public class Employee implements Serializable{
-
- private static final long serialVersionUID = -723583058586873479L;
-
- @Id
- @GeneratedValue(strategy=GenerationType.AUTO)
- @Column(name = “empid”)
- private Integer empId;
-
- @Column(name=”empname”)
- private String empName;
-
- @Column(name=”empaddress”)
- private String empAddress;
-
- @Column(name=”salary”)
- private Long salary;
-
- @Column(name=”empAge”)
- private Integer empAge;
-
- public Integer getEmpId() {
- return empId;
- }
-
- public void setEmpId(Integer empId) {
- this.empId = empId;
- }
-
- public String getEmpName() {
- return empName;
- }
-
- public void setEmpName(String empName) {
- this.empName = empName;
- }
-
- public String getEmpAddress() {
- return empAddress;
- }
-
- public void setEmpAddress(String empAddress) {
- this.empAddress = empAddress;
- }
-
- public Long getSalary() {
- return salary;
- }
-
- public void setSalary(Long salary) {
- this.salary = salary;
- }
-
- public Integer getEmpAge() {
- return empAge;
- }
-
- public void setEmpAge(Integer empAge) {
- this.empAge = empAge;
- }
- }
EmployeeDao.java
- package com.dineshonjava.dao;
-
- import java.util.List;
-
- import com.dineshonjava.model.Employee;
-
- /**
- * @author Dinesh Rajput
- *
- */
- public interface EmployeeDao {
-
- public void addEmployee(Employee employee);
-
- public List<Employee> listEmployeess();
-
- public Employee getEmployee(int empid);
-
- public void deleteEmployee(Employee employee);
- }
EmployeeDaoImpl.java
- package com.dineshonjava.dao;
-
- import java.util.List;
-
- import org.hibernate.SessionFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Repository;
-
- import com.dineshonjava.model.Employee;
-
- /**
- * @author Dinesh Rajput
- *
- */
- @Repository(“employeeDao”)
- public class EmployeeDaoImpl implements EmployeeDao {
-
- @Autowired
- private SessionFactory sessionFactory;
-
- public void addEmployee(Employee employee) {
- sessionFactory.getCurrentSession().saveOrUpdate(employee);
- }
-
- @SuppressWarnings(“unchecked”)
- public List<Employee> listEmployeess() {
- return (List<Employee>) sessionFactory.getCurrentSession().createCriteria(Employee.class).list();
- }
-
- public Employee getEmployee(int empid) {
- return (Employee) sessionFactory.getCurrentSession().get(Employee.class, empid);
- }
-
- public void deleteEmployee(Employee employee) {
- sessionFactory.getCurrentSession().createQuery(“DELETE FROM Employee WHERE empid = “+employee.getEmpId()).executeUpdate();
- }
- }<span style=”color: #4c1130;”><b>
- </b></span>
EmployeeService.java
- package com.dineshonjava.service;
-
- import java.util.List;
-
- import com.dineshonjava.model.Employee;
-
- /**
- * @author Dinesh Rajput
- *
- */
- public interface EmployeeService {
-
- public void addEmployee(Employee employee);
-
- public List<Employee> listEmployeess();
-
- public Employee getEmployee(int empid);
-
- public void deleteEmployee(Employee employee);
- }
EmployeeServiceImpl.java
- package com.dineshonjava.service;
-
- import java.util.List;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
-
- import com.dineshonjava.dao.EmployeeDao;
- import com.dineshonjava.model.Employee;
-
- /**
- * @author Dinesh Rajput
- *
- */
- @Service(“employeeService”)
- @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
- public class EmployeeServiceImpl implements EmployeeService {
-
- @Autowired
- private EmployeeDao employeeDao;
-
- @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
- public void addEmployee(Employee employee) {
- employeeDao.addEmployee(employee);
- }
-
- public List<Employee> listEmployeess() {
- return employeeDao.listEmployeess();
- }
-
- public Employee getEmployee(int empid) {
- return employeeDao.getEmployee(empid);
- }
-
- public void deleteEmployee(Employee employee) {
- employeeDao.deleteEmployee(employee);
- }
-
- }
EmployeeController.java
- package com.dineshonjava.controller;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.validation.BindingResult;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.servlet.ModelAndView;
-
- import com.dineshonjava.bean.EmployeeBean;
- import com.dineshonjava.model.Employee;
- import com.dineshonjava.service.EmployeeService;
-
- /**
- * @author Dinesh Rajput
- *
- */
- @Controller
- public class EmployeeController {
-
- @Autowired
- private EmployeeService employeeService;
-
- @RequestMapping(value = “/save”, method = RequestMethod.POST)
- public ModelAndView saveEmployee(@ModelAttribute(“command”)EmployeeBean employeeBean,
- BindingResult result) {
- Employee employee = prepareModel(employeeBean);
- employeeService.addEmployee(employee);
- return new ModelAndView(“redirect:/add.html”);
- }
-
- @RequestMapping(value=”/employees”, method = RequestMethod.GET)
- public ModelAndView listEmployees() {
- Map<String, Object> model = new HashMap<String, Object>();
- model.put(“employees”, prepareListofBean(employeeService.listEmployeess()));
- return new ModelAndView(“employeesList”, model);
- }
-
- @RequestMapping(value = “/add”, method = RequestMethod.GET)
- public ModelAndView addEmployee(@ModelAttribute(“command”)EmployeeBean employeeBean,
- BindingResult result) {
- Map<String, Object> model = new HashMap<String, Object>();
- model.put(“employees”, prepareListofBean(employeeService.listEmployeess()));
- return new ModelAndView(“addEmployee”, model);
- }
-
- @RequestMapping(value = “/index”, method = RequestMethod.GET)
- public ModelAndView welcome() {
- return new ModelAndView(“redirect:/add.html”);
- }
-
- @RequestMapping(value = “/delete”, method = RequestMethod.GET)
- public ModelAndView editEmployee(@ModelAttribute(“command”)EmployeeBean employeeBean,
- BindingResult result) {
- employeeService.deleteEmployee(prepareModel(employeeBean));
- Map<String, Object> model = new HashMap<String, Object>();
- model.put(“employee”, null);
- model.put(“employees”, prepareListofBean(employeeService.listEmployeess()));
- return new ModelAndView(“addEmployee”, model);
- }
-
- @RequestMapping(value = “/edit”, method = RequestMethod.GET)
- public ModelAndView deleteEmployee(@ModelAttribute(“command”)EmployeeBean employeeBean,
- BindingResult result) {
- Map<String, Object> model = new HashMap<String, Object>();
- model.put(“employee”, prepareEmployeeBean(employeeService.getEmployee(employeeBean.getId())));
- model.put(“employees”, prepareListofBean(employeeService.listEmployeess()));
- return new ModelAndView(“addEmployee”, model);
- }
-
- private Employee prepareModel(EmployeeBean employeeBean){
- Employee employee = new Employee();
- employee.setEmpAddress(employeeBean.getAddress());
- employee.setEmpAge(employeeBean.getAge());
- employee.setEmpName(employeeBean.getName());
- employee.setSalary(employeeBean.getSalary());
- employee.setEmpId(employeeBean.getId());
- employeeBean.setId(null);
- return employee;
- }
-
- private List<EmployeeBean> prepareListofBean(List<Employee> employees){
- List<employeebean> beans = null;
- if(employees != null && !employees.isEmpty()){
- beans = new ArrayList<EmployeeBean>();
- EmployeeBean bean = null;
- for(Employee employee : employees){
- bean = new EmployeeBean();
- bean.setName(employee.getEmpName());
- bean.setId(employee.getEmpId());
- bean.setAddress(employee.getEmpAddress());
- bean.setSalary(employee.getSalary());
- bean.setAge(employee.getEmpAge());
- beans.add(bean);
- }
- }
- return beans;
- }
-
- private EmployeeBean prepareEmployeeBean(Employee employee){
- EmployeeBean bean = new EmployeeBean();
- bean.setAddress(employee.getEmpAddress());
- bean.setAge(employee.getEmpAge());
- bean.setName(employee.getEmpName());
- bean.setSalary(employee.getSalary());
- bean.setId(employee.getEmpId());
- return bean;
- }
- }
Spring Web configuration file web.xml
<web-app version="2.5" 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_2_5.xsd">
<servlet>
<servlet-name>sdnext</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name><param-value>/WEB-INF/config/sdnext-servlet.xml</param-value></init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>sdnext</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Spring Web configuration file sdnext-servlet.xml
<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:property-placeholder location="classpath:resources/database.properties">
</context:property-placeholder>
<context:component-scan base-package="com.dineshonjava">
</context:component-scan>
<tx:annotation-driven transaction-manager="hibernateTransactionManager">
</tx:annotation-driven>
<!-- <bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean> -->
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="viewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer" id="tilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/config/tiles.xml</value>
</list>
</property>
</bean>
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="${database.driver}"></property>
<property name="url" value="${database.url}"></property>
<property name="username" value="${database.user}"></property>
<property name="password" value="${database.password}"></property>
</bean>
<bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" id="sessionFactory">
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses">
<list>
<value>com.dineshonjava.model.Employee</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto} </prop>
</props>
</property>
</bean>
<bean class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="hibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
tiles.xml
<tiles-definitions>
<definition name="base.definition" template="/WEB-INF/views/mainTemplate.jsp">
<put-attribute name="title" value=""></put-attribute>
<put-attribute name="header" value="/WEB-INF/views/header.jsp"></put-attribute>
<put-attribute name="menu" value="/WEB-INF/views/menu.jsp"></put-attribute>
<put-attribute name="body" value=""></put-attribute>
<put-attribute name="footer" value="/WEB-INF/views/footer.jsp"></put-attribute>
</definition>
<definition extends="base.definition" name="addEmployee">
<put-attribute name="title" value="Employee Data Form"></put-attribute>
<put-attribute name="body" value="/WEB-INF/views/addEmployee.jsp"></put-attribute>
</definition>
<definition extends="base.definition" name="employeesList">
<put-attribute name="title" value="Employees List"></put-attribute>
<put-attribute name="body" value="/WEB-INF/views/employeesList.jsp"></put-attribute>
</definition>
</tiles-definitions>
database.properties
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/DAVDB
database.user=root
database.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
menu.jsp
- <h2> Menu</h2>
- 1. <a href=”employees.html”>List of Employees</a>
- 2. <a href=”add.html”>Add Employee</a>
header.jsp
- <h2>Header- Employee Management System</h2>
footer.jsp
-
- <p>Copyright © 2013 dineshonjava.com</p>
mainTemplate.jsp
- <%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″
- pageEncoding=”ISO-8859-1″%>
-
- <html>
- <head>
-
- <title>
<tiles:insertAttribute name=”title” ignore=”true”></tiles:insertAttribute>
</title>
</head>
<body>
<table border=”1″ cellpadding=”2″ cellspacing=”2″ align=”left”>
<tr>
<td colspan=”2″ align=”center”>
<tiles:insertAttribute name=”header”></tiles:insertAttribute>
</td>
</tr>
<tr>
<td>
<tiles:insertAttribute name=”menu”></tiles:insertAttribute>
</td>
<td>
<tiles:insertAttribute name=”body”></tiles:insertAttribute>
</td>
</tr>
<tr>
<td colspan=”2″ align=”center”>
<tiles:insertAttribute name=”footer”></tiles:insertAttribute>
</td>
</tr>
</table>
- </body>
- </html>
Once you are done with creating source and configuration files, export your application. Right click on your application and use Export-> WAR File option and save your Spring3TilesApp.war file in Tomcat’s webapps folder.
Now start your Tomcat server and make sure you are able to access other web pages from webapps folder using a standard browser. Now try a URL http://localhost:8080/sdnext/ and you should see the following result if everything is fine with your Spring Web Application:
Now we click on the List of Employee link on the Menu section then we get the following output screen we observe that only body of the mainTemplate is refreshed.
Dwonload this Application SourceCode+Libs
Spring3TilesApp.zip
<<Spring Web MVC Framework |index| Spring 3 MVC Framework with Interceptor>>