title: How to conditionally change the initial JSP for a JSR-168 portlet?
body: <p>I have a JSR-168 portlet application running on WebSphere Portal 6.0.<br>
  In the application's portlet.xml file, a specific portlet is defined as follows:</p>
  
  <pre>
  <code>&lt;portlet>
          &lt;portlet-name>Individual Portlet&lt;/portlet-name>
          &lt;display-name>Individual Portlet&lt;/display-name>
          &lt;display-name xml:lang="en">
              Individual Portlet
          &lt;/display-name>
          &lt;portlet-class>
              com.companyname.util.hibernate.MHFacesHibernatePortlet
          &lt;/portlet-class>
          &lt;init-param>
              &lt;name>com.ibm.faces.portlet.page.view&lt;/name>
              &lt;value>/TEIndividual/TEIndividualView.jsp&lt;/value>
          &lt;/init-param>
          &lt;init-param>
              &lt;name>wps.markup&lt;/name>
              &lt;value>html&lt;/value>
          &lt;/init-param>
          &lt;expiration-cache>0&lt;/expiration-cache>
          &lt;supports>
              &lt;mime-type>text/html&lt;/mime-type>
              &lt;portlet-mode>view&lt;/portlet-mode>
          &lt;/supports>
  
          &lt;resource-bundle>
              resources.nl.IndividualPortletResource
          &lt;/resource-bundle>
          &lt;portlet-info>
              &lt;title>Individual Portlet&lt;/title>
          &lt;/portlet-info>
          &lt;portlet-preferences>
              &lt;preference>
                  &lt;name>portletType&lt;/name>
                  &lt;value>individual&lt;/value>
              &lt;/preference>
              &lt;preference>
                  &lt;name>wmmAttribSalesmanId&lt;/name>
                  &lt;value>facsimileTelephoneNumber&lt;/value>
              &lt;/preference>
          &lt;/portlet-preferences>
      &lt;/portlet></code>
  </pre>
  
  <p>Note that it defines a parameter named "com.ibm.faces.portlet.page.view" with the value "/TEIndividual/TEIndividualView.jsp", which is the initial JSP used when the portlet is rendered.<br>
  I need to conditionally change the value of that parameter based on the result of a database query.</p>
  
  <p>I think this might involve a redirect somewhere in the MHFacesHibernatePortlet class.<br>
  Is that correct, and what method of the class should I modify?</p>
  
  <p><strong>Edit, with more info and source code for two classes...</strong></p>
  
  <p>I've added code for the MHFacesHibernatePortlet class below, and also the MHFacesPortlet class which it extends.</p>
  
  <p>In the application's portlet.xml file, multiple portlets are all configured to use the MHFacesHibernatePortlet class, but each portlet uses a different initial JSP. The "Individual Portlet" above is just one example.</p>
  
  <p>I need to conditionally change the initial JSP only for some of the portlets, so I have some code that should be able to check the JSP name against a list of the ones I want to change, then run a Hibernate query, then change the JSP name only if necessary.</p>
  
  <p>I saw in <code>MHFacesPortlet.processAction</code> that it reads the relevant parameter and passes it to <code>request.getPortletSession().setAttribute()</code> under some conditions, so I tried to put my code there and change the <code>homeJsp</code> variable, but that didn't work. After adding some logging I found that processAction isn't even called when I go to a page with one of these portlets.</p>
  
  <p>Source for the MHFacesHibernatePortlet class:</p>
  
  <pre>
  <code>package com.companyname.util.hibernate;
  
  import java.io.IOException;
  import javax.portlet.PortletException;
  import javax.portlet.RenderRequest;
  import javax.portlet.RenderResponse;
  import com.companyname.util.portlet.MHFacesPortlet;
  
  public class MHFacesHibernatePortlet extends MHFacesPortlet {
      /*
       * (non-Javadoc)
       *
       * @see com.ibm.faces.webapp.FacesGenericPortlet#doConfigure(javax.portlet.RenderRequest,
       *      javax.portlet.RenderResponse)
       */
      public void doConfigure(RenderRequest arg0,RenderResponse arg1)
      throws PortletException,IOException {
          super.doConfigure(arg0,arg1);
          try {
              HibernateUtil.commitTransaction();
          } finally {
              HibernateUtil.closeSession();
          }
      }
  
      /*
       * (non-Javadoc)
       *
       * @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest,
       *      javax.portlet.RenderResponse)
       */
      public void doView(RenderRequest arg0,RenderResponse arg1)
      throws PortletException,IOException {
          super.doView(arg0,arg1);
          // Close and commit open hibernate transactions
          try {
              HibernateUtil.commitTransaction();
          } finally {
              HibernateUtil.closeSession();
          }
      }
  }</code>
  </pre>
  
  <p>Source for the MHFacesPortlet class:</p>
  
  <pre>
  <code>package com.companyname.util.portlet;
  
  import javax.portlet.ActionRequest;
  import javax.portlet.ActionResponse;
  import javax.portlet.PortletException;
  import javax.portlet.PortletMode;
  import com.ibm.faces.webapp.FacesGenericPortlet;
  
  public class MHFacesPortlet extends FacesGenericPortlet {
      private static final String FACES_CURRENT_PAGE_STUB="com.ibm.faces.portlet.page.";
  
      /** parameter used to fire an extended action */
      public static final String PARAMETER_EXTENDED_ACTION="extAction";
  
      /** Action to reset view mode of the portlet */
      public static final String ACTION_VIEW_MODE_RESET="viewReset";
  
      public MHFacesPortlet() {
          super();
      }
  
      /*
       * (non-Javadoc)
       *
       * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest,
       *      javax.portlet.ActionResponse)
       */
      public void processAction(ActionRequest request,ActionResponse response)
      throws PortletException {
          // Check if we need to process an extended action
          String extendedAction=(String)request
              .getParameter(PARAMETER_EXTENDED_ACTION);
  
          if (extendedAction!=null&&!extendedAction.equals("")) {
              // Reset view mode
              if (extendedAction.equals(ACTION_VIEW_MODE_RESET)) {
                  PortletMode portletMode=request.getPortletMode();
                  String modeString=null;
                  if (portletMode.equals(PortletMode.EDIT)) modeString="edit";
                  else if (portletMode.equals(PortletMode.VIEW)) modeString="view";
                  else if (portletMode.equals(PortletMode.HELP)) modeString="help";
                  else modeString=portletMode.toString();
  
                  String homeJsp=getPortletConfig().getInitParameter(
                      FACES_CURRENT_PAGE_STUB+modeString);
                  request.getPortletSession().setAttribute(
                      FACES_CURRENT_PAGE_STUB+modeString,homeJsp);
              }
  
          }
  
          //delegate to ibm faces
          super.processAction(request,response);
      }
  }</code>
  </pre>