问题
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
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 underliferay-portal-6.2-ce-ga2\tomcat-7.0.42\lib\ext
folder. - 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
回答1:
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.
回答2:
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=
来源:https://stackoverflow.com/questions/35269627/liferay-access-db-table-in-theme-no-bean-named-com-colors-themes-service-color