无法将隐藏的 commandButton 与 @RequestScoped backing-bean 一起使用

发布于 2025-01-04 20:31:44 字数 1450 浏览 1 评论 0原文

我有以下示例代码。 最初,只有命令按钮 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮坠入山谷 2025-01-11 20:31:44

您可以使用请求范围,您应该仅通过 将条件作为请求参数传递给后续请求,而不是通过 JSF 隐藏输入字段 。隐藏输入字段的值仅在更新模型值阶段在模型中设置,而渲染属性的条件已在较早的应用请求值阶段评估。

因此,使用 而不是

<h:form id="form1">
    <h:commandButton id="button1" value="One" action="#{bean.click1}"
        rendered="#{bean.show1}">
        <f:param name="show1" value="#{bean.show1}" />
    </h:commandButton>
</h:form>
<h:form id="form2">
    <h:commandButton id="button2" value="Two" action="#{bean.click2}">
        <f:param name="show1" value="#{bean.show1}" />
    </h:commandButton>
</h:form>

这样您就可以将它们提取为 bean 的(post)构造函数中的请求参数。

public JsfTrial() {
    String show1 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("show1");
    this.show1 = (show1 != null) && Boolean.valueOf(show1);
}

丑陋,但 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 the rendered attribute is already evaluated during Apply Request Values phase which is earlier.

So, use <f:param> instead of <h:inputHidden>:

<h:form id="form1">
    <h:commandButton id="button1" value="One" action="#{bean.click1}"
        rendered="#{bean.show1}">
        <f:param name="show1" value="#{bean.show1}" />
    </h:commandButton>
</h:form>
<h:form id="form2">
    <h:commandButton id="button2" value="Two" action="#{bean.click2}">
        <f:param name="show1" value="#{bean.show1}" />
    </h:commandButton>
</h:form>

This way you can extract them as request parameter in bean's (post)constructor.

public JsfTrial() {
    String show1 = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("show1");
    this.show1 = (show1 != null) && Boolean.valueOf(show1);
}

Ugly, but CDI doesn't offer a builtin annotation which substituties JSF's @ManagedProperty("#{param.show1}"). You could however homegrow such an annotation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文