Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to create JSP layout template? [duplicate]

Tags:

java

jsp

jstl

Possible Duplicate:
JSP tricks to make templating easier?

I'm new to JSPs and Servlets, I'm wondering is there a neat way to create a layout jsp and reuse it on similar jsp pages, something like asp.net master pages.

I googled it, some people say use templates http://java.sun.com/developer/technicalArticles/javaserverpages/jsp_templates that uses jstl tag library. It says to put a tag like this:

<%@ taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %> 

but I get error (because jstl.jar and standard.jar are in WEB-INF/lib/ directory).

However some say jstl template have problems according to this Struts OR Tiles OR ???...... JSP template solution

I would be glad to help me know the best way.

EDIT: What I need is to split page's layout into parts like content, header,... and set this parts in the page that uses the layout template, exactly like asp.net master page.

like image 299
Ashkan Avatar asked May 10 '12 08:05

Ashkan


People also ask

What is a JSP template?

Templates are JSP files that include parameterized content. The templates discussed in this article are implemented with a set of custom tags: template:get , template:put , and template:insert . The template:get tag accesses parameterized content, as illustrated in Example 2.

Which of the following JSP tag is most suitable to write the Java code inside the HTML of JSP page?

Scriptlet tag This tag allow user to insert java code in JSP.

What is the alternative of JSP?

FreeMarker: An open alternative to JSP.

How do I import one JSP to another?

To include JSP in another JSP file, we will use <jsp:include /> tag. It has a attribute page which contains name of the JSP file.


1 Answers

Put the following in WEB-INF/tags/genericpage.tag

<%@tag description="Overall Page template" pageEncoding="UTF-8"%> <%@attribute name="header" fragment="true" %> <%@attribute name="footer" fragment="true" %> <html>   <body>     <div id="pageheader">       <jsp:invoke fragment="header"/>     </div>     <div id="body">       <jsp:doBody/>     </div>     <div id="pagefooter">       <jsp:invoke fragment="footer"/>     </div>   </body> </html> 

To use this:

<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@taglib prefix="t" tagdir="/WEB-INF/tags" %>  <t:genericpage>     <jsp:attribute name="header">       <h1>Welcome</h1>     </jsp:attribute>     <jsp:attribute name="footer">       <p id="copyright">Copyright 1927, Future Bits When There Be Bits Inc.</p>     </jsp:attribute>     <jsp:body>         <p>Hi I'm the heart of the message</p>     </jsp:body> </t:genericpage> 

That does exactly what you think it does!

This was part of a great answer by Will Hartung on this link.

like image 196
Ashkan Avatar answered Oct 15 '22 21:10

Ashkan