Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

image url in portlet

 
Pablo Olmos
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm having a real hard time by trying to display an image with the <img> tag in a portlet. I can't seem to generate the URL appropriately

This is the closest I've come to generating the right url in the doView() method:


I even tried to encode it like so:

but that throws and Illegal argument exception. becase the url generated is something like this:

and the encodeURL method requires the path to start with a "/" or contain "://".

the bottom line question is: how can i get an url for an image that is included in my war archive?

thanks,

Pablo
 
osman cinar eren
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can do the following:
if your context is wps(as in the case with IBM Websphere), you can use:
<a href="/wps/images/page.gif">click to see page.gif</a>


hope this helps..
 
Jose Pereira
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
havent tryed bun the spec says that for u not to have problem whith URL encoding u should use the encodeURL() method to get the url specific to the conteiner u use ....

PLT.12.1.2 Encoding of URLs
 
Mehdi Chaouachi
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the same problem with liferay earlier. While I didnt find a perfect solution, I found a temporary one.

You can place your images in the liferay folder (under tomcat) and then use the "/" in your image. I thik this is a known problem with liferay, I dont know if they have solved it yet or not.
 
pegel van de Wiel
Greenhorn
Posts: 3
Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also using Liferay and stubled upon this old thread.
Without knowing too much about it, the following worked for me

All within the same .jsp:

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<portlet:defineObjects />

<img src='<%=renderResponse.encodeURL(renderRequest.getContextPath() +
"/images/earth.jpg")%>'
alt="Earth" />
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic