当前位置: 首页 > 面试题库 >

如何通过ajax使用jsf2模板进行部分页面渲染(中心内容)(来自菜单)

赵英范
2023-03-14
问题内容

我一直在努力使其工作2周,我正尝试将BalusC的信息从这2个帖子合并到(link1


link2),以通过左侧菜单实现中心内容区域的部分页面渲染。因此,左侧的链接将使用<f:ajax>或可能<a4j:ajax>通过richfaces
通过部分页面渲染来更新中心内容

-------------------------------------------------- ----
| include1链接| |
| include2链接| 中心内容,例如include1.xhtml |
| include3链接| |
-------------------------------------------------- ----

我的page-layout.xhtml(由include1.xhtml,include2.xhtml等使用的主模板)看起来像这样。

<h:body>
    <div id="top" class="top">
        <ui:insert name="top">page-layout default top content</ui:insert>
    </div>
    <div>
        <div id="left">
            <h:form id="navigationFormId">             
                    <f:ajax render=":contentForm">                                        
                        <h:commandLink value="include1" 
                                       action="#{navigationBean.setPage('include1')}" />
                        <p></p>
                        <h:commandLink value="include2" 
                                       action="#{navigationBean.setPage('include2')}" />                    
                        <p></p>
                        <h:commandLink value="include3" 
                                       action="#{navigationBean.setPage('include3')}" />   
                        <p></p>
                    </f:ajax>                                     
            </h:form>
        </div>
        <div id="content">
           <ui:insert name="content">

                <h:panelGroup id="contentPanelGroup" layout="block"> 
                    <h:form id="contentForm">

                        <h:panelGroup rendered="#{navigationBean.page == 'include1'}">            
                            <ui:include src="include1.xhtml" />         
                        </h:panelGroup> 
                        <h:panelGroup rendered="#{navigationBean.page == 'include2'}">            
                            <ui:include src="include2.xhtml" />         
                        </h:panelGroup>
                        <h:panelGroup rendered="#{navigationBean.page == 'include3'}">             
                            <ui:include src="include3.xhtml" />         
                        </h:panelGroup>

                    </h:form> 
               </h:panelGroup>

            </ui:insert>
        </div>
    </div>
</h:body>

NavigationBean.java通过带有@Named的CDI定义为ViewScoped bean

公共类NavigationBean实现Serializable {

public NavigationBean() {}

public String getPage() {return page;}
public void setPage(String p) {page = p;}
private String page = "include1";

}

包含的页面是诸如include1.xhtml和include2.xhtml之类的文件,当单击左侧菜单链接时应将其加载到中心内容中,这是一个示例include2.xhtml

<h:body>

    <ui:composition template="page-template.xhtml">
        <ui:define name="content">

             include2

             <h:form> 
                 form components for include2
             </h:form>

        </ui:define>
    </ui:composition>

</h:body>

我正在获取FileNotFoundException,尽管堆栈跟踪无法确定是哪一个,但是删除了

<h:panelGroup rendered=".....>
    <ui:include src="..." />
</h:panelGroup>

标签可删除异常。我认为这是基于BalusC帖子中的2条来完成此操作的正确实现,但无法使其正常工作。


问题答案:

我能够通过JSF2 /
Ajax和richfaces4获得设计(我认为其他实现也是可以的),其中通过模板实现的左侧导航菜单项能够通过ajax渲染模板的中心内容,从而阻止整个页面刷新,并具有闪烁效果。

我的navigationmenu.xhtml代码看起来像这样

<ui:composition>
<h:form id="navigationFormId">
        <rich:collapsiblePanel id="carrierCollapsibleId" header="Links that update center content" switchType="client">
            <h:commandLink  id="linkId1" action="/page1" value="Update Center Content with page1">
                <a4j:ajax execute="@form" render=":centerContentDiv,:centerForm" />                    
            </h:commandLink>
            <br />
            <h:commandLink  id="linkId2" action="/page2" value="Update Center Content with page2">
                <a4j:ajax execute="@form" render=":centerContentDiv,:centerForm" />                    
            </h:commandLink>
</rich:collapsiblePanel>
...
</h:form>

我相信标记的render=":centerContentDiv,:centerForm"属性a4j:ajax可以更改为仅render=":centerForm"但尚未测试。我知道两者可以一起工作。另外,我尝试在ID之间使用空白的JSF2
ajax标记并得到了错误,所以我去了richfaces 4.x的a4j,如果不使用空格,可以使用逗号。

现在,为了使其正常工作,每个pageX.xhtml文件(例如page1.xhtml)都需要具有<h:form id="centerForm">,这是page1.xhtml的示例

<ui:composition template="/templates/uiTemplate.xhtml">
        <ui:define name="center_content">                                
            <h:form id="centerForm">
               <!-- put your components in here -->

            </h:form>
        </ui:define>
    </ui:composition>

一个限制是使用模板的每个视图必须定义一个<h:form id=centerForm" ...>否则,JSF2会抱怨它找不到渲染目标。

我的uiTemplate.xhtml文件具有用于页眉,页脚的代码,但是定义了导航和中心内容的相关部分如下

<div class="header_content">
        <div id="left">
            <ui:include src="navigationMenu.xhtml" />
        </div>
        <div id="center_content">
                                 <!-- think this panelGroup wrapper can be -->
                                 <!-- removed. need to test that though -->
            <h:panelGroup id="centerContentDiv" layout="block">
                <ui:insert name="center_content" />
            </h:panelGroup>
        </div>
    </div>


 类似资料:
  • 问题内容: 我有一个带有选项卡的页面,我想在单击选项卡时仅通过ajax渲染不同的部分。我有一些代码,但我认为这不是最有效的方法。我希望有人能帮助我解决已经存在的代码,或者如果有一种更简单的方法,我将不胜感激。谢谢! HTML标签 学校负责人 最新JS 路线 问题答案: 请参阅我对上一篇文章的回答。这将为您提供一个在Rails中的AJAX想法:完成模型#更新后显示模型#新表单 您可以做的是在操作中呈

  • 问题内容: 对于那里的MVC专家来说,这应该相对简单,但我仍在学习中。 简单来说,我有一个视图不是强类型的。 在此视图上,我有一个文本框,使用jQuery的AutoComplete对其进行了扩展 当用户在文本框中键入内容时,自动完成功能将对Controller进行AJAX调用,该Controller会调用存储过程,并返回具有2个属性的JSON记录集合: ID (商品的标识符) 名称 (商品名称)

  • 问题内容: 我检查了这个问题,它解决了我的最初问题。但是我不希望仅当用户单击链接时才显示部分视图,我希望在页面加载时显示部分视图,并且可能在加载部分视图时显示进度指示器。 如何实现? 非常感谢您阅读本文。 问题答案: 如果要加载页面,然后通过ajax加载部分视图,则可以创建一个执行以下操作的对象: 然后在您的页面中执行以下操作:

  • 问题内容: 用AJAX响应提交的表单动态呈现页面似乎很常见。其他类似问题都没有集中于如何以一般方式进行此操作。 我可以找到的关于该主题的最佳博客文章位于:http : //www.gotealeaf.com/blog/the- detailed-guide-on-how-ajax-works-with-ruby-on- rails 问题:如何编写Rails应用程序的代码,以便在提交表单或单击链接时

  • 英文原文: http://emberjs.com/guides/templates/rendering-with-helpers/ Ember.js提供了数个助手来协助你以不同的方式来渲染其他视图或模板 {{partial}} 助手 {{partial}}接收一个模板作为其参数,然后恰当地渲染这个模板 {{partial}}不改变上下文或作用域。它只是简单地在当前作用域下将指定的模板渲染出来。 1

  • 问题内容: 我在MVC应用程序中有此标记。 当它运行时,IngredientsListControl.ascx在浏览器中显示为新页面,并且不会更新Ingredientlistdiv。 这是我的控制器动作 我在这条线上做对了吗? 这就是我将控件呈现到div中的方式,以便它不会加载新页面。 马尔科姆 问题答案: 使用此功能时: …您应该注意,这与 它不会引发onsubmit事件,并且不会调用MVC的A