我正在为WebSphere Portal 8创建一个Portlet,并且想要检索呈现我的Portlet的页面名称。这很重要,因为根据页面的不同,Portlet将以不同的方式处理内容
我尝试使用NavigationSelectionModel API,但认为我没有正确使用它。我希望这段代码在呈现视图之前发生,然后将代码放入doView方法中。问题是我无法转换ServletRequest / Response,因为doView方法中只有RenderRequest和RenderResponse。
public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
// Declarations
List<ForeignAuthority> faList = new ArrayList<ForeignAuthority>();
String resp;
// Set the MIME type for the render response
response.setContentType(request.getResponseContentType());
// Check if portlet session exists
ForeignAuthoritiesPortletSessionBean sessionBean = getSessionBean(request);
if (sessionBean == null) {
response.getWriter().println("<b>NO PORTLET SESSION YET</b>");
return;
}
try{
Context ctx = new InitialContext();
NavigationSelectionModelHome home = (NavigationSelectionModelHome)
ctx.lookup("portal:service/model/NavigationSelectionModel");
if (home != null) {
NavigationSelectionModelProvider provider =
home.getNavigationSelectionModelProvider();
NavigationSelectionModel model =
provider.getNavigationSelectionModel((ServletRequest)request, (ServletResponse)response);
for (java.util.Iterator i = model.iterator(); i.hasNext(); )
{
NavigationNode node = (NavigationNode) i.next();
if (i.hasNext()) {
System.out.println(node.getObjectID().getUniqueName());
}
}
}
}catch(Exception e){
e.printStackTrace();
}
PortletRequestDispatcher rd = getPortletContext()
.getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
rd.include(request, response);
}
预期结果将是检索呈现portlet的当前页面的页面名称或唯一名称。
答案 0 :(得分:0)
您可以尝试下面的代码片段是否有帮助。您可以获取URI值并从中提取页面名称。
HttpServletRequest httpServletRequest = PortletUtils.getHttpServletRequest(renderRequest); httpServletRequest.getRequestURI();