简体   繁体   English

JSF Ajax Update不更新组件

[英]JSF Ajax Update not updating the component

Following is the scenario. 以下是方案。 I have left panel which contains all the menu items. 我的左侧面板包含所有菜单项。 When I click on the menu item the corresponding pages should be loaded in the center / main panel with out page refresh. 当我单击菜单项时,应将相应的页面加载到中央/主面板中,而无需刷新页面。 So I use ajax (setupdate method) to load the page when the menu item is clicked. 因此,当单击菜单项时,我使用ajax(setupdate方法)加载页面。

My requirement is I have the same page loaded for some set of menu items. 我的要求是我要为某些菜单项集加载同一页面。 But based on the menu item selection I need to retrieve the records from the database hence the page layout remains the same. 但是基于菜单项的选择,我需要从数据库中检索记录,因此页面布局保持不变。 But I get the same page served for all the menu items. 但是我为所有菜单项提供了相同的页面。 Only for the first time, the call is sent to the backing bean and for subsequent clicks, no call is made to the backing bean and the same page is getting served. 仅第一次将调用发送到支持Bean,而对于随后的单击,将不对支持Bean进行任何调用,并且将提供相同的页面。

I am using PrimeFaces 3.4.1 and JSF 2.0 我正在使用PrimeFaces 3.4.1和JSF 2.0

Note: I am dynamically loading the screen. 注意:我正在动态加载屏幕。 In Client page, I use #{menuMB.screenName} to load the screen dynamically. 在“客户端”页面中,我使用#{menuMB.screenName}动态加载屏幕。

Following is the code snippet. 以下是代码段。

Template page 模板页面

<f:view contentType="text/html; charset=UTF-8" encoding="UTF-8" >

        <div id="outerWrapper">

            <div id="contentWrapper">
                <div id="leftPanel">
                    <div class="companylogo">

                    </div>
                    <div class="jsmenu">
                        <ui:insert name="leftPanel">
                        </ui:insert>
                    </div>
                </div>

                <div id="mainContentWrapper">
                    <div id="pageHeader">
                        <ui:insert name="pageHeader">
                        </ui:insert>
                    </div>
                    <div id="mainContent">
                        <div id="mainStyle">
                            <ui:insert name="mainContent">
                            </ui:insert>
                        </div>
                    </div>
                </div>
                <div class="clearFloat"></div>
            </div>
            <div class="clearFloat"></div>
            <div id="footer">
                <ui:insert name="footer">
                </ui:insert>
            </div>
        </div>
    </f:view>

Client XHTML 客户端XHTML

All the pages are loaded inside the mainOutputPanel which is inside the mainContentForm. 所有页面都加载在mainContentForm内部的mainOutputPanel内部。 I am getting the screen name that needs to be loaded from the backing bean menuMB. 我正在获取需要从支持bean menuMB加载的屏幕名称。

<h:body>
    <ui:composition template="/pages/protected/templates/layoutTemplate.xhtml">


        <ui:define name="pageHeader">
            <ui:include src="/pages/protected/templates/loginHeader.xhtml">
            </ui:include>
        </ui:define>
        <ui:define name="leftPanel">
            <h:form id="leftMainForm">
                <ui:include src="/pages/protected/templates/requestfactoryleft.xhtml">
                </ui:include>
            </h:form>
        </ui:define>
        <ui:define name="mainContent" >
            <h:form id="mainContentForm" enctype="multipart/formdata" prependId="true">
                <h:panelGroup id="mainOutputPanel" layout="block" >
                    <ui:include src="#{menuMB.screenName}">
                    </ui:include>
                </h:panelGroup>

            </h:form>

        </ui:define>
        <ui:define name="footer">
            <ui:include src="/pages/protected/templates/footer.xhtml">
            </ui:include>
        </ui:define>
    </ui:composition>

</h:body>

Left XHTML (Contains the menu which is loaded dynamically from the database) 左XHTML(包含从数据库动态加载的菜单)

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
            xmlns:ui="http://java.sun.com/jsf/facelets"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:p="http://primefaces.org/ui"
            xmlns:f="http://java.sun.com/jsf/core" 
            xmlns:c="http://java.sun.com  /jsp/jstl/core">

     <div class="testmenu" style="padding:7px; 
             border-radius: 10px 10px 10px 10px; margin-top: 7px; 
             background: darkgray;  width: 187px;">
        <div id="rfMainMenu">
            <p:panelMenu id="rfleftMainMenu" model="#{menuMB.mnuModel}" >
            </p:panelMenu>
        </div>

     </div>


</ui:composition>

Backing bean (to load the pages dynamically when menu item is selected) 支持bean(在选择菜单项时动态加载页面)

@SessionScoped
@ManagedBean(name = "menuMB")
public class MenuMB implements Serializable {


    @PostConstruct
    public void loadMenu() {

      if (getLoggedUser() != null) {
          fmList = loginService.getMenuForUser(getLoggedUser());
      }

    //Call createMenu method to populate the sub menu and menu items

     createMenu(fmList);
  }

    public void loadScreensForUser(ActionEvent event) {
        MenuItem menuItem = (MenuItem) event.getComponent();
        String attrName;

    try {
        if (menuItem != null) {
            selectedMenuItem = menuItem.getId();

            //Get the screen name from the properties file
            menuUrl = RequestFactoryContextUtil.getResourceBundleString(menuItem.getId());

            //Set the screen name to be displayed
            setScreenName(menuUrl);

            //Call update to update the form  
          RequestContext.getCurrentInstance().update("mainContentForm:mainOutputPanel");

        }


    } 

    catch (Exception exc) {

    }

  }


  private void createMenu(List<FunctionMaster> fmList) {
    //Submenu rfSubMenu = new Submenu();

    try {
        if (fmList != null) {
            for (FunctionMaster sub : fmList) {
                if (sub.getParentFunctionID() == 0) {
                    Submenu rfSubMenu = new Submenu();
                    rfSubMenu.setLabel(sub.getScreenDisplayName());
                    getMnuModel().addSubmenu(rfSubMenu);

                    for (FunctionMaster item : fmList) {
                        if (item.getParentFunctionID() != 0) {
                            if (item.getParentFunctionID() == sub.getFunctionID()) {

                                MenuItem rfSubItem = new MenuItem();

                                rfSubItem.setId(item.getFunctionName() + item.getFunctionID().toString());
                                rfSubItem.setValue(item.getScreenDisplayName());

                                rfSubItem.setImmediate(true);

                                rfSubItem.setUpdate(":mainContentForm:mainOutputPanel");
                                rfSubItem.setAjax(true);
                                rfSubItem.setRendered(true);
                                rfSubItem.setIcon("search");
                                //rfSubItem.setIcon("ui-icon-search");

                                 ExpressionFactory factory = FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
                                 //rfSubItem.setActionExpression(factory.createMethodExpression(FacesContext.getCurrentInstance().getELContext(), #{menuMB.loadScreenFromMenu}", Void.class, new Class[]{ActionEvent.class}));
                                 MethodExpression methodExpr = factory.createMethodExpression(FacesContext.getCurrentInstance().getELContext(), "#{menuMB.loadScreensForUser}", Void.class, new Class[]{ActionEvent.class});
                                 MethodExpressionActionListener actionListener = new MethodExpressionActionListener(methodExpr);
                                 rfSubItem.addActionListener(actionListener);

                                 rfSubMenu.getChildren().add(rfSubItem);
                                //addMenuItem(rfSubItem);
                            }
                        }
                    }
                    //mnuModel.addSeparator(new Separator());

                }

            }
        }
      } catch (Exception ex) {
        String excep = ex.getMessage();
    }

  }

}

I noticed that <h:body> resides outside the <ui:composition this will cause the <h:body> to be dropped.... (anything outside the <ui:composition is being ignored) 我注意到<h:body>驻留在<ui:composition之外,这将导致<h:body>被丢弃....( <ui:composition之外的任何内容都将被忽略)

I suggest you to place the <h:body> tags inside the template itself 我建议您将<h:body>标记放置在模板本身内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM