2012-05-07 75 views
0

我試圖在不使用ext和servicebuilder的情況下實現可重用的定製服務。 我把這篇文章引用到:http://www.devatwork.nl/2010/04/implementing-a-reusable-liferay-service-without-ext-or-service-builder/,但我很困惑我該如何使用eclipse實現這個功能?以下是我也跟着這樣做的步驟:可重用Liferay(6.0.6)服務

- 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); 
         } 
    } 
    

請點我就如何實現可重用的服務?任何指導都會非常有用。

謝謝。

+0

1.你是否曾經嘗試啓動eclipse的Liferay的外面? ClassNotFoundException說,這個類不能被類加載器找到。 – Mark

+0

2.您是否想要創建許多訪問此服務的項目,或者爲什麼需要製作可重用服務? – Mark

+0

@ 2是的,我想最大限度地減少使用ext,並希望創建一個可用於其他項目的可重用組件。我是liferay中的新成員,對此沒有太多的想法,但是我發現不使用ext的唯一方法是可重用組件。 –

回答

0

我的想法是,您不需要複雜的服務。只需製作實用課程並將其寫入tomcat/lib/ext即可。確保tomcat/lib/ext是正確的tomcat/conf/catalina.properties配置是這樣的:

common.loader=${catalina.home}/lib/ext/*.jar