【发布时间】:2012-02-23 03:29:22
【问题描述】:
注意:这个问题已经过大量编辑,因为一个人为的例子被发现有效。但是,问题基本上与只有人为的示例似乎有效。
考虑以下 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。因此,例如,如果原始 URL 是 http://localhost:8080/faces/pages/test.xhtml?foo=bar,当我单击 <h:commandLink/> 时,完整的 URL - 包括视图参数 - 将在重定向后继续存在。
问题是,当我从这个人为的示例转到更真实的世界(在我的例子中是搜索页面)时,视图参数不会在重定向后仍然存在。
这是它不起作用的真实页面。抱歉,典型的 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>
底层 bean 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。
【问题讨论】:
-
对不起@BalusC - 我看到你删除了你的答案。我已经确定了问题 - 请参阅“最终更新”中的链接。
-
我删除了它,因为它没有回答您的问题 :) 随意删除当前问题以及它过于宽泛。你的新问题好多了。
-
我的新问题略有错误。但这使它变得更加简单。固定。
-
好的,我把它留在这里是因为
<ui:repeat/>在这种情况下实际上会干扰<h:commandLink/>,而在另一个问题中,非常简单的示例无论如何都失败了。所以也许我的真实世界模板有一些东西,结合页面本身中的<ui:repeat/>与<h:commandLink/>一起搞砸了。
标签: java jsf-2 post-redirect-get