Thoughts Electrique

Posts Tagged ‘JSP’

How to log in a user of a specified OpenCms OU

Monday, October 12th, 2009

I recently had to login a user of a specified OU (Organizational Unit) through a JSP. This is done by simply prepending the username with the path to the OU.

Example: I want to login the user “foo” of the OU “/germany/marketing“. Simple specify the String “/germany/marketing/foo” as a username and you’re all set.

JSP to download OpenCms resources as a ZIP

Thursday, June 25th, 2009

What often bugged me is that I can upload a set of files into OpenCms as a ZIP file but can not download a tree of files as such (without the use of the database export feature). I had hacked together this little JSP some time ago. It allows you to dynamically download a set of OpenCms resources as a ZIP file.

The JSP has no interface since I mainly use it for development. Simply create a JSP somewhere in the OpenCms VFS and then open the file via the browser. E.g.:

“http://workplace.com/system/createzip?source=/system/modules/foo&filename=foo-module.zip

The source parameter specifies which directory to zip up and the filename parameter which filename to use for the generated file. You can only zip up resources with a total size of 10 megabytes. This is mainly to prevent OutOfMemory errors to happen. You can easily increase the size by altering the variable maxSourceSize to any value you need.

If you find any bugs or want to give feedback simply leave a comment or send me an email.

(more…)

Manipulating OpenCms XMLContent programatically (some examples)

Friday, December 12th, 2008

Because someone asked on the mailing list and I keep forgetting the respective API calls to manipulate an OpenCms XMLContent programatically.

This is just a raw code dump not a step by step tutorial.

You can also download a ZIP file for importing it into OpenCms. But you have to adjust the paths manually.

(more…)

Use the power of JSP tag files in OpenCms

Friday, December 12th, 2008

Since version 7 OpenCms is a Servlet 2.4 and JSP 2.0 conforming application. In addition to the improved expression language (EL) JSP 2.0 also brings in the possibility of developing custom tags(actions) using JSP rather than writing Java code. This allows for some pretty neat things and enables you to go one step further towards cleaner view code. I’ve used this in one of my last projects extensively to refactor common code like pagers and link generation. For example:

<cms:include file="/system/modules/com.example.ocms.commons/elements/pager">
  <cms:param name="currentPage">
    <c:out value="${model.currentPage}"/>
  </cms:param>
  <cms:param name="pageCount">
    <c:out value="${model.pageCount}"/>
  </cms:param>
</cms:include>

Became:

<wt:pager model="${model}"/>

Not does it only look cleaner and is much more focused, it also decouples your view more from the underlying CMS solution. Of course you could also develop a custom tag library using Java (which I have done a few times) but using a JSP the advantage that code can be changed on the fly and even from a web designer. You can always refactor your JSP taglib into a Java one if you need to do it. There are some limitations of JSP tag files (like you can’t use scriptlets in the body) but they are easy to get around.

But as always: This is no silver bullet. There are problems with tag files which may not be obvious at the first glance.

(more…)

Display RSS Feeds in JSPs with RSS4JSP

Thursday, December 11th, 2008

I recently discovered the freshly released RSS4JSP tag library. Its a wrapper around the mature and widely used Rome RSS library which was developed by Sun and then open sourced. The library allows you to simply display (news) feeds inside your JSP pages.

While it’s certainly a young project, I found it very useful and easy to use. I gave it a try to create the Latest blog posts box on my, OpenCms based, homepage. The box was created during 5 minutes which I think is impressive (given that you have to type the HTML too).

Unfortunately the project does not allow the syndication of multiple feeds into one feed (This is a nice feature of Rome which I used in a recent project) but maybe it will be added in the future (or I will add it myself).

Check it out at sourceforge or visit the authors blog.

Better SEO for external links in OpenCms Navigation

Wednesday, November 26th, 2008

The Problem

If you put an external link into the OpenCms navigation the resulting link will target the external link file which then in turns redirects the surfer to the external page. This is bad if you really want to optimize your navigation for search engines, because search engines won’t see the real link inside the navigation.

The solution

You can use the following JSP snippet to get the real URL from a CmsJspNavElement.

// We assume that the variable "link" holds an CmsJspNavElement and
// "cmsa" holds an CmsJspActionElement.

// Prevent external links from beeing a redirect
// therefore extract the real URL out of the file
String fileUri = link.getResourceName();
CmsResource navResource = cmsa.getCmsObject().readResource(fileUri);
if (navResource.getTypeId() == CmsResourceTypePointer.getStaticTypeId()) {
    CmsFile navFile = CmsFile.upgrade(navResource,cmsa.getCmsObject());
    String externalLink = new String(navFile.getContents());
    externalLink = externalLink.trim();
}

Further reading