Liferay: get PortletId and Plid from init(PortletConfig) [i.e, no request object, just PortletConfig]

杀马特。学长 韩版系。学妹 提交于 2019-12-24 17:22:34

问题


I would like to get both values, and since init(Portletconfig) is executed when loading the portlet, I don't see any doubt about whether this values should be available.

For portletId I tried

String portletId = ((PortletConfigImpl) portletConfig).getPortletId(); 

but it seems I can't. Guess it is because the impl is in another jar not meant to be accessed from portlets

By the way, my main goal is to get to pass both params to another non-request context so I can do

final PortletPreferences prefs = PortletPreferencesFactoryUtil.getLayoutPortletSetup(LayoutLocalServiceUtil.getLayout(plid), portletId);

to read portlet's prefs in real time. If there is any other way to indicate that from init(), like getting the whole preferences it would be enough

EDIT: I found a different approach, and opened a new question with slightly changes Liferay: get PortletID and companyID from init()


回答1:


So... If I understand it right, your goal is to read out portlet preferences inside the init method of your Portlet class.

According to the API, the PortletPreferences object can be retrieved from a PortletRequest instance, which is available in both doView() and processAction() classes. E.g.:

public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
    String name = request.getPreferences().getValue("name", null);

    Writer writer = response.getWriter();
    writer.write("Hello, "+name+"!");
}

It is important to note that in Liferay, portlet preferences by default only exist within the scope of a page, or, in Liferay terms, a Layout, which is identified by a plid, short-hand for page layout id.

Concerning your init() method, the portlet API dictates that this method should be called "before the first portlet request is handled". In Liferay, a new instance (and only ONE instance) of your portlet class is created at deploy time, not when you add a portlet on a page. After instantiation, the portal container will call the init() method of the portlet class.

Conclusion: it doesn't make sense at all to retrieve portlet preferences in the init() method of your portlet class, because at that point the portlet doesn't have any context (read: Layout) from which it should retrieve the preferences.




回答2:


If you want to get configuration of portlet initialization, then you can override the init(PortletConfig config) methode from GenericPortlet

public class MyLiferayTestPortlet extends MVCPortlet {

  @Override
  public void init(PortletConfig config) throws PortletException {
    System.out.println("~~~~~~~~~~~~~ ~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ init");
    super.init(config);
    Enumeration<String> parameterNames = config.getInitParameterNames();
    while(parameterNames.hasMoreElements()) {
        String name = parameterNames.nextElement();
        System.out.println("Parameter: " + name + " = " + config.getInitParameter(name));
    }
    System.out.println("~~~~~~~~~~~~~ ~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ /init");
  }
}



回答3:


Why don't you just put the preferences retrieval code to the render phase, and make a flag when the preferences have been validated? This way you only need to do it once.

For example, in the controller you have have a static field

private static boolean checked = false;

And then in the render phase, you check

if(!checked) {
//validate preferences
checked = true;
}


来源:https://stackoverflow.com/questions/12213294/liferay-get-portletid-and-plid-from-initportletconfig-i-e-no-request-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!