JSF Ajax Update不更新组件

时间:2013-01-27 17:33:24

标签: ajax jsf

以下是该方案。我有左侧面板,其中包含所有菜单项。当我点击菜单项时,相应的页面应该加载到中心/主面板中,不刷新页面。因此,当单击菜单项时,我使用ajax(setupdate方法)加载页面。

我的要求是我为某些菜单项加载了相同的页面。但根据菜单项选择,我需要从数据库中检索记录,因此页面布局保持不变。但我得到了所有菜单项的相同页面。只有第一次,调用才会被发送到辅助bean,并且对于后续的点击,不会对辅助bean进行调用,并且正在为相同的页面提供服务。

我正在使用PrimeFaces 3.4.1和JSF 2.0

注意:我正在动态加载屏幕。在客户端页面中,我使用#{menuMB.screenName}动态加载屏幕。

以下是代码段。

模板页面

<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>

客户端XHTML

所有页面都加载在mainContentForm内的mainOutputPanel内。我正在获取需要从支持bean菜单中加载的屏幕名称。

<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>

左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>

支持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();
    }

  }

}

1 个答案:

答案 0 :(得分:0)

我注意到<h:body>位于<ui:composition之外,这会导致<h:body>被删除....(<ui:composition之外的任何内容都会被忽略)

我建议您将<h:body>标记放在模板本身