【发布时间】:2016-06-04 05:58:03
【问题描述】:
我有一个带有非常简单表单的页面,它提交仅针对同一表单中的组件的 ajax 请求。在同一页面中(但在表单之外),还有一个 ui:repeat 迭代从 request scoped 托管 bean 返回的数组(假设是产品类别列表)。此 bean 没有绑定在表单中的属性,并且除了 ui:repeat 标记的 value 属性之外,不能以任何其他方式访问。我不明白为什么 JSF 需要在每个 ajax 回发上重新创建请求范围的 bean,只是 好像 我要求渲染这个外部 ui:repeat(与表单无关)以及表单内的一些组件。
这是一个错误吗?或者这是一个预期的行为?当然,我可以将 bean 注释为 ViewScoped,但我看不出将类别存储在视图范围中的原因,因为它们在回发之间是完全静态的。
我发现的另一个解决方案/解决方法是仅在非 ajax 请求的情况下呈现 ui:repeat:
<ul>
<ui:repeat value="#{someRequestScopedBean.categories}" var="category" rendered="#{not facesContext.partialViewContext.ajaxRequest}">
<li>#{category.name}</li>
</ui:repeat>
</ul>
但我不知道这是否会导致问题并且看起来不太清楚。
测试用例
index.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:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h1>Test page</h1>
<p>Random jokes</p>
<ul>
<ui:repeat value="#{oneLiners.list}" var="oneliner">
<li>#{oneliner}</li>
</ui:repeat>
</ul>
<h:form>
<h:selectOneMenu value="#{backingBean.greeting}" hideNoSelectionOption="true">
<f:selectItem value="#{null}" itemLabel="Select a greeting..." noSelectionOption="true"/>
<f:selectItems value="#{backingBean.greetings}"/>
<f:ajax render="@this btn"/>
</h:selectOneMenu>
<h:commandButton id="btn" value="Say Hello!" disabled="#{empty backingBean.greeting}">
<f:ajax render="otxt"/>
</h:commandButton>
<h:outputText id="otxt" value="#{backingBean.greeting}, Maurizio!" style="display: #{empty backingBean.greeting ? 'none' : 'block'}"/>
</h:form>
</h:body>
</html>
请求作用域 bean:
package testuirepajax;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
/**
*
* @author maurizio
*/
@ManagedBean
@RequestScoped
public class OneLiners {
private String[] list;
public OneLiners() {
System.out.println("testuirepajax.OneLiners.<init>()");
list = new String[] {
"Life is wonderful. Without it we'd all be dead.",
"Daddy, why doesn't this magnet pick up this floppy disk?",
"Daddy, what does FORMATTING DRIVE C mean?",
"Never forget: 2 + 2 = 5 for extremely large values of 2.",
"C:\\ is the root of all directories."
};
}
public String[] getList() {
System.out.println("testuirepajax.OneLiners.getList()");
return list;
}
}
表单支持 bean:
package testuirepajax;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
/**
*
* @author maurizio
*/
@ManagedBean
@ViewScoped
public class BackingBean {
private String[] greetings;
private String greeting;
public BackingBean() {
System.out.println("testuirepajax.BackingBean.<init>()");
greetings = new String[] {
"Hello", "Hi", "Good morning", "Good evening", "Good night"
};
}
public String[] getGreetings() {
return greetings;
}
public void setGreeting(String greeting) {
this.greeting = greeting;
}
public String getGreeting() {
return greeting;
}
}
检查容器的输出。使用 Payara 服务器(附带 Mojarra 2.2.12),我看到如下行:
Informazioni: testuirepajax.OneLiners.<init>()
Informazioni: testuirepajax.OneLiners.getList()
Informazioni: testuirepajax.OneLiners.getList()
Informazioni: testuirepajax.OneLiners.getList()
Informazioni: testuirepajax.OneLiners.getList()
Informazioni: testuirepajax.OneLiners.getList()
Informazioni: testuirepajax.OneLiners.<init>()
Informazioni: testuirepajax.OneLiners.getList()
Informazioni: testuirepajax.OneLiners.getList()
Informazioni: testuirepajax.OneLiners.getList()
Informazioni: testuirepajax.OneLiners.getList()
Informazioni: testuirepajax.OneLiners.getList()
从菜单中选择元素或单击“打个招呼!”时按钮。
【问题讨论】:
-
是否包含输入组件?哪个 JSF 实现/版本? Mojarra 2.2.7 和 2.1.29 中修复了类似的错误。那里有很好的解决方法!如有必要,请为未来的读者添加
<!-- comment -->,以免他们被迷惑。 -
@BalusC 1) 该表单包含一对
h:selectOneMenu和一个h:button,但也适用于包含h:commandButton的表单。 2) 莫哈拉 2.2.12 -
他们在 ui:repeat?那么,这种副作用是不可避免的,因为 JSF 无论如何都需要重申它,以便在回发期间恢复这些组件的状态。不幸的是,JSF(鸡蛋和所有)不支持“部分恢复”。
-
@BalusC
ui:repeat内没有输入组件。它们仅在表格内。我现在正在写一个测试用例。 -
@BalusC 我不知道是否相关,但我正在使用捆绑 Mojarra 2.2.12 的 Payara 服务器。