2012-09-09 151 views
0

我無法讓preRenderView事件偵聽器在JSF 2.1中的GET請求上工作。JSF 2.1 preRenderEvent不適用於GET請求

我發現了很多關於它,但似乎沒有任何工作如:
Conditional redirection in JSF
http://andyschwartz.wordpress.com/2009/07/31/whats-new-in-jsf-2/#get-prerenderview-event
http://developer.am/j2eetutorial/jsf/?page=jsf-2-prerenderviewevent-example
JSF, Spring, and the PreRenderViewEvent
http://balusc.blogspot.dk/2011/09/communication-in-jsf-20.html#ProcessingGETRequestParameters

我有4個插入塊模板,我曾試圖在所有這些地方插入事件代碼,但沒有任何運氣。我已經嘗試使用和不使用圍繞它的f:metadata標籤。

<f:event type="preRenderView" listener="#{applicationData.redirectIfNoResults}" /> 

豆:

@ManagedBean 
@ApplicationScoped 
public class ApplicationData implements Serializable { 
    public void redirectIfNoResults() throws IOException { 
     if (getTotal() < 1) { 
      ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext(); 
      ec.redirect(ec.getRequestContextPath() + "/noResults.xhtml"); 
     } 
    } 
    ... 
} 

模板:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core"> 
    <ui:insert name="beforeHeader" /> 
    <f:view> 
     <ui:insert name="inView" /> 
    </f:view> 
    <h:head> 
     <meta http-equiv="cache-control" content="no-store" /> 
     <link href="style.css" rel="stylesheet" type="text/css" /> 
     <title>Quick Poll</title> 
     <ui:insert name="header" /> 
    </h:head> 
    <h:body> 
     <h1>Quick Poll</h1> 
     <ui:insert name="content" /> 
    </h:body> 
</html> 

查看:

<ui:define name="content"> 
     #{applicationData.question}?<p/> 
     <h:panelGrid columns="3" border="0"> 
       Yes: 
       <h:panelGrid bgcolor="black" height="20" width="#{300*applicationData.yes/applicationData.total}"/> 
       #{applicationData.yes} 
       <h:outputText value="No:"/> 
       <h:panelGrid bgcolor="black" height="20" width="#{300*applicationData.no/applicationData.total}"/> 
       #{applicationData.no} 
     </h:panelGrid> 
    </ui:define> 
</ui:composition> 

請幫我弄清楚如何得到它的工作..

更新1:
通過BalusC的建議,但它仍然沒有工作,我已經修改..

模板:

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core"> 
    <h:head> 
     <meta http-equiv="cache-control" content="no-store" /> 
     <link href="style.css" rel="stylesheet" type="text/css" /> 
     <title>Quick Poll</title> 
     <ui:insert name="header" /> 
    </h:head> 
    <h:body> 
     <h1>Quick Poll</h1> 
     <ui:insert name="content" /> 
    </h:body> 
</html> 

查看:

<?xml version='1.0' encoding='UTF-8' ?> 
<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:c="http://java.sun.com/jsp/jstl/core" 
    xmlns:f="http://java.sun.com/jsf/core" 
    template="template.xhtml"> 
    <ui:define name="content"> 
     <f:event listener="#{applicationData.redirectIfNoResults}" type="preRenderView"></f:event> 
     #{applicationData.question}?<p/> 
     <h:panelGrid columns="3" border="0"> 
       Yes: 
       <h:panelGrid bgcolor="black" height="20" width="#{300*applicationData.yes/applicationData.total}"/> 
       #{applicationData.yes} 
       <h:outputText value="No:"/> 
       <h:panelGrid bgcolor="black" height="20" width="#{300*applicationData.no/applicationData.total}"/> 
       #{applicationData.no} 
     </h:panelGrid> 
    </ui:define> 
</ui:composition> 

回答

0

我最終在視圖範圍bean中使用PostConstruct方法制作解決方案。
像這樣:Initializng a Backing Bean With Parameters on Page Load with JSF 2.0

豆:

@ManagedBean 
@ViewScoped 
public class ResultsController { 
    @ManagedProperty(value="#{applicationData.total}") 
    private int total; 
    @ManagedProperty(value="#{applicationData.yes}") 
    private int yes; 
    @ManagedProperty(value="#{applicationData.no}") 
    private int no; 

    @PostConstruct 
    public void postConstruct() { 
     if (getTotal() < 1) { 
      ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext(); 
      try { 
       ec.redirect(ec.getRequestContextPath() + "/noResults.jsf"); 
      } catch (IOException e) { 
       System.out.println("noResults.jsf redirect failed."); 
       e.printStackTrace(); 
      } 
     } 
    } 
    ... 
} 

查看:

<?xml version='1.0' encoding='UTF-8' ?> 
<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:c="http://java.sun.com/jsp/jstl/core" 
    xmlns:f="http://java.sun.com/jsf/core" 
    template="template.xhtml"> 
    <ui:define name="content"> 
     #{applicationData.question}?<p/> 
     <h:panelGrid columns="3" border="0"> 
       Yes: 
       <h:panelGrid bgcolor="black" height="20" width="#{300*resultsController.yes/resultsController.total}"/> 
       #{resultsController.yes} 
       <h:outputText value="No:"/> 
       <h:panelGrid bgcolor="black" height="20" width="#{300*resultsController.no/resultsController.total}"/> 
       #{resultsController.no} 
     </h:panelGrid> 
    </ui:define> 
</ui:composition> 
0

ComponentSystemEvent似乎在方法簽名中缺失。

方法需要看起來像這樣:

public void newRequest(final ComponentSystemEvent event) { 
System.out.println("Someone requested me"); 
} 

,然後將呼叫者在模板或獨立意見,認爲不會有什麼差別。

<f:event listener="#{userSessionAction.newRequest}" type="preRenderView"></f:event> 
+0

這樣的說法是可選的,所以這個答案並不適用。 – BalusC

+0

我以前也試過這個簽名,並且在文檔中發現我並不需要。我在beforeHeader標籤中再次嘗試過 - 沒有任何改變。你可以使用GET請求來處理頁面嗎?你能讓我的代碼工作嗎? –

2

<f:view>不正確地使用,它具有包住整個圖。刪除它(JSF將隱式創建一個),或者至少讓它包裝整個視圖,包括<h:head><h:body>標記。

順便說一下,<f:event>不需要去<f:metadata>。這僅適用於<f:viewParam>。取決於<f:viewParam>的結果的聽衆確實經常用於唯一的自我記錄目的被放置在相同的<f:metadata>塊中,但這因此不是<f:event>本身的要求。

就你而言,將它放在<ui:define name="content">會更容易。

+0

好的..我已經在內容標籤內添加了這一行:''但沒有變化.. –

+0

您是否刪除/修復了錯誤''以及? ''即隱含地附在''上,但如果它錯誤/被破壞,那麼這個機會是合理的,它不會被調用。 – BalusC

+0

是 - 我已更新該帖子。 –