Reputation: 683
I am using Liferay portlets, having an issue embedding jpg images within a view.jsp page.
My directory structure appears as follows:
webapp
---myportlet
------view.jsp
------myimage.jpg
Within my view.jsp page, I have the following:
<img src="<%=request.getContextPath()%>/myimage.jpg" />
The above image path resolves to the following:
path = /myportlet/myimage.jpg
Within my portlet.xml file, I have the following:
<supports>
<mime-type>image/jpeg</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
I understand one solution is to put all images within the underlying web/app server and reference that path. But it is strongly preferred to put these images within the portlet directory.
With that said, some questions:
Upvotes: 1
Views: 3394
Reputation: 683
It is possible to embed images within your portlet, and render via the <img>
tag. Here's how to do it.
The following code gives you the root directory to where all your images (and other stuff) can be found within the web server:
request.getContextPath()
You then append to this the following pathname to your image:
/html/<yourportletname>/<portletRelativePathToYourImage>
In this thread's original post, I stated a portlet named myportlet with an embedded image named myimage.jpg. So the code to display this image is as follows:
<img src='<%=request.getContextPath() + "/html/myportlet/myimage.jpg"%>' />
Hope this helps others.
Upvotes: 1