【问题标题】:How I can dynamically load a template through a menu on a single method for all the menu. JSF 2, Primefaces我如何通过一个菜单对所有菜单的单个方法动态加载模板。 JSF 2,Primefaces
【发布时间】:2013-03-18 14:39:36
【问题描述】:

我如何通过一个菜单对所有菜单的单一方法动态加载模板。我如何识别调用该方法的菜单项。 这是我的 xhtml:

 <h:body>

        <p:layout fullPage="true">

            <p:layoutUnit position="north" size="80" resizable="true" closable="true" collapsible="true">
                <h:form id="form3">  
                    <ui:include src="./templates/template.xhtml" />  
                </h:form> 
            </p:layoutUnit>

            <p:layoutUnit position="south" size="100" closable="true" collapsible="true">
                Footer
            </p:layoutUnit>

            <p:layoutUnit position="west" header="Opciones" size="175">
                <h:form id="form1">
                    <p:panelMenu style="width:200px">   
                        <p:submenu label="Clientes" >
                            <p:menuitem value="Adicionar" update=":form2" actionListener="#{templateMB.templateAddCliente}" />
                            <p:menuitem value="Modificar" update=":form2" actionListener="#{templateMB.templateConfigCliente}" />
                            <p:menuitem value="Listado" update=":form2" actionListener="#{templateMB.templateListadoCliente}" />
                        </p:submenu> 
                        <p:submenu label="Contratos">  
                            <p:menuitem value="Adicionar" update=":form2" actionListener="#{templateMB.templateAddContrato}" />
                        </p:submenu>  
                        <p:separator/>  
                        <p:submenu label="Suplementos" >  
                            <p:menuitem value="Adicionar" icon="ui-icon-signal"/>  
                        </p:submenu>  
                    </p:panelMenu>
                </h:form>
            </p:layoutUnit>

            <p:layoutUnit position="center" id="centerlayout">
                <h:form id="form2">  
                    <ui:include src="#{templateMB.centerTemplate}" />  
                </h:form> 
                <p:dialog id="dialog" header="Subir Documento del Contrato" resizable="false" widgetVar="fileDialog" showEffect="explode" hideEffect="explode" width="500" height="200"> 

                    <h:form enctype="multipart/form-data">  
                        <p:panel id="panelDialog">
                            <p:fileUpload value="#{contratosMB.fileContrato}" mode="simple" style="width: 400px"/>  
                            <br/>
                            <br/>
                            <p:commandButton value="Guardar" ajax="false"  
                                             actionListener="#{contratosMB.upload}"/>  
                        </p:panel>
                    </h:form>  

                </p:dialog> 
            </p:layoutUnit>

        </p:layout>

    </h:body>

这是我的支持 bean:

      public String templateAddCliente() {
    try {
        this.centerTemplate = "./templates/addCliente.xhtml";
         return null;

    } catch (Exception e) {
        JsfUtil.addErrorMessage("Ha ocurrido un error: " + e.getMessage());
        return null;
    }
}

  public String templateConfigCliente() {
    try {
        this.centerTemplate = "./templates/configCliente.xhtml";
         return null;

    } catch (Exception e) {
        JsfUtil.addErrorMessage("Ha ocurrido un error: " + e.getMessage());
        return null;
    }
}
  public String templateHome() {
    try {
        this.centerTemplate = "./templates/hometemplate.xhtml";
         return null;

    } catch (Exception e) {
        JsfUtil.addErrorMessage("Ha ocurrido un error: " + e.getMessage());
        return null;
    }
}

  public String templateListadoCliente() {
    try {
        this.centerTemplate = "./templates/listadoCliente.xhtml";
         return null;

    } catch (Exception e) {
        JsfUtil.addErrorMessage("Ha ocurrido un error: " + e.getMessage());
        return null;
    }
}

  public String templateAddContrato() {
    try {
        this.centerTemplate = "./templates/addContratos.xhtml";
         return null;

    } catch (Exception e) {
        JsfUtil.addErrorMessage("Ha ocurrido un error: " + e.getMessage());
        return null;
    }
}

到目前为止一切正常,但我必须为每个菜单项创建一个方法,我不喜欢。我想在backinbean中使用单一的方法来加载页面,但找不到方法。

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    请记住,使用 JSF 2,您可以在操作方法中处理参数(与 JSF 本身无关,但与 EL 实现有关)。

    您可以使用枚举、数字类型或String 来定义要显示的实际模板。最简单的例子是一个方法(我们称之为displayTemplate),它接收一个String,定义了你想要显示的模板。

    public displayTemplate(String templateId) {
        centerTemplate = templateMap.get(templateId);
    }
    

    作为一个想法,您可以在包含路径作为值和 templateId 作为键的Map 中注册您的模板,以便在模板之间轻松切换。请记住初始化此地图。

    Map<String,String> templateMap = new HashMap<String,String>();
    //...
    templateMap.put("addCliente","./templates/addCliente.xhtml");
    //The very same with the other templates
    

    最后,在您的页面中,您可以像这样调用方法:

    <p:menuitem value="Adicionar" update=":form2" actionListener="#{templateMB.displayTemplate('addCliente')}" />
    

    【讨论】:

    • 嗨,谢谢,但是如果菜单是动态生成的,我该怎么办?
    • 如果模板是由菜单动态生成的,那么您将别无选择,只能切换不同的可能性并返回值(通过使用条件语句或通过 int/char/如果您使用的是 Java 7,则使用枚举 switch 语句或字符串)。如果您有机会对模板进行预处理,我建议您进行,否则按需进行。
    猜你喜欢
    • 2014-09-09
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多