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

This question already has answers here:
Closed 10 years ago.
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.

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.

Related

add jsp file to another jsp file and have the servlet read it as a whole,

I want to include lines of jsp code into another jsp file, where the servlet only sees appended lines of code from both files. I wan it to build the file all at once, rather than included page first and then the parent page.
What I've done:
I had server errors when I used include file. It built the included page first when I used the include page. Tried templating with tags but I kept getting the error shown below, but all the code was copied and pasted from another stackoverflow answer.
HTTP Status 500 - /web/templateTest.jsp (line: 4, column: 0) No tag "genericpage" defined in tag library imported with prefix "t"
I mostly just want my question answered, that being whether or not there is a way to include files so that they are read as a whole. However it would be nice to know what i'm doing wrong with the tags if someone can identify it. this is my templateTest.jsp
<%#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>
below is my genericpage.tag file in the WEB-INF/tags folder
<%#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>
I'll say it again, I mostly just want my question answered, that being whether or not there is a way to include files so that they are read as a whole. However it would be nice to know what I'm doing wrong with the tags if someone can identify it.
it seems that you might not declare the tag library which has defination of tag "t"
i hope it resolves your query
As you didn't show more, my answer will be : use #include to include to second file into the first. That way the inclusion occurs at the source level.

include a jsp as a header based on a condition inJSF

I'm using JSF-2.0 and I'm trying to include a jsp as a header for my current jsp.But all i want is the included jsp should be altered based on the login credentials.
More clearly...depending on the person logging in to my application, the header menu (included jsp) should be different.I've tried implementing in the below way but it did not work..any help would be appreciated
<html>
<head></head>
<body>
<%
String menuHeader = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("menuAssigned");
if (menuHeader.equals("XX")){ %>
<f:view> <jsp:include page="XHeader.jsp" /> </f:view>
<% }else if(menuHeader.equals("YY")){ %>
<f:view> <jsp:include page="YHeader.jsp" />
<%}%>
---
</f:view>
</body>
</html>
Don't use Scriptlets. Ever.
Your menuAssigned variable is just available in EL by #{menuAssigned}. I suggest to align your menuAssigned variable value with the JSP include filename. Then you can just use
<jsp:include page="#{menuAssigned}Header.jsp" />
Imagine that menuAssigned is XX, then this will include XXHeader.jsp.
Unrelated to the concrete problem, why are you using legacy JSPs while you're apparently already on JSF 2.0 which comes along with JSP's awesome successor Facelets (XHTML)?
Short answer, use EITHER JSP or JSF flow control. Don't mix them too much.
<html>
<head></head>
<body>
<f:view>
<h:panelGroup rendered="#{menuHeader == 'XX'}">
<%#include file=”XHeader.jsp" %>
</h:panelGroup>
<h:panelGroup rendered="#{menuHeader == 'YY'}">
<%#include file=”YHeader.jsp" %>
</h:panelGroup>
</f:view>
</body>
</html>
Perhaps static includes? Again, I've been using facelets with JSF for several years now. Not used to the JSP stuff anymore. Its been a while.

Why my included JSP file won't get processed correctly?

I am trying (and learning) to build a java web framework, and in the process of developing its' code generator based on the content of the database. In the view making process, I stumble in a difficulty, which I don't know how to solve it.
Firstly, I want all the pages to be created using the following index.jsp :
<body>
<%# include file="header.jsp" %>
<hr/>
<%# include file="body.jsp" %>
<hr/>
<%# include file="footer.jsp" %>
</body>
And, in the body.jsp, I want it to be like this :
<jsp:include page="${application_modul}" %>
Where application_modul is an attribute defined in its' controller this way :
request.setAttribute("application_modul","user_account\\view_user_account.jsp");
It can find the file correctly, but the processed jsp is not what I expected. Here :
<c:forEach items="[application.models.UserAccountModel#18a49e0, application.models.UserAccountModel#1f82982]" var="item" varStatus="status" >
<tr>
....
You can see the items attribute of jstl forEach, got its variable name (toString())...
Any Idea what the problem is????
I hope I describe my problem correctly
Many thanks!
PS :
I already create a quick fix for this, but not what I want it though. In the generated view_user_account.jsp, I do it like this :
<body>
<%# include file="header.jsp" %>
<hr/>
<c:forEach items="${row}" var="item" varStatus="status" >
<tr>
....
<hr/>
<%# include file="footer.jsp" %>
</body>
You can see that I create the whole file here...
EDITED:
PS : ${row} is an ArrayList populated with data from certain table
So, to summarize your problem in a single sentence, JSTL tags are not been parsed and they end up plain in generated HTML output?
You need to declare JSTL taglib in top of the JSP page where you're using JSTL tags to get them to run. For the JSTL core taglib, that'll be
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
I am not sure but, Try this...
index.jsp
<jsp:param name="parameterName" value="{parameterValue | <%= expression %>}" />

JSP, can it work similar to yield, layout, content_for in Ruby/Rails/Erb

I am trying to figure out how to most effectively reuse JSP code.
I love the way Rails/erb works in that way ... with yield, layout, content_for
Example:
main_layout.erb.html
<html>
<head><%= yield :head %></head>
<body><%= yield %></body>
</html>
use
<% content_for :head do %>
<title>A simple page</title>
<% end %>
<p>Hello, Rails!</p>
in controller
layout "main_layout"
What is the closest I can get to this with JSP (without using extra frameworks)? I know about JSP include but that's not really the same as yield.
Any suggestions?
Thanks
I'm not familiar with what yield and content_for provide, but JSP tag files allow you a more robust way to template pages than JSP includes.
Example:
layout.tag
<%# tag body-content="scriptless" %>
<%# attribute name="pageTitle" required="true" type="java.lang.String" %>
<html>
<head>
<title>${pageTitle}</title>
</head>
<body>
<jsp:doBody/>
</body>
</html>
An individual JSP
<%# taglib prefix="z" tagdir="/WEB-INF/tags" %>
<z:layout pageTitle="A simple page">
<p>Hello, JSP!</p>
</z:layout>
Just place your layout.tag in the /WEB-INF/tags directory. You can use any available prefix you want, I just used "z" for the example.
While you mentioned wanting no frameworks on top of stock jsp, the Layout functionality of the Stripes Framework does pretty much exactly what you're asking for.

Do I have to include libs in every tile within Apache Tiles?

I am using Tiles within my web-application. I have a standard-layout (standard.jsp) within the tiles are used. On top of the standard.jsp are a lot of includes, concerning tag-libraries and such.
Let's do a simplified example.
standard.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# include file="/WEB-INF/jsp/includes/include.jsp" %>
<html>
<head>
<tiles:insertAttribute name="head" flush="false"/>
</head>
<body>
<tiles:insertAttribute name="body" flush="false"/>
</body>
</html>
body.jsp:
<div id="body-div">
<p>Hello, <c:out value="${forname}" />!</p>
</div>
This prints:
Hello, !
In the tiles I would like to use the tags, but it's not working. It only works, if I add the includes to the tile-jsp.
body.jsp with includes:
<%# include file="/WEB-INF/jsp/includes/include.jsp" %>
<div id="body-div">
<p>Hello, <c:out value="${forname}" />!</p>
</div>
This prints:
Hello, John!
Is there a better way to do this or do I have to add all includes to every jsp used?
You don't need ALL includes to be present in each of your tiles, but each used tag-library in a tile must specifically be included in the using tile.
eg :
In your example, each tile using the C JSTL library should at least have the <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> include

Categories