Powered by Blogger.

Simple Struts program Using Tiles

>> Monday, June 20, 2011

Simple Struts program Using Tiles

Welcome to Struts Tiles Component.Tiles is a templating system.It can be used to create a common look and feel for a Web Application.This can be used to  reusable view components.

The main features of Tiles:
  • Inheritance mechanism on definitions
  • Common layouts with overriding mechanism
  • Dynamic page reload based on parameter layout
  • We can load different tiles according to Locale Internationalization(i18N).
  • It is possible to load different Tiles according to a key.
The additional elements to develop Tiles application
  1. layout.jsp 
  2. tiles-defs.xml
These are the two files we have to add our Application.

Step1. Develop your jsp pages to create layout to your application.

here i am using header,body,footer this is layout. If you perform some action in body part the page will be replaced with bodyone.jsp page.It means body.jsp is going to overridden by bodyone.jsp based on struts configurations.

Step2. Create a layout by using the three pages.

Step3. If you are using html,tiles tld files in your jsp pages dont forget to configure in web.xml as taglib.Configure the tld files in WEB-INF folder.

<jsp-config>
  <taglib>
   <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
  </taglib>
  <taglib>
   <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
 </jsp-config>


Step4. Develop web.xml with all tags.(better to copy and paste in you application).

Step5. Develop tiles-defs.xml with definitions.

      Here the mappings can be done to perticular attribute name given in jsp's and the path of perticular jsp pages. The definition name and parent definition name which is extending to antoher definition.


<definition name="layout" path="/layout.jsp"> 
         <put name="header" value="/header.jsp" />
         <put name="body"   value="/body.jsp" />
         <put name="footer" value="/footer.jsp" />   
 </definition>
 
 <definition name="bodypart" extends="layout"> 
         <put name="body"   value="/bodyone.jsp" />   
 </definition>


Step6. Configure tiles plugin in struts-config.xml with action path and parameter to forward in the layout.

<plug-in className="org.apache.struts.tiles.TilesPlugin" >
        <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />      
        <set-property property="moduleAware" value="true" />
    </plug-in>

Based on the path given in address bar the action will be done in struts-config.xml.

<action-mappings>
        <action parameter="layout" path="/tiles" type="org.apache.struts.actions.ForwardAction">
        
        <action parameter="bodypart" path="/bodypart" type="org.apache.struts.actions.ForwardAction">
    </action></action></action-mappings>

Start your Server(Tomcat) and run your application by giving this url in addressbar

http://localhost:8080/TilesDemo/tiles.do

The Given programs for this application:

#1). header.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>Insert title here</title>
</head>
<body>
  <center>Header to my Site</center>
</body>
</html>

#2). body.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<!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>Insert title here</title>
</head>
<body>

<center><h3>Body to my Site<br></br></h3><html:link action="/bodypart">Click</html:link> </center>
</body>
</html>

#3). footer.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>Insert title here</title>
</head>
<body>
  <center>Footer to my Site<br>
  <a href="http://javabynataraj.blogspot.com">http://javabynataraj.blogspot.com</a></center>
</body>
</html>

#4). bodyone.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>Insert title here</title>
</head>
<body>
 <center><h3><font color="red">Body One to my Site</font></h3></center>
</body>
</html>

#5). layout.jsp

<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>

<table border=2 width="50%">
<tr>
 <td bgcolor="pink"><tiles:insert attribute="header" /></td>
</tr>

<tr>
<td><tiles:insert attribute="body" /></td>
</tr>

<tr>
 <td bgcolor="pink"><tiles:insert attribute="footer" /></td>
</tr>

</table>

#6). tiles-defs.xml

<?xml version="1.0" encoding="UTF-8" ?>

 <!DOCTYPE tiles-definitions PUBLIC
       "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN"
       "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
       
<tiles-definitions>
 <definition name="layout" path="/layout.jsp"> 
         <put name="header" value="/header.jsp" />
         <put name="body"   value="/body.jsp" />
         <put name="footer" value="/footer.jsp" />   
 </definition>
 
 <definition name="bodypart" extends="layout"> 
         <put name="body"   value="/bodyone.jsp" />   
 </definition>
</tiles-definitions>

#7). web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <init-param>
   <param-name>debug</param-name>
   <param-value>2</param-value>
  </init-param>
  <init-param>
   <param-name>detail</param-name>
   <param-value>2</param-value>
  </init-param>
  <load-on-startup>2</load-on-startup>
 </servlet>

 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>

 <session-config>
  <session-timeout>30</session-timeout>
 </session-config>

 <welcome-file-list>
  <welcome-file></welcome-file>
 </welcome-file-list>

 <jsp-config>
  <taglib>
   <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
  </taglib>
  <taglib>
   <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
 </jsp-config>
</web-app>

#8). struts-config.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

    <form-beans></form-beans>
    
    <global-exceptions></global-exceptions>

    <global-forwards></global-forwards>

    <action-mappings>
        <action path="/tiles" parameter="layout" type="org.apache.struts.actions.ForwardAction"/>
        
        <action path="/bodypart" parameter="bodypart" type="org.apache.struts.actions.ForwardAction"/>
    </action-mappings>
    
    <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
    
    <plug-in className="org.apache.struts.tiles.TilesPlugin" >
        <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />      
        <set-property property="moduleAware" value="true" />
    </plug-in>
    
</struts-config>

The screen will be after running this application:




Click on hyperlink given in body area then it overrides the given page in tiles-defs.xml



Download SOURCE

              Libraries

Related Posts Plugin for WordPress, Blogger...

About Author

My Photo
GeekDude
Hi, this is Muralidhar from Chennai.I like blogging on some known things. Trying to provide some basic information for java beginners.
View my complete profile

Random Posts

© javabynataraj.blogspot.com from 2009 - 2014. All rights reserved.