2012-01-31 34 views
2

注意:此問題已經過大量編輯,因爲已發現一個人爲的示例已經發揮作用。但是,問題仍然基本上與相同,只有這個人爲的例子似乎有效。includeViewParams = true不能與遍歷集合的複合組件結合使用

考慮下面的JSF頁面:

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets"> 
    <f:view contentType="text/html"> 
     <h:head> 
      <title>Page</title> 
     </h:head> 
     <h:body> 
      <ui:composition template="/WEB-INF/templates/myLayout.xhtml"> 
       <ui:define name="metadata"> 
        <f:metadata> 
         <f:viewParam name="foobar" value="#{pageBean.foo}"/> 
        </f:metadata> 
       </ui:define> 
       <ui:define name="content"> 
        <h:form> 
         <h:commandLink value="Click" 
            action="#{util.currentPageAction()}"/> 
        </h:form> 
       </ui:define> 
      </ui:composition> 
     </h:body> 
    </f:view> 
</html> 

而這些豆類:

@Named 
@RequestScoped 
public class PageBean implements Serializable 
{ 
    public String getFoo() 
    { 
     return foo; 
    } 

    public void setFoo(String foo) 
    { 
     this.foo = foo; 
    } 

    private String foo; 
} 
@Named 
@ApplicationScoped 
public class Util implements Serializable 
{ 
    public String currentPageAction() 
    { 
     return FacesContext.getCurrentInstance().getViewRoot().getViewId() + 
        "?faces-redirect=true&includeViewParams=true"; 
    } 
} 

這個人爲的例子其實並不如預期 - 尤其是,它穿過的原始值在重定向時查看URL的參數。因此,例如,如果原始網址是http://localhost:8080/faces/pages/test.xhtml?foo=bar,那麼當我點擊<h:commandLink/>時,整個網址(包括視圖參數)將在重定向後生存。

問題是,當我從這個人爲的例子走向更真實的世界(在我的情況下,一個搜索頁面),視圖參數不會生存的重定向。

下面是它不起作用的真實世界頁面。對不起,這對於一個典型的stackoverflow問題來說有點冗長。

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:ui="http://java.sun.com/jsf/facelets" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:p="http://primefaces.org/ui" 
     xmlns:search="http://java.sun.com/jsf/composite/components/search" 
     xmlns:oc="http://java.sun.com/jsf/composite/components/mysite" 
     xmlns:fn="http://www.mysite.com.au/jsf"> 
    <h:head> 
     <title></title> 
    </h:head> 
    <h:body> 
     <ui:composition template="/WEB-INF/templates/myLayout.xhtml"> 
      <ui:define name="metadata"> 
       <f:metadata> 
        <f:viewParam name="address" value="#{searchBean.address}"/> 
        <f:viewParam name="lng" value="#{searchBean.longitude}" required="#{!facesContext.postback}" requiredMessage="Longitude is required" validatorMessage="Longitude is invalid"> 
         <f:validateDoubleRange minimum="-180" maximum="180"/> 
        </f:viewParam> 
        <f:viewParam name="lat" value="#{searchBean.latitude}" required="#{!facesContext.postback}" requiredMessage="Latitude is required" validatorMessage="Latitude is invalid"> 
         <f:validateDoubleRange minimum="-90" maximum="90"/> 
        </f:viewParam> 
        <f:viewParam name="gender" value="#{searchBean.gender}"/> 
        <f:viewParam name="language" value="#{searchBean.spokenLanguageId}"/> 
        <f:viewParam name="service" value="#{searchBean.serviceId}"/> 
        <f:viewParam name="rangeKm" value="#{searchBean.rangeKm}" validatorMessage="Range (km) is invalid"> 
         <f:validateLongRange minimum="1"/> 
        </f:viewParam> 
        <f:viewParam name="weeksOffset" value="#{searchBean.weeksOffset}"/> 
        <f:event type="preRenderView" listener="#{searchBean.preRender(e)}"/> 
       </f:metadata> 
      </ui:define> 
      <ui:define name="windowTitle">#{searchBean.address}</ui:define> 
      <ui:define name="scripts"> 
       <script type="text/javascript"> 
        var mysite = mysite || {}; 
        mysite.longitude  = #{searchBean.longitude}; 
        mysite.latitude  = #{searchBean.latitude}; 
        mysite.defaultAddress = "#{applicationBean.defaultAddress}"; 
        mysite.region   = "#{applicationBean.region}"; 
        mysite.baseUrl  = "#{applicationBean.baseUrl}"; 
       </script> 
       <h:outputScript library="javascript" name="map2.js"/> 
       <h:outputScript name="geocode.js" library="javascript" target="head"/> 
       <h:outputScript name="search.js" library="javascript" target="head"/> 
      </ui:define> 

      <ui:define name="rightSidebar"> 
       <div class="ocSearch_sidebarSection"> 
        <oc:map styleClass="search"/> 
        <div> 
         <a style="font-size:11px;text-decoration:underline;" href="#" onclick="mysite.resetMap();return false;">Reset</a> 
        </div> 
       </div> 

       <oc:context-menu-container isFirstChild="false"> 
        <oc:context-menu title="Search Options"> 
         <form id="searchForm" method="get" onsubmit="return mysite.geocode(this);"> 
          <div style="clear:both;float:left;"> 
           <label for="ocSearchRadius">Service Providers within</label> 
           <div id="ocSearchRadius"> 
            <input type="text" id="ocRangeKm" name="rangeKm" value="#{searchBean.rangeKm}" style="width:20px;float:left;"/> 
            <div style="width:40px;margin-top:5px;float:left;overflow:hidden;text-align:center;vertical-align:bottom;">km of</div> 
            <input type="text" id="ocAddress" name="address" value="#{searchBean.address}" style="width:104px;float:left;"/> 
           </div> 
          </div> 
          <div style="float:left;"> 
           <div style="width:60px;margin-right:10px;float:left;"> 
            <label for="ocGender" style="display:block;">Gender</label> 
            <select id="ocGender" name="gender" style="width:50px;"> 
             <h:panelGroup rendered="#{searchBean.gender eq 'any'}"> 
              <option value="any" selected="selected">Any</option> 
              <option value="female">Female</option> 
              <option value="male">Male</option> 
             </h:panelGroup> 
             <h:panelGroup rendered="#{searchBean.gender eq 'female'}"> 
              <option value="any">Any</option> 
              <option value="female" selected="selected">Female</option> 
              <option value="male">Male</option> 
             </h:panelGroup> 
             <h:panelGroup rendered="#{searchBean.gender eq 'male'}"> 
              <option value="any">Any</option> 
              <option value="female">Female</option> 
              <option value="male" selected="selected">Male</option> 
             </h:panelGroup> 
            </select> 
           </div> 
           <div style="float:left;"> 
            <label for="ocLanguage">Language</label> 
            <select id="ocLanguage" name="language" style="width:176px;"> 
             <ui:repeat value="#{commonRequestBean.spokenLanguageItems}" var="item"> 
              <h:panelGroup rendered="#{searchBean.spokenLanguageId eq item.value}"> 
               <option value="#{item.value}" selected="yes">#{item.label}</option> 
              </h:panelGroup> 
              <h:panelGroup rendered="#{searchBean.spokenLanguageId ne item.value}"> 
               <option value="#{item.value}">#{item.label}</option> 
              </h:panelGroup> 
             </ui:repeat> 
            </select> 
           </div> 
          </div> 
          <div style="float:left;"> 
           <label for="ocService">Reason for visit</label> 
           <select id="ocService" name="service" style="width:176px;" > 
            <ui:repeat value="#{commonRequestBean.serviceItems}" var="item"> 
             <h:panelGroup rendered="#{searchBean.serviceId eq item.value}"> 
              <option value="#{item.value}" selected="yes">#{item.label}</option> 
             </h:panelGroup> 
             <h:panelGroup rendered="#{searchBean.serviceId ne item.value}"> 
              <option value="#{item.value}">#{item.label}</option> 
             </h:panelGroup> 
            </ui:repeat> 
           </select> 
          </div> 

          <div style="float:left;"> 
           <label for="ocStartDate">From</label> 
           <select id="ocStartDate" name="weeksOffset" style="width:176px;"> 
            <ui:repeat value="#{fn:selectableDates(13)}" var="date" varStatus="dateStatus"> 
             <h:panelGroup rendered="#{date.value eq searchBean.weeksOffset}"> 
              <option value="#{date.value}" selected="yes">#{date.label}</option> 
             </h:panelGroup> 
             <h:panelGroup rendered="#{date.value ne searchBean.weeksOffset}"> 
              <option value="#{date.value}">#{date.label}</option> 
             </h:panelGroup> 
            </ui:repeat> 
           </select> 
          </div> 

          <div style="margin-top:8px;float:left;"> 
           <p:button id="searchAgainButton" value="Search Again"/> 
           <script type="text/javascript"> 
            //<![CDATA[ 
            $(function(){ 
             // Replace the Search Again button with a clone whose type is submit. 
             var oldButton = $('#searchAgainButton'); 
             var newButton = oldButton.clone(true); 
             newButton.attr("type","submit"); 
             newButton.attr("id","searchAgainButtonClone"); 
             newButton.removeAttr("onclick"); 
             newButton.insertBefore(oldButton); 
             oldButton.remove(); 
             newButton.attr("id","searchAgainButton"); 
            }); 
            //]]> 
           </script> 
          </div> 

          <input type="hidden" id="ocLng"  name="lng"  value="#{searchBean.longitude}"/> 
          <input type="hidden" id="ocLat"  name="lat"  value="#{searchBean.latitude}"/> 
         </form> 
         <script> 
          // Ensure the form fields are set to the current values. (Webkit bug workaround.) 
          $(function(){ 
           $('#ocRangeKm').val('#{searchBean.rangeKm}'); 
           $('#ocAddress').val('#{searchBean.address}'); 
           $('#ocGender').val('#{searchBean.gender}'); 
           $('#ocLanguage').val('#{searchBean.spokenLanguageId}'); 
           $('#ocService').val('#{searchBean.serviceId}'); 
           $('#ocStartDate').val('#{searchBean.weeksOffset}'); 
          }); 
         </script> 
        </oc:context-menu> 
       </oc:context-menu-container> 
      </ui:define> 

      <ui:define name="content"> 
       <div id="ocSearchResultsHeader" class="fixed"> 
        <div style="margin-left:10px;"> 
         <h3 style="margin:8px 0 4px 0;">#{searchBean.searchResultsTitle}</h3> 
         <span class="help">Click a time to book #{searchBean.service.indefiniteArticle} #{searchBean.service.name}</span> 
        </div> 
       </div> 
       <div class="ocSearch_resultSet"> 
        <h:form> 
         <h:commandLink value="Foobar" action="#{util.currentPageAction}"/> 
        </h:form> 
        <search:result resultSet="#{searchBean.results}" startDate="#{searchBean.startDate}"/> 
       </div> 
      </ui:define> 
     </ui:composition> 
    </h:body> 
</html> 

底層豆,searchBean,是請求範圍就像人爲的例子。

更新:如果我刪除<search:result/>複合組件實例,它現在可以工作。這完全出乎意料。同樣,如果我將組件實例留在那裏並剝離組件本身,它就可以工作。所以組件中有些東西會干擾別的東西。奇怪的。我會看看我能否弄清楚它是什麼。

更新:這看起來像一個錯誤。如果我用這個替換複合組件:

<ui:repeat value="#{searchBean.results} var="item" varStatus="itemStatus"> 
    <h:outputText value="#{item.mapMarker}"/> 
    <br/> 
</ui:repeat> 

它也會失敗,就像它對複合組件一樣。

同樣,這種失敗:

<h:dataTable value="#{searchBean.results}" var="item"> 
    <h:column> 
     <h:outputText value="#{item.mapMarker}"/> 
     <br/> 
    </h:column> 
</h:dataTable> 

在我的具體情況,有三種結果,這樣我就可以在他們手動循環這樣:

<ui:param name="results" value="#{searchBean.results}"/> 
<br/> 
<h:outputText value="#{results.get(0).mapMarker}"/> 
<br/> 
<h:outputText value="#{results.get(1).mapMarker}"/> 
<br/> 
<h:outputText value="#{results.get(2).mapMarker}"/> 
<br/> 

和它的作品。

所以有一些關於迭代<h:commandLink/>的結果。

究竟會出現什麼問題?還有另一個Mojarra錯誤,其中一個簡單的用例失敗了?

最終更新:縮小與人爲的例子的問題,並提出了問題here

+0

對不起@BalusC - 我看到你刪除了你的答案。我找到了問題 - 請參閱「最終更新」中的鏈接。 – 2012-01-31 14:19:55

+0

我刪除它,因爲它沒有回答你的問題:)隨意刪除當前的問題,以及它的方式太廣泛。你的新問題要好得多。 – BalusC 2012-01-31 14:23:21

+0

我的新問題是*微微*錯誤。但是這使得它變得更簡單。固定。 – 2012-01-31 14:29:59

回答

1

這是一個錯誤的doosey,我無法弄清楚它是JDK7,Mojarra,EJB容器,一般的GlassFish還是兩個或更多這些的糟糕組合。

考慮下面的代碼片段從SearchBean

public List<NormalizedSearchResult> getResults() 
{ 
    return searchEjb.findByLocation(getRangeKm(), 
            getLongitude(), 
            getLatitude(), 
            gender, 
            getSpokenLanguageId(), 
            getServiceId(), 
            1, 
            99, 
            getStartDate().toDateMidnight().toDate(), 
            7); 
} 

public List<NormalizedSearchResult> getResults2() 
{ 
    float lng = longitude; 
    float lat = latitude; 
    return searchEjb.findByLocation(getRangeKm(), 
            lng, 
            lat, 
            gender, 
            getSpokenLanguageId(), 
            getServiceId(), 
            1, 
            99, 
            getStartDate().toDateMidnight().toDate(), 
            7); 
} 

public List<NormalizedSearchResult> getResults3() 
{ 
    float lng = getLongitude(); 
    float lat = getLatitude(); 
    return searchEjb.findByLocation(getRangeKm(), 
            lng, 
            lat, 
            gender, 
            getSpokenLanguageId(), 
            getServiceId(), 
            1, 
            99, 
            getStartDate().toDateMidnight().toDate(), 
            7); 
} 

public List<NormalizedSearchResult> getResults4() 
{ 
    float lng = longitude; 
    float lat = latitude; 
    return searchEjb.findByLocation(getRangeKm(), 
            138.5999594f, 
            -34.9286212f, 
            gender, 
            getSpokenLanguageId(), 
            getServiceId(), 
            1, 
            99, 
            getStartDate().toDateMidnight().toDate(), 
            7); 
} 

public List<NormalizedSearchResult> getResults5() 
{ 
    float lng = 138.5999594f; 
    float lat = -34.9286212f; 
    return searchEjb.findByLocation(getRangeKm(), 
            lng, 
            lat, 
            gender, 
            getSpokenLanguageId(), 
            getServiceId(), 
            1, 
            99, 
            getStartDate().toDateMidnight().toDate(), 
            7); 
} 

public Float getLongitude() 
{ 
    return longitude; 
} 

public void setLongitude(Float longitude) 
{ 
    this.longitude = longitude; 
} 

public Float getLatitude() 
{ 
    return latitude; 
} 

public void setLatitude() 
{ 
    this.latitude = latitude; 
} 

private Float longitude; 
private Float latitude; 

無論這些片斷的,包括在search.xhtml時,引起includeViewParams=true失敗對上述<h:commandLink/>

<ui:repeat value="#{searchBean.results}"/> 
<ui:repeat value="#{searchBean.results2}"/> 
<ui:repeat value="#{searchBean.results3}"/> 
<ui:repeat value="#{searchBean.results4}"/> 

然而,這一個不具有相同的不良影響:

<ui:repeat value="#{searchBean.results5}"/> 

也沒有這樣的:

<ui:param name="results" value="#{pageBean.results}"/> 
<h:outputText value="#{results.get(0).mapMarker}"/> 
<br/> 
<h:outputText value="#{results.get(1).mapMarker}"/> 
<br/> 
<h:outputText value="#{results.get(2).mapMarker}"/> 
<br/> 

當然,當使用includeViewParams=true作品沒有這些XHTML代碼段。

我再變longitudelatitude私有變量的類型從Floatfloat,不再有這些失敗對於任何上述情況。

WTF ?!真的嗎?

任何人都可能想象在渲染視圖的早期階段拆箱視圖參數會打破includeViewParams=true

評論JAVASERVERFACES-2260