【发布时间】:2012-05-08 16:19:32
【问题描述】:
我正在尝试在不使用 ext 和 servicebuilder 的情况下实现可重复使用的自定义服务。 我引用了这篇文章:http://www.devatwork.nl/2010/04/implementing-a-reusable-liferay-service-without-ext-or-service-builder/,但我对如何使用 eclipse 实现这一点感到困惑?以下是我执行此操作的步骤:
- Created liferay-plugin project within eclipse.
- Created package containing CustomServices (interface) and CustomServicesUtil.
- Created jar file of package in step 2.
- Placed that jar file in tomcat\lib\ext\
- Then created package (with in same liferay-plugin project), that includes CutomServicesImpl and CustomServicesBaseImpl
- Defined portlet-spring.xml, service.properties, and modified web.xml (as per the article), and finally deployed the project.
在部署时,项目已成功部署,但是当我尝试通过 CustomServicesUtil.getCustomMethod() 使用 CustomServicesImpl 中定义的 customMethods 时,出现以下错误:
"java.lang.ClassNotFoundException: com.demo.custom.services.CustomServicesUtil"
我将构建路径配置为包含 customservices.jar 文件,但它无法正常工作,仍然显示相同的错误。我不知道这是否是实现可重用服务的正确方法。我试过这个,以便我可以在我的一个项目中使用自定义方法。
这是自定义服务的代码:
-
CustomServices.java
package com.demo.custom.services; import com.liferay.portal.model.User; public interface CustomServices { String getCustomName(User user); } -
CustomServicesUtil.java
package com.demo.custom.services; import com.liferay.portal.model.User; public class CustomServicesUtil { private static CustomServices services; public static CustomServices getServices() { if (services == null) { throw new RuntimeException("Custom Services not set"); } return services; } public void setServices(CustomServices pServices) { services = pServices; } public static String getCustomName(User user){ return getServices().getCustomName(user); } } -
CustomServicesBaseImpl.java
package com.demo.custom.services.impl; import com.demo.custom.services.CustomServices; import com.liferay.portal.kernel.exception.SystemException; import com.liferay.portal.service.base.PrincipalBean; import com.liferay.portal.util.PortalUtil; public abstract class CustomServicesBaseImpl extends PrincipalBean implements CustomServices { protected CustomServices services; public CustomServices getServices() { return services; } public void setServices(CustomServices pServices) { this.services = pServices; } protected void runSQL(String sql) throws SystemException { try { PortalUtil.runSQL(sql); } catch (Exception e) { throw new SystemException(e); } } } -
CustomServicesImpl.java
package com.demo.custom.services.impl; import com.liferay.portal.model.User; public class CustomServicesImpl extends CustomServicesBaseImpl { @Override public String getCustomName(User user) { // TODO Auto-generated method stub if(user == null){ return null; }else{ return new StringBuffer().append(user.getFirstName()).append(" ").append(user.getLastName()).toString(); } } }
这是我的另一个 portlet 的控制器类的代码,我正在使用该服务。
-
HelloCustomName.java
package com.test; import java.io.IOException; import javax.portlet.PortletException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import com.demo.custom.services.CustomServicesUtil; import com.liferay.portal.kernel.util.WebKeys; import com.liferay.portal.model.User; import com.liferay.portal.theme.ThemeDisplay; import com.liferay.util.bridges.mvc.MVCPortlet; public class HelloCustomName extends MVCPortlet { @Override public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { System.out.println("--doview----"); ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY); User user = themeDisplay.getUser(); String customName = CustomServicesUtil.getCustomName(user); //getting error here System.out.println("customName:" + customName); } }
请指点我如何实现可重用服务?任何指导都会非常有用。
谢谢。
【问题讨论】:
-
1.您是否尝试在 Eclipse 之外启动 liferay? ClassNotFoundException 说,类加载器找不到这个类。
-
2.您是否要创建许多访问此服务的项目,或者为什么需要提供可重用服务?
-
@2 是的,我想尽量减少 ext 的使用,并想创建一个可重用的组件,它可以在其他项目中有用。我是 liferay 的新手,对此并没有太多了解,但我发现不使用 ext 的唯一方法是可重用组件。
-
@1 如前所述,我用必要的 jar 文件配置了构建路径,但它仍然给了我 classNotFoundException,所以我仍在寻找正确的方法。
标签: liferay-6