无法将隐藏的 commandButton 与 @RequestScoped backing-bean 一起使用
我有以下示例代码。 最初,只有命令按钮 Two 可见。当我单击此按钮时,命令按钮 One 也可见。但是,当我单击 One 时,backing-bean 方法 click1 不会被触发。
以下是我的代码:
xhtml
<h:form id="form1">
<h:inputHidden id="show" value="#{bean.show1}" />
<h:commandButton id="button1" value="One" action="#{bean.click1}"
rendered="#{bean.show1}" />
</h:form>
<h:form id="form2">
<h:inputHidden id="show" value="#{bean.show1}" />
<h:commandButton id="button2" value="Two" action="#{bean.click2}" />
</h:form>
backing-bean
@RequestScoped
@Named("bean")
public class JsfTrial implements Serializable {
private static final long serialVersionUID = 2784462583813130092L;
private boolean show1; // + getter, setter
public String click1() {
System.out.println("Click1()");
return null;
}
public String click2() {
System.out.println("Click2()");
setShow1(true);
return null;
}
}
我发现 BalusC 提供了非常丰富的答案。
如果我理解正确的话,我的问题是由于这个答案的第5点造成的。
这是否也意味着我们不能将隐藏的 commandButton 与 @RequestScoped backing-bean 一起使用?
I have following sample code.
Initially, only commandButton Two is visible. When I click this button, commandButton One is also visible. But when I click One, the backing-bean method click1 does not get fired.
Following is my code:
xhtml
<h:form id="form1">
<h:inputHidden id="show" value="#{bean.show1}" />
<h:commandButton id="button1" value="One" action="#{bean.click1}"
rendered="#{bean.show1}" />
</h:form>
<h:form id="form2">
<h:inputHidden id="show" value="#{bean.show1}" />
<h:commandButton id="button2" value="Two" action="#{bean.click2}" />
</h:form>
backing-bean
@RequestScoped
@Named("bean")
public class JsfTrial implements Serializable {
private static final long serialVersionUID = 2784462583813130092L;
private boolean show1; // + getter, setter
public String click1() {
System.out.println("Click1()");
return null;
}
public String click2() {
System.out.println("Click2()");
setShow1(true);
return null;
}
}
I found a very informative answer by BalusC.
If I understand it correctly, my problem is due to point 5 of this answer.
Does that also mean we can not use hidden commandButton with @RequestScoped backing-bean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用请求范围,您应该仅通过
将条件作为请求参数传递给后续请求,而不是通过 JSF 隐藏输入字段
。隐藏输入字段的值仅在更新模型值阶段在模型中设置,而渲染属性的条件已在较早的应用请求值阶段评估。因此,使用
而不是
:这样您就可以将它们提取为 bean 的(post)构造函数中的请求参数。
丑陋,但 CDI 不提供替代 JSF 的
@ManagedProperty("#{param.show1}")
的内置注释。但是,您可以homegrow 这样的注释。You can use the request scope, you should only pass the condition as a request parameter to the subsequent requests by
<f:param>
instead of by a JSF hidden input field<h:inputHidden>
. The value of the hidden input field is only set in the model during Update Model Values phase, while the condition of therendered
attribute is already evaluated during Apply Request Values phase which is earlier.So, use
<f:param>
instead of<h:inputHidden>
:This way you can extract them as request parameter in bean's (post)constructor.
Ugly, but CDI doesn't offer a builtin annotation which substituties JSF's
@ManagedProperty("#{param.show1}")
. You could however homegrow such an annotation.