Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
962 views
in Technique[技术] by (71.8m points)

jsf 2 - Test if ui:insert has been defined in the template client

I am wondering if it is possible to know if ui:insert was defined in the ui:composition. I know that I can do it using separate ui:param, but just wanted to do it without in order to keep it simple and less error prone.

Example :

Template

...
<ui:insert name="sidebar" />

<!-- Conditionnaly set the class according if sidebar is present or not -->
<div class="#{sidebar is defined ? 'with-sidebar' : 'without-sidebar'}">
    <ui:insert name="page-content" />
</div>
...

Page 1

...
<ui:define name="sidebar">
    sidebar content
</ui:define>

<ui:define name="page-content">
    page content
</ui:define>
...

Page 2

...
<ui:define name="page-content">
    page content
</ui:define>
...
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

ui:param is for me the best way to go. It's just a matter of using it the right way. As a simple example, I define a param here to specify wether there's a sidebar or not. Keep in mind you can define a default insertion definition in the template, so just declare it inside:

template.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">

    <ui:insert name="sidebar">
        <!-- By default, there's no sidebar, so the param will be present.
            When you replace this section for a sidebar in the client template,
            the param will  be removed from the view -->
        <ui:param name="noSideBar" value="true" />
    </ui:insert>

    <div class="#{noSideBar ? 'style1' : 'style2'}">
        <ui:insert name="content" />
    </div>

</ui:composition>

Then couple of views here, one using the sidebar and the other with no sidebar. You can test it and see how the style changes in the browser. You'll notice there's no value for #{noSideBar} in the second one, which will evaluate to false in any EL conditional statement.

page1.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets" template="/template.xhtml">
    <ui:define name="content">
        No sidebar defined? #{noSideBar}
    </ui:define>
</ui:composition>

page2.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets" template="/template.xhtml">
    <ui:define name="sidebar" />
    <ui:define name="content">
        No sidebar defined? #{noSideBar}
    </ui:define>
</ui:composition>

This way you only need to worry about including the sidebar or not in the client view.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...