使用请求范围的Bean函数作为JSF中的临时呈现按钮的操作 [英] Using request-scoped bean function as action for a temporary rendered button in JSF
问题描述
我在JSF中有以下基本(也许是愚蠢的)理解问题:
I have the following basic (and maybe stupid) understanding problem in JSF:
有一个JSF页面"testPage.xhtml":
There is one JSF page "testPage.xhtml" :
<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<f:view>
<h:body>
<h:form>
<h:commandLink id="B1" value="B1" action="#{testBean.ctrl}"/>
<h:commandLink id="B2" value="B2" action="#{testBean.ctrl}"
rendered="#{testBean.renderB2}"/>
</h:form>
</h:body>
</f:view>
</html>
还有一个支持bean"TestBean.java":
And one backing bean "TestBean.java" :
package test;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean(name="testBean")
@RequestScoped
public class TestBean implements Serializable {
public static final long serialVersionUID = 1L;
private boolean renderB2 = false;
public String ctrl() {
setRenderB2(true);
System.out.println("ctrl() is called.");
return null;
}
public boolean getRenderB2() {
return renderB2;
}
public void setRenderB2(boolean renderB2) {
this.renderB2 = renderB2;
}
}
因此,两个链接都将TestBean.ctrl()
作为操作.
So both links have TestBean.ctrl()
as action.
仅首先渲染B1
.单击B1
会导致执行TestBean.ctrl()
,并且也会渲染B2
.
First only B1
is rendered. Clicking B1
causes execution of TestBean.ctrl()
and B2
is rendered too.
但是,单击B2
,然后不执行TestBean.ctrl()
.
However, clicking B2
then does not execute TestBean.ctrl()
.
这是我的问题:为什么单击B2
时不执行该操作方法?
And this is my question: Why is the action method not executed when clicking B2
?
可能是因为没有再次渲染B2
,但是为什么这会阻止action方法的执行(由先前渲染的B2
链接调用)?
Probably it is because B2
is not rendered again.But why does that prevent the execution of the action method (called by the previously rendered B2
link)?
推荐答案
这是因为rendered
属性在表单提交的应用请求值"阶段被重新评估.如果此时未呈现UIInput
或UICommand
组件,则JSF不会为该组件应用请求值. IE. UIInput
组件的模型值不会被更新,并且UICommand
组件的动作也不会被调用.
That's because the rendered
attribute is re-evaluated again during apply request values phase of the form submit. If an UIInput
or UICommand
component is not rendered at that point, then JSF won't apply the request values for the component. I.e. the UIInput
component's model value will not be updated and the UICommand
component's action will not be invoked.
由于您的bean处于请求范围内,因此在显示该表单的响应结束时已将其废弃,并且在表单提交请求开始时创建了一个全新的bean.
Because your bean is request scoped, it's been trashed by end of the response which displays the form and a brand new one is been created by start of the request of the form submit.
您必须保留负责rendered
属性的所有请求范围的属性.最简单的方法是通过标记@ViewScoped
将其放置在视图范围内,并确保从应回传到同一视图的操作方法中返回null
或void
.
You have to preserve any request scoped properties responsible for the rendered
attribute. The easiest way is to place the bean in the view scope by marking it @ViewScoped
and ensuring that you return null
or void
from action methods which should postback to the same view.
这篇关于使用请求范围的Bean函数作为JSF中的临时呈现按钮的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!