Opencms useful paths and urls

March 28, 2011 4:33 pm 0 comments

This is the first post in my new brand site about technology and I hope you can enjoy it or at least you get something useful.

As a person who likes programming and technology, I like to read and write about the newest stuff, but nowadays I am working as a OpenCms programmer, and since there is not a great documentation and tutorials on the internet about it, I want to share the solutions I found to the thousand problems i found myself in with OpenCms.

One of those problems is related with the paths used to get some data in a jsp. The next list will make your life easier when you want to load some content, display an image, or link some url with opencms.

Just a little explanation for newbies on OpenCms, if you want to use a taglib solution you will have to include the next code in the begining of your jsp:

If you want to use a scriplet solution, then you will need to declare a CmsJspActionElement also in the begining of your file:

Loading some contents from the current folder

If you want to create a jsp that read some contents from the folder that contains it, the easier way is using the cms:info tag
In this example we want to read all the news using a jsp in the same folder than the news. The path to our jsp will be “/extra/news/index.jsp”.

Using a scriptlet

Using this, you will get your script to access to the content of the current folder. If you are using it to display news, you will only need to create a link to the file from other path to display their news.

Adding the context path “/opencms/opencms/” to your links

In my computer I have several OpenCms installations, one of them in /opencms/opencms/ and other in /opencms752/opencms/, and I need my jsp to work in all of them. If I write a link in my jsp like this:

It wouldn’t work in the /opencms752/opencms instalation. Instead hardcoding the link, in this case it is very useful the <cms:link> tag:

This is the proper way of linking for HTML, use it with a, img, script, style and form HTML tags.

Making a link work in the root site “/” and other sites “/sites/default/”

Imagine you need a bulletproof jsp that have to work if you call it from the administration area, whenever you are in the root site or in your /sites/default or any other site you have in your opencms.
This is typical when you are creating a administration section in opencms, you want to make it work the same way when you are in any site. Keep imagining that that jsp is the one from the first example that display the news list in the folder /extra/news/.
If you have your opencms in “/sites/default/” it will work for sure, but if you have it in the root site “/” it will return an error because you should search the news in the path “/sites/default/extra/news/”.
You can know when to add the “/sites/default/” prefix using the session manager:

So when I need this functionality in my jsp’s I use the following function:

This function only works with the default site, if we have more sites in our opencms we can add another parameter to the function
with the prefix of the site to prepend. When you are using one OpenCms for multiple sites you will find the CmsSiteManager class documentation very useful.


Let’s push your website a bit further

My name is Javier Márquez and I have more than 10 years of web programming and web designing experience. If you have a difficult development to complete, maybe you can stop by and see what I can do for you. You can find me on and Twitter.

Leave a reply

contact meAnything related to web development like javascript and CSS to create responsive designs, or PHP and node.js to make your website work properly, is my pleasure. If you have an interesting project in mind, I can help to make it real.