我有一個在WebSphere Portal 6.0上運行的JSR-168 portlet應用程序。
在應用程序的portlet.xml文件,一個特定的portlet定義如下:如何有條件地更改JSR-168 portlet的初始JSP?
<portlet>
<portlet-name>Individual Portlet</portlet-name>
<display-name>Individual Portlet</display-name>
<display-name xml:lang="en">
Individual Portlet
</display-name>
<portlet-class>
com.companyname.util.hibernate.MHFacesHibernatePortlet
</portlet-class>
<init-param>
<name>com.ibm.faces.portlet.page.view</name>
<value>/TEIndividual/TEIndividualView.jsp</value>
</init-param>
<init-param>
<name>wps.markup</name>
<value>html</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
<resource-bundle>
resources.nl.IndividualPortletResource
</resource-bundle>
<portlet-info>
<title>Individual Portlet</title>
</portlet-info>
<portlet-preferences>
<preference>
<name>portletType</name>
<value>individual</value>
</preference>
<preference>
<name>wmmAttribSalesmanId</name>
<value>facsimileTelephoneNumber</value>
</preference>
</portlet-preferences>
</portlet>
注意,它定義了一個名爲「com.ibm.faces.portlet.page.view」具有值「參數/ TEIndividual/TEIndividualView.jsp「,這是在portlet呈現時使用的初始JSP。
我需要根據數據庫查詢的結果有條件地更改該參數的值。
我認爲這可能涉及在MHFacesHibernatePortlet類中的某個地方重定向。
這是否正確,我應該修改什麼類的方法?
編輯,有更多的信息和對兩類源代碼
我添加的代碼下面的MHFacesHibernatePortlet類,也是MHFacesPortlet類其延伸。
在應用程序的portlet.xml文件中,多個portlet都配置爲使用MHFacesHibernatePortlet類,但每個portlet使用不同的初始JSP。上面的「個人門戶」只是一個例子。
我需要有條件地更改一些portlet的初始JSP,所以我有一些代碼應該能夠根據我想要更改的列表來檢查JSP名稱,然後運行Hibernate查詢,然後只在必要時更改JSP名稱。
我在MHFacesPortlet.processAction
看到它讀取相關參數並在某些條件下將其傳遞給request.getPortletSession().setAttribute()
,所以我試圖將我的代碼放在那裏並更改homeJsp
變量,但那不起作用。在添加了一些日誌記錄之後,我發現在使用其中一個portlet訪問頁面時,processAction甚至不會被調用。
來源爲MHFacesHibernatePortlet類:
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();
}
}
}
爲MHFacesPortlet類來源:
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);
}
}
要正確回答這個問題,我們需要查看'MHFacesHibernatePortlet'的來源。 (鑑於'com.ibm.faces ...'的'init-param/name',我猜這個portlet類必須擴展一個基本的IBM portlet類。) – ziesemer 2012-02-01 01:44:24
添加了一些信息和源代碼。試圖改變(在processAction方法),沒有效果。 – 2012-02-01 06:25:22
斯科特 - 請看我更新的答案。 – ziesemer 2012-02-01 13:32:11