I am trying to include a JSP page using directive in another JSP.
My code is:
<%#include file="${pageContext.request.contextPage}/Pages/Loader/
load-resources.jsp" %>
As you can see here clearly that my url should be like
localhost://port/WebApp1/Pages/Loader/load-resources.jsp
But on execution i am receiving this error which says this URL does not exist. But i passed url in reference from contextPage.
Caused by: org.apache.jasper.JasperException: PWC6117: File
"/Pages/Test/Take/${pageContext.request.contextPage}/Pages/Loader/
load-resources.jsp" not found
How to resolve this issue?
Ok, if you want to include Pages/Loader/load-resources.jsp, simply use :
<%#include file="/Pages/Loader/load-resources.jsp" %>
You do not include an URL, but a file. The root of the hierarchy is simply the root of your web application.
By the way, are you aware of the differences between the directive <%#include ... %> and the tag <jsp:include .../> :
with the directive, you include the source file, meaning all page context variables will be shared
with <jsp:include .../>, you include at output level, meaning each page will have its own page context
Related
I want to include html pages dynamically in a JSP page. I'm fetching the html url from HTML forder and using struts2 to pass the value to JSP page but I'm unable to do this on JSP using either jsp:include or #include tags.
For Example,
I have variable html Url like /somepath/variablehtmlname.html in my struts action property. I want to use this path to include the actual html files located at /somepath location.
<%# include ... %> is evaluated when your JSP pages are compiled and have no access to request variables (like Struts 2 action properties.) Use <c:import /> or <s:include /> instead, which include content on a per-request basis. <jsp:include /> should also work, but (as #BalusC requested) without the code, we can't tell why it doesn't.
Reusing Content in JSP Pages
I agree with the first answer (BobG). You can also simply have the JSP page directly serve up an http forwardTo using the refresh tag, where the servlet writes the new url location to a session variable : <meta http-equiv="refresh" content="0; URL=<%=htmlSessionLink>" />**
i have scenario where, i need to call a jsp from a servlet and pass a hashmap containing custom objects whose details jsp needs to display...(this is working fine using Request Dispatcher and jstl in jsp code)
however my jsp includes some other jsps that define the look and feel of the whole application.
my structure is as:
TestProject
<Tabs>- images
<Tabs><Tabs>* background.jpg
<Tabs>- jsp
<Tabs><Tabs> * common
<Tabs><Tabs><Tabs>+ common.jsp
<Tabs><Tabs> * xml
<Tabs><Tabs><Tabs> + XMLDisplay.jsp
my XMLDisplay.jsp includes common.jsp which in turn has path for background.
the common.jsp is common to all other files in application and is working fine.... but not in XMLDisplay.jsp.
i have displayed the context path and the servlet path in XMLDisplay.jsp and its fine. (same as in other files) however for images the path somehow is getting distorted.
and the images are getting accessed from
http:localhost:8080\images\background.jpg
instead of
http:localhost:8080\TestProject\images\background.jpg
the only difference in the other pages is that the call is from one jsp to another while here the call is from servlet to jsp.
Try to use jstl tag <c:url value="\images\background.jpg" var="image1"/> from taglib
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
You should prefer this tag for resources like (*.js, images, etc.) and for constructing links with parameters.
For example, a user will be rendered throughout my application as
<div class="user">
<p class="username">${user.name}</p>
<p class="karma">${user.karma}</p>
<img src="/users/${user.id}"/>
</div>
How can I reuse this code block?
Note - my code is running within a tag, so I can't use tags for this (or any JSP) otherwise I get a Scripting elements are disallowed here error.
Edit
I'm trying to use a tag file, but getting PropertyNotFoundException.
This is my tag file called 'user.tag':
<%#tag description="User" pageEncoding="UTF-8" %>
<a href="../user/showUser.do?userId=${user.id}">
<p>${user.name}</p>
<img class='avatar' src='${user.avatarUrl}' alt=""/>
</a>
And usage inside a jsp:
Where job.poster is a java bean with id, name, and avatarUrl properties.
If I add
<%#attribute name="user" %>
to the tag file, then I get an exception
Property 'id' not found on type java.lang.String
Since JSP 2.0, there is yet another kind of tags: Tag files. Tag files are JSP custom tags written as a JSP template itself, which seems to be what you want.
http://fforw.de/post/creating-jsp-layouts-with-page-tags/ shows how to use such a tag file as general layout solution. Using them as component should be even easier.
You should be able to use tag files within tag files; this works for me in a JSP 2.2 container:
<%-- mytag.tag --%>
<%#tag description="demo code" pageEncoding="UTF-8"%>
<%#taglib prefix="cust" tagdir="/WEB-INF/tags" %>
<%#attribute name="message"%>
<cust:mytag2 message="${message}" /><%-- uses mytag2.tag --%>
If that fails, you can use the include directive: <%#include file="/WEB-INF/jspf/fragment.jspf" %>
Note that the spec says about tags:
Directive Available? Interpretation/Restrictions
======================================================================
page no A tag file is not a page. The tag directive must
be used instead. If this directive is used in a
tag file, a translation error must result.
So, fragment.jspf must not have a any elements that are not supported in tags, including a page directive.
For the example you have given it sounds like some templating framework is needed, to display the user badge on each screen. At its simplest level this may just be a jsp:include which always includes your "UserBadge.jsp".
If you are running on a web framework e.g. JSF you may use Facelet templates or write a custom component for this. So the answer depends on what framework you have. Breaking it down to just JSP and JSTL - the included JSP or a javax.servlet.jsp.tagext.Tag would certainly reduce the duplication.
Always be careful to follow the DRY Principle... Don't Repeat Yourself!
I feel sometimes creating a .tag file is overkill (especially if it's only for one page), and I've wanted what you describe for years, so I wrote my own, simple solution. See here:
https://stackoverflow.com/a/25575120/1607642
Why don't you use a custom tag or jsp functions.
Thanks,
I'm new to JSP, using Eclipse, and am trying to just get started with templates. I've imported template.tld into WebContent/WEB-INF/tlds.
Guide: http://www.javaworld.com/jw-09-2000/jw-0915-jspweb.html
When I run the test.jsp file, I get this error:
org.apache.jasper.JasperException: /test.jsp(3,0) Unable to load tag handler class "tags.templates.InsertTag" for tag "template:insert"
I've tried searching Google, but am unable to find a solution. Any help would be greatly appreciated!
template.jsp
<%# taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>
<html><head><title><template:get name='title'/></title></head>
<body>
Welcome!<br />
<template:get name='content'/>
</body></html>
test.jsp
<%# taglib uri='/WEB-INF/tlds/template.tld' prefix='template' %>
<template:insert template='/template.jsp'>
<template:put name='content' content='this is our website'/>
</template:insert>
Have you also added the taglib's classes to /WEB-INF/lib or /WEB-INF/classes? Just having the TLD is not enough, you need the code that actually implements it. Your error message fragment suggests you haven't done that.
This test was prompted by reading a question on the Sun java forums and thought I would try it out.
The JSP2.0 specification section JSP.13.8 contains an "Example Simple Tag Handler Scenario". I copy and pasted the code fragments and attempted to run it.
Environment:
Apache Tomcat version 5.5.26 and 6.0.14 (tested on both)
Java: 1.5
Code I am testing with:
Jsp page:
<%# taglib prefix="my" tagdir="/WEB-INF/tags" %>
<my:simpletag x="10">
<jsp:attribute name="y">20</jsp:attribute>
<jsp:attribute name="nonfragment">
Nonfragment Template Text
</jsp:attribute>
<jsp:attribute name="frag">
Fragment Template Text ${var1}
</jsp:attribute>
<jsp:body>
Body of tag that defines an AT_BEGIN
scripting variable ${var1}.
</jsp:body>
</my:simpletag>
And the tag file:
<%-- /WEB-INF/tags/simpleTag.tag --%>
<%# attribute name="x" %>
<%# attribute name="y" %>
<%# attribute name="nonfragment" %>
<%# attribute name="frag" fragment="true" %>
<%# variable name-given="var1" scope="AT_BEGIN" %>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Some template text.
<c:set var="var1" value="${x+y}"/>
<jsp:invoke fragment="frag" varReader="var1"/>
Invoke the body:
<jsp:doBody/>
This code is directly copied from the PDF copy of the JSP2.0 specification.
It is also available as part of the JSP-API here
Minor change made - I changed the name of the tagfile from simpletag.tag to simpleTag.tag to match the invocation of it in the JSP.
I also tried copying from the PDF of the spec (adjusting quotes as necessary) - same result.
When I execute the page I end up with a standard 500 error
Root cause:
java.lang.ClassCastException: java.io.StringReader
org.apache.jsp.tagVariableTest_jsp._jspService(tagVariableTest_jsp.java:62)
Line62 of the generated JSP turns out to be:
var1 = (java.lang.String) _jspx_page_context.findAttribute("var1");
Ok, I can understand ClassCastException - it thinks that var1 should be a String, and the actual attribute is a StringReader. But why is it a StringReader? Where did the variable get created? And why is it attempting to do this assignment at all?
Can someone please point me in the right direction?
What is wrong with the code/setup?
Is this a known issue? I googled for it but couldn't seem to find anything.
Thanks,
evnafets
Editing with resolution:
The ClassCastException was being caused by the line in the tag:
<jsp:invoke fragment="frag" varReader="var1"/>
As mentioned here the varReader attribute specifies the attribute to store the evaluation result as a StringReader. The exception was caused by Tomcat generated code trying to retrieve the value of "var1" and cast it to a String. As a String is not a StringReader so, it raised an exception at that point.
I'm not sure if the coding error is they should have used the "var" instead of the "varReader" attribute, or they shouldn't have used either and just evaluated it as is.
Removing that attribute completely prints out the fragment, and then the body both with the value of "var1":
Fragment Template Text 30.
Invoke the body: Body of tag that defines an AT_BEGIN scripting variable 30
Making the attribute var="var1" executes the fragment, and stores the result to var1. The body is then evaluated with this new value of var1 resulting in:
Invoke the body: Body of tag that defines an AT_BEGIN scripting variable Fragment Template Text 30
Personally I think the first case makes more sense, but this code is presented as an example of the innerworkings, not best practice.
I still would have expected the example to compile/run in any case. I was fairly surprised when it didn't.
From the syntax reference you can see that either var or varReader is
The name of a scoped attribute to store the result of the fragment invocation in
So I am afraid the sample code contains an error. If you need to write the result of jsp:invoke directly to the page you do not need to assign it to a var.
Can you try leaving out the "varReader" attribute?