Tiles Simple Program?
>> Tuesday, June 21, 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 webapplication.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 LocaleInternationalization(i18).
It is possible to load different Tiles according to a key.
The additional elements to develop Tiles application
layout.jsp
tiles-defs.xml
These two elements we have to add our struts 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 overriden 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.(best 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 jsps 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"> </put></put></put></definition> <definition extends="layout" name="bodypart"> <put name="body" value="/bodyone.jsp"> </put></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"> </set-property></set-property></plug-in>Based on the path given in addressbar 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.doThe 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></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 <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>6.)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>7.)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>8.)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> <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"/> </defination>9.)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>After running the application the output looks like below screen
By clicking the hyperlink the next screen looks like below
0 comments:
Post a Comment