2012-10-11 66 views
0

我有了与不包括在下面的代码中其他的东西沿着菜单中的facelet模板的facelet模板拆卸零件:从某些网页

<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:f="http://java.sun.com/jsf/core" 
    xmlns:c="http://java.sun.com/jsp/jstl/core" 
    template="/layout/template.xhtml"> 

</ui:composition> 

很少有页面需要使用模板中除菜单外的所有内容。有没有办法从这些页面指定不显示菜单。

我正在寻找facelets的方法,就像传递facelet参数或其他东西。我想到了以下的选择,但我想,以避免他们:

  1. 创建另一个模板完全一样,现有一个,但没有菜单,并在这些页面中使用它
  2. 就拿菜单出来的模板和在需要的页面上使用,但这意味着将菜单添加到大约25页,我想将菜单保留在模板中。
+0

为什么这个问题被标记为JSF 1.2和JSF 2.0?这只是令人困惑。如果您使用的是JSF 2.0,请勿标记JSF 1.2。如果您使用的是JSF 1.2,请勿标记JSF 2.0。如果您尝试过* *和*两个*版本都出现同样的问题,请删除这两个标签并粘贴到[[jsf]'标签。 – BalusC

+0

我删除了JSF 1.2和JSF 2.0标签 – Nick

回答

0

一个为我工作的这个解决方案是deliveres一个布尔值的文件列表,并在模板中使用的是该用于切换豆包括:

<c:choose> 
    <c:when test="#{toogleMenu.withMenu}" > 
     <ui:include src="/menu.xhtml" /> 
    </c:when> 
    <c:otherwise> 
     ... 
    </c:otherwise> 
</c:choose> 

这个bean是一样的东西这个,也许你可以使用.properties或DB而不是带有菜单的文件。

@Named(value = "toogleMenu") 
@ViewScoped 
public class ToogleMenuBean implements Serializable { 
    ... 
    private static final String[] EXCLUDE_FILES = { "nomenu.xhtml", ... }; 

    public String getCurrentURL() { 
    FacesContext context = FacesContext.getCurrentInstance(); 
    HttpServletRequest req = (HttpServletRequest) context.getExternalContext().getRequest(); 
    return req.getRequestURL().toString(); 
    }    

    public String getCurrentFile() { 
    String url = getCurrentURL(); 
    return url.substring(url.lastIndexOf("/")+1); 
    } 

    public List<String>getExcludes() { 
    List<String> excludes = new LinkedList<>(); 
    excludes.addAll(Arrays.asList(ToogleMenuBean.EXCLUDE_FILES)); 
    return excludes; 
    } 

    public boolean isWithMenu() { 
    boolean ret = ! getExcludes().contains(getCurrentFile()); 
    return ret; 
    } 
} 

感谢您的getCurrentUrl这里的想法:
How do I get request url in jsf managed bean without the requested servlet?

我用这个想法触发额外调试的相关信息等,为文件的固定列表。