Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Rick Herrick
Using Velocity templates in portlet
April 28, 2011 10:08 AM
Answer

Rick Herrick

Rank: New Member

Posts: 10

Join Date: April 27, 2011

Recent Posts

I can't figure out how to get Velocity templates working in a portlet. I've created a really simple portlet using the Liferay IDE (attached). I can get the basic version working quite easily with the view.jsp. Great. But then I try to get the view.vm version working and can't seem to do it. Here's what I've tried:

  1. Just change the value for view-jsp to indicate the vm file instead of the jsp. I expected this to fail and it did.
  2. Tried to change the portlet class to com.liferay.portlet.VelocityPortlet. This caused an error in Eclipse, since the archive for that class (portal-impl.jar) isn't included in the Liferay Portlet Plugin API.
  3. To fix the missing VelocityPortlet issue, I added in portal-impl.jar to the project build path. That resolved the error in the configuration file, but gave me a ClassNotFound exception on start-up. Makes sense, I didn't package the jar in the deployment.
  4. To fix the ClassNotFoundException, I added the portal-impl.jar to the /WEB-INF/lib of the portlet deployment assembly. This seems like a bad idea, so even if it were to work I wouldn't like it, but as it happens it doesn't work either. I get a log4j error that I *think* is related to differing versions of log4j, but I'm not 100% certain of that (I've attached this error as well).


So it seems like I'm going down a rabbit hole to accomplish something that should be fairly easy to do. I've scoured the docs, this forum, and search engines fairly assiduously and can't find anything other than references to using Velocity in themes and the like. But we really need to be able to use it in our portlets and that's the sticking point.

Any help would be greatly appreciated.
Attachments: MyHelloVelocity-portlet.zip (17.2k), log4j.portlet.error.txt (7.0k)
Sharana Basavaraj Ballari
RE: Using Velocity templates in portlet
April 28, 2011 11:52 AM
Answer

Sharana Basavaraj Ballari

Rank: Regular Member

Posts: 136

Join Date: September 9, 2007

Recent Posts

Hi There,


If you are willing to use Spring portlet framework, it is easy to use either Velocity/JSP/Fremarker etc..

configure your portlet with SpringDispatcher Portlet class and use VelocityViewResolver.

I know I am deviating you form the original point of using VelocityPortlet class. but I personally feel doing this way might be easier and you will not end up conflicting yourself with adding portal-impl.jar onto your plugin sdk portlets.

This would be the easier and safe way to develop portlets.

HTH,
Sharan
Rick Herrick
RE: Using Velocity templates in portlet
April 28, 2011 12:14 PM
Answer

Rick Herrick

Rank: New Member

Posts: 10

Join Date: April 27, 2011

Recent Posts

Hey Sharan, thanks for replying.

It's funny you mention that, because that's my preferred solution (I come to this project with a lot more Spring development experience than portal development experience!). However, I'm having a problem when trying to use Velocity in my portlet rather than JSP. I actually posted here about this, but the full error information is available in the Spring forums.

I'm assuming you've been able to get Velocity templates working with the Spring portlet framework? If so, do you see anything in the configuration that I may have missed or mis-configured?

Thanks!
Sharana Basavaraj Ballari
RE: Using Velocity templates in portlet
April 28, 2011 1:33 PM
Answer

Sharana Basavaraj Ballari

Rank: Regular Member

Posts: 136

Join Date: September 9, 2007

Recent Posts

Hi Rick,

Can you please paste your xmls here?

I need to see,

1. Web.xml
2. portlet.xml
3. Where you have done your viewResolver Configuration - name-portlet.xml.

I have seen the spring thread as well, there I think we are still missing the portlet.xml

once I see those XML's, If I found the issue, I will update the spring thread as well accordingly.

Cheers!
Sharan
Rick Herrick
RE: Using Velocity templates in portlet
April 28, 2011 2:36 PM
Answer

Rick Herrick

Rank: New Member

Posts: 10

Join Date: April 27, 2011

Recent Posts

Sharana Basavaraj Ballari:
1. Web.xml
2. portlet.xml
3. Where you have done your viewResolver Configuration - name-portlet.xml.


Here you go, thanks for taking a look at this!

I just tested again and if I replace the Velocity elements with a standard InternalResourceViewResolver, it displays fine:

1<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
2    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
3    <property name="prefix" value="/WEB-INF/jsp/" />
4    <property name="suffix" value=".jsp" />
5</bean>


Then I change to the VelocityViewResolver and pfft. The full exception is attached.

Let me know if I can get you any more info on this issue. I do appreciate the effort looking at it!
Attachments: error.log (59.8k), portlet.xml (0.7k), web.xml (0.9k), xnatProjectBrowser-portlet.xml (2.2k)
Sharana Basavaraj Ballari
RE: Using Velocity templates in portlet
April 28, 2011 6:15 PM
Answer

Sharana Basavaraj Ballari

Rank: Regular Member

Posts: 136

Join Date: September 9, 2007

Recent Posts

Hi Rick,

I just had a look at the xml files.

To me, it looks like the beans that you have written in xnatProjectBrowser-portlet.xml are not getting loaded anywhere.

to load the beans you could do the following file update.

in your portlet.xml add,

<init-param>
<name>contextConfigLocation</name>
<value>/WEB-INF/xnatProjectBrowser-portlet.xml</value>
</init-param>

If this still doesnt work, Put your ViewResolverConfiguration onto a different XML file, and load it via web.xml like below,

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/${common-beans}.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

I hope this solves your problem... To load your beans what you need is the contextConfigLocation and a listener class to load them onto the application context by spring.


I am on holiday from tomorrow till sunday, If you still face issues, I could help you on the same only on monday.

Have a nice weekend,
Cheers!
HTH
Sharan
Rick Herrick
RE: Using Velocity templates in portlet
April 29, 2011 7:07 AM
Answer

Rick Herrick

Rank: New Member

Posts: 10

Join Date: April 27, 2011

Recent Posts

Sharana Basavaraj Ballari:
To me, it looks like the beans that you have written in xnatProjectBrowser-portlet.xml are not getting loaded anywhere.


I'm sure that it's getting loaded because it is the changes to that configuration that break the application. That's where I have the view resolver class specified and, when I change to the VelocityViewResolver there, things break. My assumption is that, by default, the Spring DispatcherPortlet class loads the configuration file with the name portlet-name-portlet.xml, which, in the files I posted here, would be xnatProjectBrowser-portlet.xml.

But I went ahead and tried your suggestion just to verify, and because we'll eventually need to be able to load multiple configuration files anyway! The results were the same.

Sharana Basavaraj Ballari:

If this still doesnt work, Put your ViewResolverConfiguration onto a different XML file, and load it via web.xml like below,

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/${common-beans}.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

I hope this solves your problem... To load your beans what you need is the contextConfigLocation and a listener class to load them onto the application context by spring.


I was skeptical about this but I went ahead and tried it because, well, nothing else was working. And guess what? It works! Happy day! It doesn't really make any sense, why would it work in a separate configuration file?

It's not the separate configuration file: it's the ContextLoaderListener. I tried removing the contextConfigLocation parameter and reloaded and that failed, since the ContextLoaderListener tries to load /WEB-INF/applicationContext.xml by default unless instructed otherwise with the contextConfigLocation. I went ahead and added that back in, specifying liferayMavenProject-portlet.xml specifically (just to test something different), and it worked.

For reference, I've attached the project I used to isolate and test this solution.

Thanks a lot for your help, Sharan, it's much appreciated!
Attachments: LiferayMavenProject-portlet.zip (11.1k)
Sharana Basavaraj Ballari
RE: Using Velocity templates in portlet
May 2, 2011 12:18 PM
Answer

Sharana Basavaraj Ballari

Rank: Regular Member

Posts: 136

Join Date: September 9, 2007

Recent Posts

Hey Rick,

Glad to hear that it worked.. emoticon I have just had a look at the attached portlet. everything seems to be ok now. your portlet.xml file is loading the beans.. emoticon

You are right, it will look for applicationContext.xml unless we specify another configuration file to ContextLoaderListener. I didnt see applicationContext.xml file in your old portlet.. anyways, Happy times now... Glad to see it working...

Cheers,
Sharan
SZ khan
RE: Using Velocity templates in portlet
June 6, 2011 11:50 AM
Answer

SZ khan

Rank: Regular Member

Posts: 148

Join Date: October 31, 2009

Recent Posts

I do have a similar question. I am using ResourceBundleViewResolver in a Spring MVC Portlet.

<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="views"/>
<property name="order" value="1" />
</bean>


It gives the following error

java.lang.IllegalStateException: WebApplicationObjectSupport instance [org.springframework.web.servlet.view.ResourceBundleViewResolver@1164deb] does not run within a ServletContext. Make sure the object is fully configured!


If I add the following in my web.xml as shown in your solution

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context-test/*-portlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

then I get the following error.

Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!

I think your solution works because you are not using service builder in your portlet and I do.

Any ideas...Thanks
Bob Fleischman
RE: Using Velocity templates in portlet
June 21, 2011 1:22 PM
Answer

Bob Fleischman

Rank: Junior Member

Posts: 30

Join Date: April 8, 2008

Recent Posts

There is another, simpler approach. User the Velocity jsp tag and then you can just call your velocity template from your JSP page.

Here is an example:
1<%@taglib prefix="velocity" uri="http://velocity.apache.org/velocity-view" %>
2<velocity:view template="\vm\newVportlet\view2.vm" />


You need to include the Velocity Tools (2.0) jar.

This allows the best of both worlds. I can use the Liferay JSP tage (like defineObjects) and the Velocity templates I've previously developed.
Ram Manusani
RE: Using Velocity templates in portlet
November 14, 2011 1:22 PM
Answer

Ram Manusani

Rank: Regular Member

Posts: 124

Join Date: October 27, 2011

Recent Posts

Can u provide a example.. If you have any

Able to see it working with jsp way and no luck with velocity.
Ian Harrigan
RE: Using Velocity templates in portlet
January 27, 2012 6:09 AM
Answer

Ian Harrigan

Rank: New Member

Posts: 19

Join Date: November 22, 2011

Recent Posts

Has anyone got jsp/vm templates to mix an match, any help would be good.

Thanks,
Ian
Apoorva Prakash
RE: Using Velocity templates in portlet
April 3, 2012 12:12 PM
Answer

Apoorva Prakash

Rank: Liferay Master

Posts: 659

Join Date: June 15, 2010

Recent Posts

Hi all,
I hope this link can help you in this regard...

Thanks and regards...
Apoorva Prakash