Reputation: 59
I am porting my Spring MVC Servlet application to be compatible for deployment in Liferay. Am converting the application to a Spring MVC portlet. My application makes use of html + javascript as the frontend. Am unable to import .js or .css files in my index.html now because relative URL's are not working. Any help in this regard?
Upvotes: 0
Views: 1612
Reputation: 2193
If you provide the location of the javascript and css files in your liferay-portlet.xml
file, then it will be automatically added. You don't have to explicitly add the same into your html/jsp file.
in your liferay-portlet.xml, please add following attributes with the correct file path.
<header-portlet-css>/css/main.css</header-portlet-css>
<header-portlet-javascript>/js/main.js</header-portlet-javascript>
Also, if you want to access any static resources like image or anything else, you can use the path like this
<img src='<%=request.getContextPath()%>/images/gear.png' class='settings' />
where the structure is like this
-portlet ---docroot ------images --------gear.png
Upvotes: 1
Reputation: 246
Your references are relative to your web application context references, which normally is the portal ROOT context including the portlet instance and some other dependent constructs (the related LR theme) in this case and not the portlet (your application) context which you assume. So, the solution is to use a portal instance specific global variable with the prefix in front of your path definitions. To achieve this you have to construct this path before you render your HTML page content. This is usually achieved using some Liferay utility classes e.g. in case of a JSP like this:
<link href="<%= PortalUtil.getStaticResourceURL(request, request.getContextPath() + "/css.jsp", portlet.getTimestamp()) %>"
Apparently, in case you do not have server side scripting like the JSP (BTW which I think is not a good idea) then you have to solve this using some other Java layer facility to write into your HTML before it is rendered by the browser.
Upvotes: 0