我在 JSF2.0 中遇到标签问题,希望有人能指出我做错了什么。这是我在用户界面中得到的内容:
<h:panelGroup>
<h:form id="theForm">
<h:selectOneMenu id="theMenu" value="#{viewBean.selectedItem}">
<f:ajax event="change" render="selectedItemText"/>
<f:selectItem itemLabel=""/>
<f:selectItems value="#{viewBean.selectableItems}"/>
</h:selectOneMenu>
<h:outputText id="selectedItemText" value="#{viewBean.selectedItemText}" />
</h:form>
</h:panelGroup>
这工作得很好 - 我的对话范围支持 bean 有一个方法 setSelectedItem
,当我第一次从菜单中选择不同的项目时,它会被调用并执行其操作;输出文本在前端更新,我很高兴。但是,对菜单选择的进一步更改不会触发通过 ajax 对 setter 的调用。我还在 f:ajax
标记上尝试过使用监听器 - 监听器方法也仅在第一次被调用(代码中的断点来解决这个问题)。
我做错了什么吗?
最佳答案
我也遇到了类似的问题。
下面的第二个命令按钮仅在下面具有 View 参数的 JSF View 中工作一次。添加<f:param>
我的第一个命令按钮解决了问题。这是BalusC的very helpful discussion未涵盖的情况.
<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<f:view>
<f:metadata>
<f:viewParam name="id" value="#{fooManager.millis}" required="true"/>
</f:metadata>
<h:head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</h:head>
<h:body>
<h:form id="fooForm">
<h:commandButton
id="barBbutton"
value="foo:test"
action="#{fooManager.test}">
<f:param name="id" value="1"/>
<f:ajax render="fooMillis1"/>
</h:commandButton>
<p>
<h:outputText id="fooMillis1" value="foo:display1: #{fooManager.millis}"/>
</p>
<p>
<h:outputText id="fooMillis2" value="foo:display2: #{fooManager.millis}"/>
</p>
</h:form>
<h:form id="barForm">
<h:commandButton
id="barButton"
value="bar:test"
action="#{barManager.test}">
<f:ajax render="barMillis1"/>
</h:commandButton>
<p>
<h:outputText id="barMillis1" value="bar:display1: #{barManager.millis}"/>
</p>
<p>
<h:outputText id="barMillis2" value="bar:display2: #{barManager.millis}"/>
</p>
</h:form>
</h:body>
</f:view>
</html>
我的 FooManager 和 BarManager 看起来是一样的:
@ManagedBean
@ViewScoped
public class FooManager {
public long getMillis() {
return millis;
}
public void setMillis(long millis) {
this.millis = millis;
}
public void test() {
setMillis(System.currentTimeMillis());
}
private long millis;
}
当它不工作时,我的 Weblogic/Mojarra 库不会给出任何有用的提示。任何地方都没有错误。经过多次尝试后,我才想出了一个像上面第一个按钮一样的工作按钮。
关于JSF2.0 与 <f :ajax> only works once,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4998817/