JSF Ajax 更新不更新组件



下面是场景。我有左面板,其中包含所有菜单项。当我单击菜单项时,相应的页面应该加载到中心/主面板中,而无需刷新页面。所以我使用 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中。我正在获取需要从支持豆菜单加载的屏幕名称MB。

<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(包含从数据库动态加载的菜单)

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

我注意到<h:body>驻留在<ui:composition之外,这将导致<h:body>被丢弃......(<ui:composition之外的任何内容都将被忽略)

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

相关内容

  • 没有找到相关文章

最新更新