JSF - 通过使用Ajax调用更改panelGroup - Beans,EL和?

3

我需要对一些panelGroup UI进行切换。关于视图部分,我做了这个:

<h:panelGroup layout="block" id="profile_content">
    <h:panelGroup layout="block" styleClass="content_title">
        Profilo Utente
    </h:panelGroup>

    <h:panelGroup rendered="#{selector.profile_page=='main'}">
        <ui:include src="/profile/profile_main.xhtml" />
    </h:panelGroup>

    <h:panelGroup rendered="#{selector.profile_page=='edit'}">
        <ui:include src="/profile/profile_edit.xhtml" />
    </h:panelGroup>

    <h:panelGroup rendered="#{selector.profile_page=='insert'}">
        <ui:include src="/profile/profile_articles.xhtml" />
    </h:panelGroup>
</h:panelGroup>

现在,为了改变这个值,我编写了一个专门的JavaBean,称为Selector:
@ManagedBean(name="selector")
@RequestScoped
public class Selector {
    @ManagedProperty(value="#{param.profile_page}")
    private String profile_page;

    public String getProfile_page() {
        if(profile_page==null || profile_page.trim().isEmpty()) {
            this.profile_page="main";
        }
        return profile_page;
    }
    public void setProfile_page(String profile_page) { this.profile_page=profile_page; }
}

现在我遇到了一个问题:当我使用一些 h:commandButton 更改 "context" 时,我希望使用异步调用到服务器。这是我的按钮面板:

<h:commandButton value="EDIT">
    <f:ajax event="action" execute="???" render=":profile_content"/>
</h:commandButton>

<h:commandButton value="PM">
    <f:ajax event="action" execute="???" render=":profile_content"/>
</h:commandButton>

<h:commandButton value="ARTICLES">
    <f:ajax event="action" execute="???" render=":profile_content"/>
</h:commandButton>

我不知道应该在execute上放什么,以便我可以编辑Beans值,并使用render=":profile_content"更改上下文。

希望这个问题能够被理解。请原谅我的语言 :)

干杯

1个回答

2
使用f:setPropertyActionListener。您不需要覆盖f:ajaxexecute属性(即@this,唯一的按钮)的默认设置。

例如:

<h:commandButton value="EDIT">
    <f:setPropertyActionListener target="#{selector.profile_page}" value="edit" />
    <f:ajax event="action" render=":profile_content"/>
</h:commandButton>

请注意,Java编码规范建议使用驼峰命名法而不是下划线。我建议将profile_page改为profilePage。这是Java,不是PHP。

感谢有关CamelCase的建议。是的,这很有效。我发现奇怪的是,如果我删除<f:ajax>,它会向服务器发送普通请求(页面重新加载)。否则,如果我添加<f:ajax>,之前的ActionListener将作为异步工作。奇怪的行为,我需要学习为什么会发生这种情况 :) - markzzz
2
这就是 f:ajax 的全部意义。将其改为异步(ajax)提交。 - BalusC
你真的是JSF的专家!!就像我多次说过的那样,再次感谢你!我认为你应该得到1-10瓶啤酒 :) - markzzz
为什么在profile_content之前有一个冒号:,例如<f:ajax event="action" render=":profile_content"/>,它不应该没有冒号吗?我也想这样做,但实际上我不需要冒号,事实上冒号会抛出错误,即id::profile_content未找到。不用说,我已经赞了答案和评论。如果您可以,请回复,谢谢。 - Shahzeb
@Shahzeb:那就是 OP 使用的内容。如果组件不在同一个表单中,那么使用它是强制性的。如果你有问题或疑问,请点击“提问”按钮。 - BalusC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接