2012-10-11 65 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?

我用這個想法觸發額外調試的相關信息等,爲文件的固定列表。