Alex Man February 2016

Liferay access DB table in theme: No bean named 'com.colors.themes.service.ColorLocalService' is defined

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> </servi

Answers


dnebing February 2016

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.


aritzg February 2016

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=

Post Status

Asked in February 2016
Viewed 2,690 times
Voted 12
Answered 2 times

Search




Leave an answer