0
  1. Created workspace
  2. Created service builder and added entity
  3. Created DemoPortlet
  4. I'am trying to get data from DB and system.out in doView method in DemoPortlet.
  5. When i use CountryLocalServiceUtil (CountryLocalServiceUtil.getCountriesCount()) - ERROR [http-bio-8080-exec-10][render_portlet_jsp:132] null

I read that I can use CountryLocalService, @Reference etc.? Is this code ok? I can see that my portlet is Active, but it is not showing in Sample widget. Can you provide code, hot to call getCountriesCount() using CountryLocalService?

  @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;
        }
    }
Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • Your question mentions `CountryLocalServiceUtil`, but your code doesn't. Please clarify (e.g. it looks like the error is triggered in a JSP, not in this code). Also, you tag liferay-6, liferay-7, tomcat - only one of them makes sense, I'll leave it to you to find the relevant tag. And last, I'm puzzled by your obfuscation to set `CountryLocalService` in a method called `setFooLocalService`, but that's only a cosmetic question. – Olaf Kock Aug 29 '18 at 09:31
  • I tried with CountryLocalServiceUtil and get error. Now, I'm trying with CountryLocalService. Any idea? Also tried with renderRequest.setAttribute("countryLocalService", countryLocalService); <%@ include file="/init.jsp" %>

    <% CountryLocalService countryLocalService = (CountryLocalService) renderRequest.getAttribute("CountryLocalService"); %>

    – user2974612 Aug 29 '18 at 09:48
  • please edit your question with the problem that you need to solve. "I'm trying various stuff but still have problems" is too vague to be answered. And react to *all* of my previous sentences. – Olaf Kock Aug 29 '18 at 10:04
  • Now, it's edited. I don't know what else to write. I think that it's clearly. – user2974612 Aug 29 '18 at 10:24

1 Answers1

0

You don't specify which exact version of Liferay you're using, it's just obvious that it's either 7.0 or 7.1. In the current codebase I don't see any CountryLocalService, but there's a com.liferay.portal.kernel.service.CountryService.

The following code, that is almost identical to your code, works for me on Liferay DXP 7.0, the portlet deploys, appears in the sample category, and was created by just starting from the mvc-portlet template in Liferay Developer Studio. The only file I've edited is the one shown here.

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;
    }
}
Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • I'm using liferay CE portal 7.1.0. GA1. I succeeded to getCount from Country (249). It looks that my problem here is connection to Oracle. It persistently connects me to default DB. There is many examples "How to connect liferay external db with oracle" where they added ojdb14.jar, ext-spring.xml etc., but after all that steps I got (CountryLocalServiceUtil.getCountriesCount()) - ERROR [http-bio-8080-exec-10][render_portlet_jsp:132] null), so some guy told me that is maybe because getService is null and I tried to get data without Util class. Sorry of wasting your time! – user2974612 Aug 29 '18 at 11:58
  • Well, this code should work with 7.1 as well, I've just not tested it. Your comment sounds like you work with your own, custom, CountryLocalService? In that case, edit the other question with more information, It's not too obvious that both belong together. – Olaf Kock Aug 29 '18 at 12:06