【问题标题】:JSF - Change panelGroup by using ajax calls - Beans, EL and?JSF - 使用 ajax 调用更改 panelGroup - Beans、EL 和?
【发布时间】:2011-05-15 21:54:12
【问题描述】:

我必须对一些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 更改“上下文”时,我想对服务器使用异步调用。这是我的按钮面板:

<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" 更改上下文

希望这个问题可以理解。请原谅我的语言:)

干杯

【问题讨论】:

    标签: java jsf jsf-2 facelets javabeans


    【解决方案1】:

    使用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 Coding Conventions 建议使用 CamelCase 而不是 under_score。我建议将profile_page 修复为profilePage。这是 Java,不是 PHP。

    【讨论】:

    • 感谢 CamelCase 的建议。是的,这行得通。我觉得奇怪的是,如果我删除 ,它会向服务器发出正常请求(重新加载页面)。否则,如果我添加 ,之前的 ActionListener 将作为异步工作。奇怪的行为,我需要了解为什么会发生这种情况:)
    • 这就是f:ajax 的全部意义所在。将其更改为异步(ajaxical)提交。
    • 你是真正的JSF专家!!正如多次所说,再次感谢男人!我认为你应该喝 1-10 杯啤酒 :)
    • 为什么你有一个冒号:在 profile_content 之前,即 不应该没有冒号我也在这个之后,但我做到了不需要冒号事实上冒号会抛出一个错误,即 id::profile_content not found。不用说已经赞成答案和评论。如果你可以欢呼,请回复。
    • @Shahzeb:这正是 OP 使用的。当组件不在同一个表单内时,它是强制性的。如果您有问题/疑问,只需按 按钮。
    猜你喜欢
    • 1970-01-01
    • 2013-07-03
    • 2014-05-03
    • 2011-05-08
    • 1970-01-01
    • 2012-01-06
    • 2011-05-31
    • 1970-01-01
    相关资源
    最近更新 更多