Is there a way to use root path when refering to includes in JSP?
what I mean is instead of using this method:
<jsp:include page="../inc/header.jsp" />
<jsp:include page="../../inc/header.jsp" />
etc.
To just refer to it like this:
<jsp:include page="jsp/inc/header.jsp" />
Also this is how my project tree looks like
Anybody trying to figure it out here is the answer that I came up with. For some reason intelij IDE tells me that the path cannot be resolved(liar!) looking at my project tree you can include header to any file using this piece of code in your project:
<%# include file="/jsp/inc/header.jsp" %>
This is page content
<jsp:include page="/jsp/inc/footer.jsp" />
In both cases the IDE will tell you that it cannot resolve URL but it works like a charm I hope this helps not only me but future visitors
Related
This question already has an answer here:
How specify path to .JSP file for request.getRequestDispatcher()?
(1 answer)
Closed 6 years ago.
I have a servelet called RegisterUser mapped as /register this servelet includes a jsp file called register.jsp . here is the structure of the project :
inside the servelet this code cannot find the jsp file :
RequestDispatcher view = request.getRequestDispatcher(registerView);
and the content of registerView is :
private static final String registerView = "../web/WEB-INF/views/Register.jsp";
It worked fine on eclipse but when I switched to Intellij it shows that the file cannot be found .
Liferay has a somewhat strange layout for it's web-inf and html folders. In Eclipse I never really code path completion to work well in JSPs because it always looked at the wrong spot for files. In IntelliJ I just setup different facets that will help the IDE help me. Here is an example. I have a facet called WEB-INF. In Liferay, when you reference a file in WEB-INF (tagfiles are a GREAT example here) you do it like this:
<%# taglib prefix="showMore" tagdir="/WEB-INF/tags/wj/showmore" %>
The problem there is that by default, the IDE wants to autocomplete like this:
<%# taglib prefix="showMore" tagdir="/docroot/WEB-INF/tags/wj/showmore" %>
I simply made a web facet and said that when I type /WEB-INF I really want you to look at /docroot/WEB-INF. This small change saves me so much headache throughout the day. I also did the same thing with /html instead of /docroot/html.
You know how in eclipse when you go to an init.jsp file in the ext environment it always freaks about the init.jsp includes from portal because it can't find it? I once again used a web facet (called portal-web) and I linked / to /liferay/builds/4.3.x/portal-web/docroot. Now when I say:
<%# include file="/
right there it starts autocompleting. It lists out everything in both portal-web and ext-web for me, because I have access to both inside Liferay! Doing this then:
<%# include file="/html/common/init.jsp" %>
it's a problem, I can even click init.jsp and press ctrl (cmd) + b and it will jump me right into the init file in portal.
I tend to run IntelliJ with one "Module" and two "Content Roots". One content root is portal, the other is ext. The plus here is that the debugger knows about the portal source files, and I can easily jump between jsp files as well.
Resource Link:
Why I use IntelliJ instead of Eclipse?
I'm trying to import a JSP using c:import and c:url, but it tells me it couldn't find the file. I tryed using a link to see if it opens the file, and it works fine. So, I suppose the path is correct, but it isnt't working.
My code is like this:
<c:url value="/cabecalho.jsp" var="cabecalho" />
<c:import value="${cabecalho}"></c:import> //page don't open
link //to test the path
If I use <c:url value="cabecalho.jsp" /> it works fine!
What must be happening?
c:url gives You absolute path from internet like http://www.somehost.com . c:import wants relative path to jsp on server.
Probably a ridiculously easy question here but I must be phrasing it weird in all of my search queries to find similar solutions.
So in my eclipse project I have a folder with some .jsp files in it. In another folder there are some .jpgs . I want to use one of these .jpg files in my .jsp but for some reason cannot get the classpath correct.
I tried right clicking and copying the qualified name and using that path but it wont link correctly for some reason...
my code looks like :
<img src="pikachu.jpg" height="300" />
I've also tried:
<img src="/My_Project_Name/WebContent/images/pikachu.jpg" height="300" />
Note: the Jsp is in:
/My_Project_Name/WebContent/JSP_FOLDER/JSP.jsp
thanks in advance - I know this should be a simple thing...
You need to use
<img src="<%= request.contextPath %>/images/pikachu.jpg" height="300" />
in your JSP.
<% request.contextPath %> will expand to the path under which your web server is serving your app.
Every resource inside of WebContent/ will then be accessible relative to this path.
Related:
http://kodejava.org/how-do-i-get-web-application-context-path-in-jsp/
Similar to Aaron's answer but I believe he has a typo in it (missing an equal sign).
Try this instead, which in my opinion is a little less messy:
<img src="${pageContext.request.contextPath}/images/pikachu.jpg" height="300" />
Aaron's solution would have been:
<img src="<%= request.getContextPath() %>/images/pikachu.jpg" height="300" />
If those don't work, view the page source and copy/paste us what the line looks like when you are using those.
Use
<img src="../images/pikachu.jpg" height="300" />
..(double dots) will move you to parent directory where you are having images folder.
I'm working on a web project using java/ jsp/ servlets/ html/ css in eclipse tomcat, where everything is in the WebContent folder.
In my jsp files... When I try to include other jsp files (using a link like "/fileName.jsp" in jsp include directive) I can do that successfully.
But When I try to include image files (using a link like "/fileName.jpg" in the <img src=""> tag) nothing happens.
Nothing happens because instead of looking in the WebContent folder for image file it looks in the tomcat home directory, i.e.
Instead of looking at "http ://localhost:port/projectName/..." it looks at "http: //localhost:port/..."
Why does it look at the wrong location only with <img src=""> tags but not in <%# /> tag.
A workaround for this is that I start giving absolute paths "/projectName/..." However doing this means I'm hardcoding project name everywhere. This is what I do not want.
Don't include binary content in an ascii output. Why not just use the img tag? If you need to do something to produce a jpeg, I would use a Servlet.
Because the jsp-Links in the website are getting processed and the image links not. Either change the image path or develop an filter that changes the images'links.
Yes Templar, that could have been a way to solve my problem.
However, I simply changed the Context Root of my project from "Project Name" to "/" in Eclipse. This solved my problem.
I'm trying to add Google Maps onto my JSPs by using the Googlemaps taglib.
I've added this into my maven pom
<dependency>
<groupId>com.lamatek</groupId>
<artifactId>googlemaps</artifactId>
<version>0.98c</version>
<scope>provided<>/scope
</dependency>
This then included the googlemaps-0.98c library under my project libraries in NetBeans, I right clicked and selected Manually install artifact and located the googlemaps.jar file I had downloaded.
I've then added this into my taglibs file
<%#taglib prefix="googlemaps" uri="/WEB-INF/googlemaps" %>
And have then included this where I actually want to show a map on my jsp
<googlemaps:map id="map" width="250" height="300" version="2" type="STREET"
zoom="12">
<googlemaps:key domain="localhost" key="xxxx"/>
<googlemaps:point id="point1" address="74 Connors Lane" city="Elkton"
state="MD" zipcode="21921" country="US"/>
<googlemaps:marker id="marker1" point="point1"/>
</googlemaps:map>
But when I load up my application, I get the following error.
org.apache.jasper.JasperException: /jsp/dashboard.jsp(1,1) /jsp/common/taglibs.jsp(6,56) PWC6117: File "/WEB-INF/googlemaps" not found
root cause
org.apache.jasper.JasperException: /jsp/common/taglibs.jsp(6,56) PWC6117: File "/WEB-INF/googlemaps" not found
Have I missed something simple? I'm unable to spot what I've done wrong so far..
Generally when you do this:
<%#taglib prefix="googlemaps" uri="/WEB-INF/googlemaps" %>
You are basically trying to say "the folder /WEB-INF/googlemaps has a bunch of .tag files for use" - which you don't.
Just browsing the documentation confirms this - it says you should be using this (note the usage of the tld extension):
<%# taglib uri="/WEB-INF/googlemaps.tld" prefix="googlemaps" %>
Source: http://www.lamatek.com/GoogleMaps/documentation.jsp#installation
If you set scope to provided in your pom it is not included in the war file and the taglib will not be found. You should change the scope to compile or runtime.
The URI should not be /WEB-INF/googlemaps.tld. It should match the value in the <uri> tag in the googlemaps.tld.
Open up the googlemaps.jar, find the googlemaps.tld, and find the <uri> tag. That's the URI you need.
UPDATE:
I just downloaded the googlemaps.jar. I'm incorrect; the URI is indeed <uri>/WEB-INF/googlemaps.tld</uri>.
That suggests that you have to extract the googlemaps.tld file and put it under /WEB-INF in your web context, be it WAR or exploded.