Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access partial Web Content in Liferay within JSP

Tags:

jsp

liferay

The short version

I've defined a custom Web Content Structure; now I want to access and emit just one of the fields defined by the Structure within my custom portlet's JSP-based view.

I'm looking for something that's entirely server-side, ideally done entirely within the JSP (i.e. no custom controller logic, no Javascript). I am okay with a small amount of servlet code and/or weird JSTL.

The longer version

I'm building a custom portlet that displays some user data. In the event that a given user has no data to show, I'd like to present some static content instead. There are actually a few special scenarios where, rather than the normal data display, I want to show one of a few pieces of static content.

To ease maintenance, I'm making those pieces of static content part of the portlet-instance configuration. And because I anticipate widespread use of the portlet as well as a desire to manage these static pieces in a single place, I intend to create a new Web Content Structure to capture separate, named HTML blobs for each of the static scenarios. Then the portlet instances will be configured to rely on that piece of Web Content by ID. The result is that site maintainers will be able to update a single piece of Web Content, and all portlet instances across the site will automatically reflect that change.

In other portlets, I've made use of the <liferay-ui:journal-article> tag to emit the entire contents of Web Content (identified in portlet-instance config), e.g.:

<liferay-ui:journal-article
    groupId="${_config_templateGroupId}"
    articleId="${_config_templateArticleId}"
/>

However, in this case I wish to only emit one of the fields defined by the Structure.

The official wiki mentions that the journal-article tag accepts an optional templateId attribute. I think I could make that work by defining separate Templates for each of the scenarios, each of which emits only the desired field from the Structure. However, the wiki also notes that templateId is ignored prior to version 6.0.2 -- I am using 6.0 EE; Major Sadface.

I'm interested in any alternatives that may be available. I can obviously emit the entire content into a hidden element and then use Javascript to move just the relevant piece into the spotlight -- but as a front-end dev this offends my sensibilities regarding the minimum accuracy of the base document pre-scripting.

So: no Javascript or CSS-based tricks (aka smoke-and-mirrors). Only 100% server-based solutions that can be implemented entirely within a relatively vanilla JSP will be accepted.

Grant my wish, O Great and Terrible Internet!

like image 765
Tom Avatar asked Nov 26 '25 05:11

Tom


1 Answers

As this went 8 hours without answer, let me give you a rough (unprecise) answer with a few options that you might want to follow:

You could use just one template and code the logic (what field/content to display) in that template, instead of your portlet. This might or might not be a good point to put maintainable conditions into, but in all scenarios that I can think of, I'd prefer this mode of operation. The ease to do this might depend on your experience with the template language chosen and the power of that environment (do you use xsl, velocity or freemarker?)

If you decide differently, an article is basically an xml document - so, given the article you can always get the xml content and display/transform this or access parts (e.g. xpath). You might want to escape the contents - but that's up to you.

Also, as you mention that templateId is ignored prior to 6.0.2 and you're using 6.0 EE: Be assured that 6.0.2 predates 6.0 EE, so - according to the referenced statement - templateId is not ignored, you should be fine, no need for sadface.

like image 178
Olaf Kock Avatar answered Nov 27 '25 20:11

Olaf Kock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!