0

I have a theme project in liferay. I have created a new table called colors in my liferay MySQL database. The colors table is given below

enter image description here

Actually I have a requirement that a particular css file should be loaded in theme based on the color table value whose status is true and my velocity template should be somewhat like as shown below

#set ($myColorService = $serviceLocator.findService("com.colors.themes.service.ColorLocalService"))
#set ($myColor = $myColorService.fetchActiveColor())
#if ($myColor == "blue")
 <link href="$css_folder/themes/blue.css" rel="stylesheet" type="text/css"/>
#elseif ($myColor == "orange")
 <link href="$css_folder/themes/orange.css" rel="stylesheet" type="text/css"/>
#else
 <link href="$css_folder/themes/green.css" rel="stylesheet" type="text/css"/>

The following things are somethings which I have done so far

  • I have created a service builder project (theme_service-portlet) for the colors table. The service.xml is shown below

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd"> <service-builder package-path="com.colors.themes"> <author>user</author> <namespace>theme</namespace> <entity name="Colors" local-service="true" remote-service="true"> <column name="colorId" type="long" primary="true" /> <column name="colorName" type="String" /> <column name="status" type="boolean" /> <finder return-type="Collection" name="Colors"> <finder-column name="status" /> </finder> </entity> </service-builder>

  • Builded the service and jar (theme_service-portlet-service.jar) is generated under lib folder.

  • Copy the theme_service-portlet-service.jar and placed under liferay-portal-6.2-ce-ga2\tomcat-7.0.42\lib\extfolder.
  • In portal_normal.vm I have used the following code:
#set ($myColorService = $serviceLocator.findService("com.colors.themes.service.ColorLocalService"))
#set ($myColor = $myColorService.fetchActiveColor())
#if ($myColor == "blue")
 <link href="$css_folder/themes/blue.css" rel="stylesheet" type="text/css"/>
#elseif ($myColor == "orange")
 <link href="$css_folder/themes/orange.css" rel="stylesheet" type="text/css"/>
#else
 <link href="$css_folder/themes/green.css" rel="stylesheet" type="text/css"/>
  • Restarted the tomcat server

But I am getting the following exception

04:44:55,896 ERROR [http-bio-8080-exec-3][ServiceLocator:39] com.liferay.portal.kernel.bean.BeanLocatorException: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.colors.themes.service.ColorLocalService' is defined
com.liferay.portal.kernel.bean.BeanLocatorException: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.colors.themes.service.ColorLocalService' is defined
    at com.liferay.portal.bean.BeanLocatorImpl.locate(BeanLocatorImpl.java:122)
    at com.liferay.portal.kernel.bean.PortalBeanLocatorUtil.locate(PortalBeanLocatorUtil.java:98)
    at com.liferay.portal.template.ServiceLocator.findService(ServiceLocator.java:36)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.doInvoke(UberspectImpl.java:389)
    at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:378)
    at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:270)
    at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:262)
    at org.apache.velocity.runtime.parser.node.ASTReference.value(ASTReference.java:507)
    at org.apache.velocity.runtime.parser.node.ASTExpression.value(ASTExpression.java:71)
    at org.apache.velocity.runtime.parser.node.ASTSetDirective.render(ASTSetDirective.java:142)
    at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:336)
    at org.apache.velocity.Template.merge(Template.java:328)

Can anyone please tell me some solution for this

Alex Man
  • 4,746
  • 17
  • 93
  • 178

2 Answers2

2

First of all you have the ServiceBuilder layer that exposes your data access layer; this part is good.

Your code for the theme is mostly right. You should be using ServiceLocator to find your service, but you're missing the servlet context that is providing the service. For example, if the plugin providing the service is in color-service-portlet.war, then the service locator call will look like:

    #set ($myColorService = $serviceLocator.findService("color-service-portlet", "com.colors.themes.service.ColorLocalService"))

The exception that you're seeing is because you are using the portal's form to find a portal service but of course the portal is not exporting that service, so you get the BeanLocatorException.

dnebing
  • 335
  • 2
  • 6
1

You need to enable access to services from velocity.

To enable it, edit the value of journal.template.velocity.restricted.variables in portal-ext.properties.

Like this

journal.template.velocity.restricted.variables=

aritzg
  • 39
  • 2
  • I have did that....you can see my full code at https://github.com/nidhishkrishnan/customfunctions-portlet – Alex Man Feb 09 '16 at 12:04
  • @dnebing may be right. Look at this. https://docs.liferay.com/portal/6.2/javadocs-all/com/liferay/portal/template/ServiceLocator.html You may need to pass the context as parameter. I this case comething like #set ($myColorService = $serviceLocator.findService("customfunctions-portlet", "com.colors.themes.service.ColorLocalService")) – aritzg Feb 09 '16 at 13:05
  • That also I have tried.....and now I am getting `BeanLocator is null for servlet context customfunctions-portlet` ....`BeanLocator has not been set for servlet context customfunctions-portlet` – Alex Man Feb 09 '16 at 13:11
  • Also one more change I have noticed is that from 6.2 it should be `velocity.engine.restricted.variables=` instead of `journal.template.velocity.restricted.variables=` ....This also I tried but still the same ....https://www.liferay.com/community/forums/-/message_boards/message/31192652 – Alex Man Feb 09 '16 at 13:17
  • should I need to deploy the `customfunctions-portlet` project? – Alex Man Feb 09 '16 at 13:20
  • Watch out!! The service is not com.colors.themes.service.ColorLocalService but com.colors.themes.service.ColorsLocalService . Mind the s of ColorS – aritzg Feb 09 '16 at 13:22
  • Good Catch......I changed but still the same Exception coming `BeanLocator is null for servlet context customfunctions-portlet` ....`BeanLocator has not been set for servlet context customfunctions-portlet` – Alex Man Feb 09 '16 at 13:29
  • Have a look at this thread. Maybe it can help. https://www.liferay.com/es/community/forums/-/message_boards/message/11195275 By the way... what is the exact name of the forlder por this portlet in webapps? – aritzg Feb 09 '16 at 13:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103003/discussion-between-alex-man-and-aritzg). – Alex Man Feb 09 '16 at 13:46