Beruflich Dokumente
Kultur Dokumente
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
view plainprint?
1.
package com.dineshonjava.bean;
2.
3.
4.
/**
* @author Dinesh Rajput
5.
6.
*/
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
this.salary = salary;
19.
20.
21.
return id;
22.
23.
24.
this.id = id;
25.
26.
27.
return name;
28.
29.
30.
31.
this.name = name;
}
32.
33.
34.
35.
36.
this.age = age;
37.
38.
39.
return address;
40.
41.
42.
43.
44.
this.address = address;
}
}
Employee.java
view plainprint?
1.
package com.dineshonjava.model;
2.
3.
import java.io.Serializable;
4.
5.
import javax.persistence.Column;
6.
import javax.persistence.Entity;
7.
import javax.persistence.GeneratedValue;
8.
import javax.persistence.GenerationType;
9.
import javax.persistence.Id;
10.
import javax.persistence.Table;
11.
12.
13.
/**
* @author Dinesh Rajput
14.
15.
*/
16.
@Entity
17.
@Table(name="Employee")
18.
19.
20.
21.
22.
@Id
23.
@GeneratedValue(strategy=GenerationType.AUTO)
24.
@Column(name = "empid")
25.
26.
27.
@Column(name="empname")
28.
29.
30.
@Column(name="empaddress")
31.
32.
33.
@Column(name="salary")
34.
35.
36.
@Column(name="empAge")
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
this.salary = salary;
}
70.
71.
72.
73.
74.
75.
76.
77.
78.
EmployeeDao.java
view plainprint?
1.
package com.dineshonjava.dao;
2.
3.
import java.util.List;
4.
5.
import com.dineshonjava.model.Employee;
6.
7.
/**
8.
9.
10.
*/
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
EmployeeDaoImpl.java
view plainprint?
1.
package com.dineshonjava.dao;
2.
3.
import java.util.List;
4.
5.
import org.hibernate.SessionFactory;
6.
import org.springframework.beans.factory.annotation.Autowired;
7.
import org.springframework.stereotype.Repository;
8.
9.
import com.dineshonjava.model.Employee;
10.
11.
/**
12.
13.
14.
*/
15.
@Repository("employeeDao")
16.
17.
18.
@Autowired
19.
20.
21.
22.
23.
sessionFactory.getCurrentSession().saveOrUpdate(employee);
}
24.
25.
@SuppressWarnings("unchecked")
26.
27.
list();
28.
29.
30.
31.
32.
33.
34.
35.
sessionFactory.getCurrentSession().createQuery("DELETE FROM Employee WHERE empid = "
+employee.getEmpId()).executeUpdate();
36.
37.
38.
</b></span>
EmployeeService.java
view plainprint?
1.
package com.dineshonjava.service;
2.
3.
import java.util.List;
4.
5.
import com.dineshonjava.model.Employee;
6.
7.
8.
/**
* @author Dinesh Rajput
9.
10.
*/
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
EmployeeServiceImpl.java
view plainprint?
1.
package com.dineshonjava.service;
2.
3.
import java.util.List;
4.
5.
import org.springframework.beans.factory.annotation.Autowired;
6.
import org.springframework.stereotype.Service;
7.
import org.springframework.transaction.annotation.Propagation;
8.
import org.springframework.transaction.annotation.Transactional;
9.
10.
import com.dineshonjava.dao.EmployeeDao;
11.
import com.dineshonjava.model.Employee;
12.
13.
14.
/**
* @author Dinesh Rajput
15.
16.
*/
17.
@Service("employeeService")
18.
19.
20.
21.
@Autowired
22.
23.
24.
25.
26.
27.
employeeDao.addEmployee(employee);
}
28.
29.
30.
31.
32.
33.
34.
return employeeDao.getEmployee(empid);
35.
36.
37.
38.
39.
40.
41.
EmployeeController.java
view plainprint?
1.
package com.dineshonjava.controller;
2.
3.
import java.util.ArrayList;
4.
import java.util.HashMap;
5.
import java.util.List;
6.
import java.util.Map;
7.
8.
import org.springframework.beans.factory.annotation.Autowired;
9.
import org.springframework.stereotype.Controller;
10.
import org.springframework.validation.BindingResult;
11.
import org.springframework.web.bind.annotation.ModelAttribute;
12.
import org.springframework.web.bind.annotation.RequestMapping;
13.
import org.springframework.web.bind.annotation.RequestMethod;
14.
import org.springframework.web.servlet.ModelAndView;
15.
16.
import com.dineshonjava.bean.EmployeeBean;
17.
import com.dineshonjava.model.Employee;
18.
import com.dineshonjava.service.EmployeeService;
19.
20.
/**
21.
22.
23.
*/
24.
@Controller
25.
26.
27.
@Autowired
28.
29.
30.
31.
ean,
32.
BindingResult result) {
33.
34.
employeeService.addEmployee(employee);
35.
36.
37.
38.
39.
40.
41.
model.put("employees", prepareListofBean(employeeService.listEmployeess()));
42.
43.
44.
45.
46.
ean,
47.
BindingResult result) {
48.
49.
model.put("employees", prepareListofBean(employeeService.listEmployeess()));
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
an,
60.
BindingResult result) {
61.
employeeService.deleteEmployee(prepareModel(employeeBean));
62.
63.
model.put("employee", null);
64.
model.put("employees", prepareListofBean(employeeService.listEmployeess()));
65.
66.
67.
68.
69.
Bean,
70.
71.
BindingResult result) {
Map<String, Object> model = new HashMap<String, Object>();
72.
model.put("employee", prepareEmployeeBean(employeeService.getEmployee(employeeBean
.getId())));
73.
model.put("employees", prepareListofBean(employeeService.listEmployeess()));
74.
75.
76.
77.
78.
79.
employee.setEmpAddress(employeeBean.getAddress());
80.
employee.setEmpAge(employeeBean.getAge());
81.
employee.setEmpName(employeeBean.getName());
82.
employee.setSalary(employeeBean.getSalary());
83.
employee.setEmpId(employeeBean.getId());
84.
employeeBean.setId(null);
85.
return employee;
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
bean.setName(employee.getEmpName());
96.
bean.setId(employee.getEmpId());
97.
bean.setAddress(employee.getEmpAddress());
98.
bean.setSalary(employee.getSalary());
99.
bean.setAge(employee.getEmpAge());
100.
beans.add(bean);
101.
102.
103.
return beans;
104.
105.
106.
107.
108.
bean.setAddress(employee.getEmpAddress());
109.
bean.setAge(employee.getEmpAge());
110.
bean.setName(employee.getEmpName());
111.
bean.setSalary(employee.getSalary());
112.
bean.setId(employee.getEmpId());
113.
return bean;
114.
115.
}
}
view plainprint?
1. <web-app version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xm
lns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/
javaee
2.
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
3.
4.
<servlet>
5.
<servlet-name>sdnext</servlet-name>
6.
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
7.
<init-param>
8.
9.
10.
<param-name>contextConfigLocation</param-name><param-value>/WEBINF/config/sdnext-servlet.xml</param-value></init-param>
<load-on-startup>1</load-on-startup>
</servlet>
11.
12. <servlet-mapping>
13. <servlet-name>sdnext</servlet-name>
14. <url-pattern>*.html</url-pattern>
15. </servlet-mapping>
16.
17. <welcome-file-list>
18. <welcome-file>index.html</welcome-file>
19. </welcome-file-list>
20.
21. </web-app>
Spring Web configuration file sdnext-servlet.xml
view plainprint?
1. <beans xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="htt
p://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSch
ema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocati
on="
2. http://www.springframework.org/schema/beans
3. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
4. http://www.springframework.org/schema/context
5. http://www.springframework.org/schema/context/spring-context-3.0.xsd
6. http://www.springframework.org/schema/tx
7. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
8.
9. <context:property-placeholder location="classpath:resources/database.properties">
10. </context:property-placeholder>
11. <context:component-scan base-package="com.dineshonjava">
12. </context:component-scan>
13.
14. <tx:annotation-driven transaction-manager="hibernateTransactionManager">
15. </tx:annotation-driven>
16.
17. <!-- <bean id="jspViewResolver"
18.
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
19.
<property name="viewClass"
20.
value="org.springframework.web.servlet.view.JstlView"></property>
21.
22.
<property name="viewClass">
27.
<value>
28.
org.springframework.web.servlet.view.tiles2.TilesView
29.
</value>
30.
</property>
31. </bean>
<property name="definitions">
34.
<list>
35.
<value>/WEB-INF/config/tiles.xml</value>
36.
</list>
37.
</property>
38. </bean>
39.
40. <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="data
Source">
41. <property name="driverClassName" value="${database.driver}"></property>
42. <property name="url" value="${database.url}"></property>
43. <property name="username" value="${database.user}"></property>
44. <property name="password" value="${database.password}"></property>
45. </bean>
46.
47. <bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactory
Bean" id="sessionFactory">
48. <property name="dataSource" ref="dataSource"></property>
49. <property name="annotatedClasses">
50. <list>
51.
<value>com.dineshonjava.model.Employee</value>
52. </list>
53. </property>
</props>
60.
</property>
61. </bean>
62.
63. <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="
hibernateTransactionManager">
64. <property name="sessionFactory" ref="sessionFactory"></property>
65. </bean>
66. </beans>
tiles.xml
view plainprint?
1. <tiles-definitions>
2.
3.
4.
5.
6.
7.
8.
</definition>
9.
10.
11.
12.
13.
</definition>
14.
15.
16.
17.
18.
19.
20. </tiles-definitions>
database.properties
view plainprint?
1. database.driver=com.mysql.jdbc.Driver
2. database.url=jdbc:mysql://localhost:3306/DAVDB
3. database.user=root
4. database.password=root
5. hibernate.dialect=org.hibernate.dialect.MySQLDialect
6. hibernate.show_sql=true
7. hibernate.hbm2ddl.auto=update
addEmployee.jsp
view plainprint?
1.
2.
pageEncoding="ISO-8859-1"%>
3.
4.
5.
6.
7.
<html>
<head>
8.
9.
10.
</head>
11.
<body>
12.
13.
14.
15.
<tbody>
16.
<tr>
17.
18.
19.
</form:input></td>
20.
</tr>
21.
<tr>
22.
23.
24.
25.
</tr>
26.
<tr>
27.
28.
29.
</form:input></td>
30.
</tr>
31.
<tr>
32.
33.
34.
</form:input></td>
35.
</tr>
36.
<tr>
37.
38.
39.
</form:input></td>
40.
</tr>
41.
<tr>
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
<td colspan="2">
<input type="submit" value="Submit"></td>
</tr>
</tbody>
</table>
</form:form>
<c:if test="${!empty employees}">
<h2> List Employees</h2>
<table align="left" border="1">
<tbody>
<tr>
53.
<th>Employee ID</th>
54.
<th>Employee Name</th>
55.
<th>Employee Age</th>
56.
<th>Employee Salary</th>
57.
<th>Employee Address</th>
58.
<th>Actions on Row</th>
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
</tr>
<c:foreach items="${employees}" var="employee">
<tr>
<td><c:out value="${employee.id}"></c:out>
</td>
<td><c:out value="${employee.name}"></c:out>
</td>
<td><c:out value="${employee.age}"></c:out>
</td>
<td><c:out value="${employee.salary}"></c:out>
</td>
<td><c:out value="${employee.address}"></c:out>
</td>
72.
73.
<a href="delete.html/?id=${employee.id}">Delete</a>
74.
</td>
75.
</tr>
76.
</c:foreach>
77.
78.
</tbody></table>
79.
</c:if>
80.
81.
</body>
</html>
employeesList.jsp
view plainprint?
1.
2.
3.
4.
5.
<html>
6.
<head>
7.
<title>All Employees</title>
8.
</head>
9.
<body>
10.
<h1>
11.
List Employees</h1>
12.
<h3>
13.
14.
15.
16.
17.
<tbody>
18.
<tr>
19.
<th>Employee ID</th>
20.
<th>Employee Name</th>
21.
<th>Employee Age</th>
22.
<th>Employee Salary</th>
23.
<th>Employee Address</th>
24.
</tr>
25.
26.
<tr>
27.
<td><c:out value="${employee.id}"></c:out>
28.
</td>
29.
30.
<td><c:out value="${employee.name}"></c:out>
</td>
31.
32.
<td><c:out value="${employee.age}"></c:out>
</td>
33.
34.
<td><c:out value="${employee.salary}"></c:out>
</td>
35.
36.
<td><c:out value="${employee.address}"></c:out></td>
</tr>
37.
</c:foreach>
38.
</tbody>
39.
</table>
40.
</c:if>
41.
</body>
42.
</html>
menu.jsp
view plainprint?
1.
<h2> Menu</h2>
2.
3.
header.jsp
view plainprint?
1.
footer.jsp
view plainprint?
1.
2.
mainTemplate.jsp
view plainprint?
1.
2.
3.
4.
5.
<html>
<head>
6.
7.
<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>
8.
</body>
9.
</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.