0% found this document useful (0 votes)
154 views11 pages

JSP Code Optimization with Tiles

The document describes different solutions to common problems encountered when building JSP-based web applications that use the same header, footer, and layout across multiple pages. It proposes using the <jsp:include> tag to extract duplicate code from pages into separate header and footer files. It then discusses using the Tiles framework to further reduce duplication by defining a common layout and populating page-specific content using definition files. The final solution avoids duplicating common component definitions by extending a base definition.

Uploaded by

api-3818400
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views11 pages

JSP Code Optimization with Tiles

The document describes different solutions to common problems encountered when building JSP-based web applications that use the same header, footer, and layout across multiple pages. It proposes using the <jsp:include> tag to extract duplicate code from pages into separate header and footer files. It then discusses using the Tiles framework to further reduce duplication by defining a common layout and populating page-specific content using definition files. The final solution avoids duplicating common component definitions by extending a base definition.

Uploaded by

api-3818400
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Page1.jsp Page2.jsp Page3.

jsp
Header Header Header
.

Body-1 Body-2 Body-3

Footer Footer Footer

Limitations:
Duplication of code, the same Header and Footer is included in all three
pages.
Solution:
Separate, Header and Footer from each page.
Use < jsp:include > tag for inserting Header.jsp and Footer.jsp.
Header.jsp Footer.jsp
Page1.jsp Page2.jsp Page3.jsp

<table> <table> <table>


<tr><td> <tr><td> <tr><td>
<jsp:include <jsp:include <jsp:include
page=”/Header.jsp> </td></tr> page=”/Header.jsp> </td></tr> page=”/Header.jsp> </td></tr>
<tr><td> <tr><td> <tr><td>
<!—write Page1 body here -- > <!—write Page2 body here -- > <!—write Page3 body here -- >
</td></tr> </td></tr> </td></tr>
<tr><td> <tr><td> <tr><td>
<jsp:include <jsp:include <jsp:include
page=”/Footer.jsp> </td></tr> page=”/Footer.jsp> </td></tr> page=”/Footer.jsp> </td></tr>

Limitations:
The same table structure (ie layout) is repeated in all the three pages.

Solution :
Separate layout from each page, and provide placeholders
Include the layout in the required page using < tiles:insert page=”/Layout.jsp”>
Populate the pages required using <tiles:put>
Layout.jsp
<table>
<tr><td>
<tiles:insert attribute=”header”>
</td></tr>
<tr><td>
<tiles:insert attribute=”body”>
</td></tr>
<tr><td>
<tiles:insert attribute=”footer”>
</td></tr></table>

Header.jsp Footer.jsp
Page1.jsp

<tiles:insert page=”/Layout.jsp”>
<tiles:put type=”header”
value=”/Header.jsp”>

<!—write Page1 body here -- >

<tiles:put type=”footer”
value=”/Footer.jsp”>

</tiles:insert>

Page2.jsp Page3.jsp

<tiles:insert page=”/Layout.jsp”> <tiles:insert page=”/Layout.jsp”>


<tiles:put type=”header” <tiles:put type=”header”
value=”/Header.jsp”> value=”/Header.jsp”>

<!—write Page2 body here -- > <!—write Page3 body here -- >

<tiles:put type=”footer” <tiles:put type=”footer”


value=”/Footer.jsp”> value=”/Footer.jsp”>

</tiles:insert> </tiles:insert>
Limitations:
Suppose the layout is changed to below given then we need to make changes in
Layout.jsp, and in all the jsp pages which make use of Layout.jsp

Header

Menu

Body

Footer

Solution:
Using tiles-defs.xml define all the jsp pages
Using <tiles:insert definition=>populate the jsp pages
Layout.jsp
<table>
<tr><td>
<tiles:insert attribute=”header”>
</td></tr>
<tr><td>
<tiles:insert attribute=”body”>
</td></tr>
<tr><td>
<tiles:insert attribute=”footer”>
</td></tr></table>

Header.jsp Footer.jsp
<!—tiles-defs.xml -- >
<tiles-definition>
<definition name=”def1” path=”/Layout.jsp”>
<put name=”header” value=”/Header.jsp”>
<put name=”menu” value=”/Menu.jsp”>
<put name=”body” value=”/Body1.jsp”>
<put name=”footer” value=”/Footer.jsp”>
</definition>
<definition name=”def2” path=”/Layout.jsp”>
<put name=”header” value=”/Header.jsp”>
<put name=”menu” value=”/Menu.jsp”>
<put name=”body” value=”/Body2.jsp”>
<put name=”footer” value=”/Footer.jsp”>
</definition>
</tiles-definition>
Body1.jsp Body2.jsp Menu.jsp
Page1.jsp Page2.jsp

<tiles:insert <tiles:insert
definition=”def1”> definition=”def2”>
Limitation:
In tiles-defs.xml the definition tab has repeated
<put name=”header” value=”/Header.jsp”>
<put name=”menu” value=”/Menu.jsp”>
<put name=”footer” value=”/Footer.jsp”> tags

Solution:
Use <definition extends=> to extend the properties of base definition

<!—tiles-defs.xml -- >
<tiles-definition>
<definition name=”basedef” path=”/Layout.jsp”>
<put name=”header” value=”/Header.jsp”>
<put name=”menu” value=”/Menu.jsp”>
<put name=”body” value=””>
<put name=”footer” value=”/Footer.jsp”>
</definition>
<definition name=”def1” extends=”basedef”>
<put name=”body” value=”/Body1.jsp”>
</definition>
<definition name=”def2” extends=”basedef”>
<put name=”body” value=”/Body2.jsp”>
</definition>
</tiles-definition>

You might also like