1

I have got the following code to get a portlet init parameter "javax.portlet.faces.defaultViewId.view" from FacesContext.

 FacesContext fc = FacesContext.getCurrentInstance();
 ExternalContext externalContext = facesContext.getExternalContext();
 PortletContext portletContext = (PortletContext) externalContext.getContext();
 return String defaultView = portletContext.getInitParameter("javax.portlet.faces.defaultViewId.view");

portlet.xml file contains the init param:

<init-param>
    <name>javax.portlet.faces.defaultViewId.view</name>
    <value>/pages/setup/page.xhtml</value>
</init-param>

My portlet is deployed in Liferay and uses Liferay Faces Bridge. When the above code is executed, I always get null value for defaultValue. Please can someone tell what I am doing wrong?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user872858
  • 770
  • 7
  • 15

2 Answers2

3

In order to avoid a hard-coded dependency on the Portlet API, you can simply use the ExternalContext.getInitParameter(String) method:

ExternalContext externalContext = facesContext.getExternalContext();
String defaultView = externalContext.getInitParameter("javax.portlet.faces.defaultViewId.view");
Neil Griffin
  • 822
  • 5
  • 10
2

I have found a solution to my own problem. To get the portlet init params, I should be using PortletConfig instead of PortletContext. The code snippet below does the job:

ExternalContext externalContext = facesContext.getExternalContext();
PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
PortletConfig config = (PortletConfig) portletRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
String defaultView = config.getInitParameter("javax.portlet.faces.defaultViewId.view");
Parkash Kumar
  • 4,710
  • 3
  • 23
  • 39
user872858
  • 770
  • 7
  • 15
  • If anybody else is wondering: `JavaConstants.JAVAX_PORTLET_CONFIG` is `"javax.portlet.config"`. – david Feb 16 '21 at 14:33