我正在尝试在不使用 ext 和 servicebuilder 的情况下实现可重用的自定义服务。我参考了这篇文章: http: //www.devatwork.nl/2010/04/implementing-a-reusable-liferay-service-without-ext-or-service-builder/,但我对如何实现这个感到困惑使用日食?以下是我执行此操作的步骤:
- 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 文件,但它无法正常工作,仍然显示相同的错误。我不知道这是否是实现可重用服务的正确方法。我试过这个,以便我可以在我的一个项目中使用自定义方法。
这是自定义服务的代码:
定制服务.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); } }
请指出我如何实施可重复服务?任何指导都会非常有用。
谢谢。