我有一个@ViewScopedwordController CDI bean.它有一个向a addWord()添加变量的方法.没有AJAX它可以正常工作,但只要我包含它,变量在方法中始终为null .在ajax与JSF一起工作的方式上有一些我缺少的东西.这里出了什么问题?currentWordListf:ajaxcurrentWordaddWord()
<h:form id="saveForm" styleClass="form">
<h:inputText value="#{wordController.currentWord}" id="words" />
<h:commandButton action="#{wordController.addWord()}" type="submit" value="+">
<f:ajax render="wordlist" />
</h:commandButton>
<h:panelGroup id="wordlist">
....
Run Code Online (Sandbox Code Playgroud)
f:ajax默认为execute="@this"(在这种情况下等于commandButton),因此不执行inputText(≈已提交).这就是为什么它在行动中为空.只需添加
<f:ajax execute="@form" render="wordlist" />
Run Code Online (Sandbox Code Playgroud)
执行整个表格,或
<f:ajax execute="@this words" render="wordlist" />
Run Code Online (Sandbox Code Playgroud)
只执行inputText和按钮 - 如果有更多的inputcomponents你不想提交.
在后一种情况下,你需要@this因为没有它只会words执行,因此不会执行,因此不会执行commandButton.
这里有更多的阅读.