如何使 commandButton 不完全刷新页面?如何使用 f:ajax? [英] How make commandButton not fully refresh page? How to use f:ajax?
问题描述
我有一个按钮可以提交表单并调用托管 bean 操作.
I have a button to submit a form and invoke a managed bean action.
<h:form>
...
<h:commandButton value="Submit" action="#{bean.submit}" />
</h:form>
但是当我按下按钮时,它会刷新整个页面,有时还会更改 URL.
But when I press the button it refreshes the whole page and sometimes also changes the URL.
有什么办法可以不刷新页面而仍然调用操作吗?
Is there some way to not refresh the page and still invoke the action?
推荐答案
利用 Ajax.这是嵌套的问题 <f:ajax>
在感兴趣的命令按钮内.
Make use of Ajax. It's a matter of nesting <f:ajax>
inside the command button of interest.
<h:form>
...
<h:commandButton ...>
<f:ajax execute="@form" render="@none" />
</h:commandButton>
</h:form>
特别是 render="@none"
(无论如何,这是默认值,您可以完全省略该属性)将指示 JSF 在提交后重新渲染任何内容.如果您打算仅重新渲染特定组件而不是整个页面,那么您还可以在 render
属性中指定该特定组件的 ID.
Particularly the render="@none"
(which is the default value anyway, you could just omit the attribute altogether) will instruct JSF to re-render just nothing after the submit. If you intend to re-render only a specific component instead of the whole page, then you could also specify the ID of that specific component in render
attribute.
<h:form>
...
<h:commandButton ...>
<f:ajax execute="@form" render="result" />
</h:commandButton>
...
<h:panelGroup id="result">...</h:panelGroup>
</h:form>
如果您已经在使用 PrimeFaces,那么只需使用
而不是
.它默认使用 ajax,所以你不需要嵌套在
中.您只需要记住使用属性名称process
和update
而不是execute
和render
.
If you're already using PrimeFaces, then it's a matter of simply using <p:commandButton>
instead of <h:commandButton>
. It uses by default ajax already, so you don't need to nest in a <f:ajax>
. You only need to remember to use attribute names process
and update
instead of execute
and render
.
<h:form>
...
<p:commandButton ... update="result" />
...
<h:panelGroup id="result">...</h:panelGroup>
</h:form>
execute
属性已经默认为 @form
,所以可以省略.
The execute
attribute defaults to @form
already, so it could be omitted.
- 了解 PrimeFaces 流程/更新和 JSF f:ajax 执行/渲染属性
- 如何找出ajax更新/渲染组件的客户端ID?找不到带有表达式foo"的组件引用自bar"
- 为什么我需要用 render="#{some}" 嵌套一个组件?在另一个组件中,当我想对其进行 ajax 更新时?
这篇关于如何使 commandButton 不完全刷新页面?如何使用 f:ajax?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!