开发者

JSF 2: Help with logic programming

i have this menu:

<ul class="lavaLamp" id="menu">
    <li><a href="./index.xhtml" >Home</a></li>
    <li class="current"><a href="#">Company</a>
    //..
</ul>

I want this menu be a component, so i don't have to copy and paste in every .xhtml page. But i need to specify the class field in the <li> tag, so the plugin 'LavaLamp' can be applied.

E.g: the current page is index.xhtml so it shou开发者_开发知识库ld generate:

<ul class="lavaLamp" id="menu">
    <li class="current"><a href="./index.xhtml" >Home</a></li>
    <li><a href="#">Company</a>
    //..
</ul>

I'm thinking how do this but nothing came out.

Any idea ?


My first refactoring would be:

<ul class="lavaLamp" id="menu">
    <li class="#{facesContext.viewRoot.viewId = 'index.html' ? 'current' : ''"><a href="./index.xhtml" >Home</a></li>
    <li><a href="#">Company</a>
    //..
</ul>

(the global el variable facesContext always points to FacesContext.getCurrentInstance())


After @meriton talk about EL, i searched about it and found something about EL ternary, so this is my solution:

<ul class="lavaLamp" id="menu">
    <li class="#{facesContext.viewRoot.viewId == '/index.xhtml' ? 'current' : '' }"><a href="index.xhtml" >Home</a></li>
    <li class="#{facesContext.viewRoot.viewId == '/pages/company/team.xhtml' ? 'current' : '' }"><a href="pages/company/team.xhtml" >Team</a></li>
    //..
</ul>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜