liferay错误未显示portlet

时间:2018-08-29 09:20:48

标签: liferay liferay-7 liferay-ide

  1. 创建的工作区
  2. 创建了服务构建器并添加了实体
  3. 创建了DemoPortlet
  4. 我正在尝试通过DemoPortlet的doView方法从数据库和system.out中获取数据。
  5. 当我使用CountryLocalServiceUtil(CountryLocalServiceUtil.getCountriesCount())-错误[http-bio-8080-exec-10] [render_portlet_jsp:132] null

我读到可以使用CountryLocalService,@ Reference等吗?这个代码可以吗?我可以看到我的portlet是活动的,但没有显示在Sample小部件中。您可以使用CountryLocalService提供热调用getCountriesCount()的代码吗?

  @Component(
        immediate = true,
        property = {
            "com.liferay.portlet.display-category=category.sample",
            "com.liferay.portlet.instanceable=true",
            "javax.portlet.init-param.template-path=/",
            "javax.portlet.init-param.view-template=/view.jsp",
            "javax.portlet.name=" + DemoPortletKeys.Demo,
            "javax.portlet.resource-bundle=content.Language",
            "javax.portlet.security-role-ref=power-user,user"
        },
        service = Portlet.class
    )
    public class DemoPortlet extends MVCPortlet {

        private CountryLocalService countryLocalService;


        @Override
        public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
                throws IOException, PortletException {
            // TODO Auto-generated method stub

            System.out.println("********" + getCountryLocalService().getCountriesCount() + " ********************");

            super.doView(renderRequest, renderResponse);
        }

        public CountryLocalService getCountryLocalService() {
            return countryLocalService;
            }

        @Reference(unbind = "-")
        public void setCountryLocalService(CountryLocalService countryLocalService) {
        this.countryLocalService = countryLocalService;
        }
    }

1 个答案:

答案 0 :(得分:0)

您没有指定要使用的Liferay的确切版本,很明显它是7.0或7.1。在当前的代码库中,我看不到任何CountryLocalService,但是有一个com.liferay.portal.kernel.service.CountryService

以下代码与您的代码几乎完全相同,可在Liferay DXP 7.0上为我工作,portlet部署,出现在示例类别中,并且仅从Liferay Developer Studio中的mvc-portlet模板开始创建。我编辑的唯一文件是此处显示的文件。

package com.example.country.portlet.portlet;

import com.example.country.portlet.constants.CountryPortletKeys;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.service.CountryService;

import java.io.IOException;

import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(
    immediate = true,
    property = {
        "com.liferay.portlet.display-category=category.sample",
        "com.liferay.portlet.instanceable=true",
        "javax.portlet.display-name=countryportlet Portlet",
        "javax.portlet.init-param.template-path=/",
        "javax.portlet.init-param.view-template=/view.jsp",
        "javax.portlet.name=" + CountryPortletKeys.Country,
        "javax.portlet.resource-bundle=content.Language",
        "javax.portlet.security-role-ref=power-user,user"
    },
    service = Portlet.class
)
public class CountryPortlet extends MVCPortlet {
    private CountryService countryService;

    @Override
    public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
            throws IOException, PortletException {
        System.out.println("************* " + countryService.getCountries().size() + " ***************");
        super.doView(renderRequest, renderResponse);
    }

    @Reference
    public void setCountryService(CountryService cs) {
        this.countryService = cs;
    }
}