这是问题的延伸 Navigate to external URL from a backing bean?
我需要在backing bean方法中使会话无效,然后转发到外部URL。 outputLink组件无法调用操作方法(在本例中为bean的注销方法)。 如果我使用commandLink,则会调用action方法,但在会话失效后,会在转发到外部URL之前创建另一个会话。
更具体一点:我有一个视图,可以在主页上加载项目列表。主页上有一堆链接(其中一个是注销图像/按钮),它们来自模板xhtml。在模板xhtml文件中,我有commandLink代码,它调用LogoutBean.java的logout方法。该方法按预期调用正常,但是当我在HomeBean的PostConstruct方法中放置一个断点时,我看到在使会话无效之后调用此方法然后它转发到外部URL。如何防止这种情况?
代码:
所引用:
<h:form>
<p:panel id="HomeHeaderPanel" styleClass="tableheaderfont" style="width:98%">
<h:panelGrid id="filterGrid" columns="2" cellpadding="0" styleClass="normalfont" style="width: 98%" columnClasses="leftalign,rightalign">
<h:panelGroup>
<h:link outcome="reviewHome.xhtml?faces-redirect=true" id="homeLink"
title="Review Home">
<h:graphicImage value="images/home_small.png" id="homeIcon" alt="Review Home"/>
</h:link>
<p:spacer width="5"/>
<h:commandLink action="#{logoutBean.logout}" >
<h:graphicImage value="images/logout_small.png" id="logoutIcon" alt="Logout" title="Logout"/>
</h:commandLink>
</h:panelGroup>
</h:panelGrid>
</p:panel>
</h:form>
reviewHome.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui">
<f:view contentType="text/html">
<ui:composition template="template.xhtml">
<ui:define name="content">
<h:form styleClass="form" id="ptpReviewForm">
<f:event listener="#{homeBean.refresh}" type="preRenderView" />...content here...
</h:form>
</ui:define>
</ui:composition>
</f:view>
</html>
HomeBean.java:
@ManagedBean
@SessionScoped
public class HomeBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = 3303546811067129792L;
public HomeBean(){}
@EJB
private ReviewEJB reviewEJB;
private List<HomeObject> homeList;
@SuppressWarnings("unused")
@PostConstruct
private void populateItpList()
{
logger.debug("In the PostConstruct method");
homeList = reviewEJB.getList();
}
//This method is loaded every time the Review Home view loads (prerenderView event)
public void refresh()
{
String refresh = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("refresh");
if(null != refresh && refresh.equalsIgnoreCase("true"))
{
homeList = reviewEJB.getList("another");
}
}
LogoutBean.java:
@ManagedBean
@SessionScoped
public class LogoutBean {
public void logout() throws ServletException
{
try {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
((HttpServletRequest) ec.getRequest()).logout();
HttpSession session = (HttpSession) ec.getSession(false);
// invalidate session
if (session != null) {
session.invalidate();
}
ec.redirect("/anotherWebAppContext/forward.jsp?link=review");
} catch (IOException e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
这是由<f:event type="preRenderView">
引起的,并且行为已完全指定。
基本上你想要杀死那个事件。我从来没有在这个构造中尝试过,所以我无法从经验中判断它是否有效,但您可以尝试通过UIViewRoot#getViewListenersForEventClass()
获取事件,然后在重定向之前将其清除。
UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
viewRoot.getViewListenersForEventClass(PreRenderViewEvent.class).clear();
// ...
理论上应该这样做。