dev@jsftemplating.java.net

Re: JSFTemplating: question about conditional hyperlinks within breadcrumbs (jsftemplating conditionals/expression language examples?)

From: Ken Paulsen <Ken.Paulsen_at_Sun.COM>
Date: Mon, 11 Sep 2006 16:23:05 -0700

Hi Priti,

I'm sure you saw my recent comments about the bug fix... here's a simple
1 page working example of an <!if ...>. If you need something more
complicated, let me know.

Thanks!

Ken

P.S. Don't forget to build the latest jsftemplating.jar file and use
that version...

Priti Tiwary wrote:
> Hi Ken,
> Any updates on this..i am still shoping that i will soon (may be today?)
> get this example to use in my conditional navigation logic case and
> hence am not working on any workaround. Please let me know, if you
> foresee longer time to provide us with the running example.
> I can start considering possible workarounds.
> Priti
>
> Ken Paulsen wrote On 09/08/06 12:11,:
>
>
>> Hi Priti,
>>
>> I have created an example and am debugging now. I do see the problem
>> and will get back to you as soon as I have an answer.
>>
>> Thanks!
>>
>> Ken
>>
>> Priti Tiwary wrote:
>>
>>
>>
>>> "
>>>
>>>
>>>
>>>> Can you send me a .jsf file that demonstrates the use of <!if>
>>>> using a sessionScope string comparison?
>>>>
>>>>
>>>>
>>> Yes, I will send you a confirmed runnable example as soon as I am back
>>> up and running."
>>>
>>> Hi Ken,
>>>
>>> Will you be able to provide us one example soon as you promised above
>>> to Mike. I am waiting for this, as i need to check a variable now to
>>> conditionally navigate to other pages.
>>>
>>> Priti
>>>
>>>
>>> Ken Paulsen wrote:
>>>
>>>
>>>
>>>> Hi Mike,
>>>>
>>>> Sorry I don't have better documentation... and I'm slow to respond
>>>> right now b/c my computer feezes up whenever I start the app server
>>>> (investigating right now) -- this makes it hard for me to give you
>>>> tested information. :(
>>>>
>>>> See comments inline.
>>>>
>>>> Mike Wright wrote:
>>>>
>>>>
>>>>
>>>>> Hi Ken,
>>>>>
>>>>> Still no conditional "joy."
>>>>>
>>>>>
>>>>> If I copy, verbatim, the if example you sent to Anissa, I get a
>>>>> trace-back. If I change the <if> to <!if> I get:
>>>>>
>>>>>
>>>>>
>>>> Yes, it should be <!if [condition]>[some components...]</if>.
>>>>
>>>>
>>>>
>>>>> This is the EE versionThis is the PE version.
>>>>>
>>>>>
>>>>>
>>>> Something doesn't seem right if you're seeing both... I wish I could
>>>> test to see (I will when my server is back up).
>>>>
>>>>
>>>>
>>>>> Now, I didn't set any version attribute (and how exactly should
>>>>> I)?
>>>>>
>>>>>
>>>>>
>>>> setAttribute() handler. Or you can put a value in the request scope
>>>> in some other way (i.e. via java add a request attribute -- see
>>>> javax.faces.context.ExternalContext; or via a managed bean defined in
>>>> the faces-config.xml file).
>>>>
>>>>
>>>>
>>>>> But, shouldn't the opposite of true be false?
>>>>>
>>>>>
>>>>>
>>>> Yes.
>>>>
>>>>
>>>>
>>>>
>>>>> Are the <!if> elements allowed to appear anywhere?
>>>>>
>>>>>
>>>>>
>>>> Anywhere a component is allowed.
>>>>
>>>>
>>>>
>>>>> If there are restrictions on where they appear, what are those
>>>>> restrictions?
>>>>>
>>>>>
>>>>>
>>>> See above... + some components don't allow some child-components.
>>>> This may further restrict where they are used. This is standard JSF
>>>> stuff. In general I believe it is bad practice to design a component
>>>> that can only accept certain types of child components... IMO, it
>>>> would be a bug for components where this is the case (Breadcrumbs is
>>>> one such component).
>>>>
>>>>
>>>>
>>>>> If there are restrictions on which variables and expressions they
>>>>> can evaluate, what are they?
>>>>>
>>>>>
>>>>>
>>>> You should be able to use any ${} or #{} expression in conjunction w/
>>>> boolean literals (true/false) + the operators I mentioned.
>>>>
>>>>
>>>>
>>>>> There's an awful lot of implied context here that isn't
>>>>> documented in these e-mails. I looked at PermissionChecker.java,
>>>>> but I'm not finding it helpful in setting up a working conditional
>>>>> example. Is there a unit test for the PermissionChecker that shows
>>>>> valid and invalid syntax?
>>>>>
>>>>>
>>>>>
>>>> There is a main() in this class which provides some testing.
>>>> However, this is only the boolean evaluation component of an <!if>,
>>>> not an end-to-end test case. There is no end-to-end test case yet.
>>>>
>>>>
>>>>
>>>>> How do I set the $attribute("version") on the including page so that
>>>>> the <!if> on the included page can check it?
>>>>>
>>>>>
>>>>>
>>>> See answer above.
>>>>
>>>>
>>>>
>>>>> How do I use the sessionScope variables instead?
>>>>>
>>>>>
>>>>>
>>>> $session{sessionKey} or #{sessionKey} or #{sessionScope.sessionKey}
>>>>
>>>>
>>>>
>>>>> Can you send me a .jsf file that demonstrates the use of <!if>
>>>>> using a sessionScope string comparison?
>>>>>
>>>>>
>>>>>
>>>> Yes, I will send you a confirmed runnable example as soon as I am
>>>> back up and running.
>>>>
>>>>
>>>>
>>>>> I prefer a runnable example to any e-mail or document, as there are
>>>>> too many typos or omissions to make the non-running examples
>>>>> reliable. This is especially important for a technology that has no
>>>>> books or white papers available, nor any online examples that one
>>>>> could google, nor much online discussion that one could google, nor
>>>>> any quantity of users whose blogs one could google. We really need
>>>>> comprehensive runnable examples (and I would argue we also need
>>>>> detailed runnable testcases).
>>>>>
>>>>>
>>>>>
>>>> I agree... volunteers? ;)
>>>>
>>>>
>>>>
>>>>> BTW, I've spent several hours on this today. I imagine that
>>>>> external users attempting to understand jsftemplating are giving up
>>>>> with much less of a time investment.
>>>>>
>>>>>
>>>>>
>>>> Yes.
>>>>
>>>> Ken
>>>>
>>>>
>>>>
>>>>> Regards,
>>>>> Mike
>>>>> ---
>>>>>
>>>>> Ken Paulsen wrote:
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>> Hi Mike,
>>>>>>
>>>>>> The attached message was recently sent out re: <if>. Try: <!if
>>>>>> true> and <!if false>. You can also use the following operators:
>>>>>>
>>>>>> = // String comarison (actually uses
>>>>>> "left.matches(right)" so regexp may work)
>>>>>> | // Logical OR
>>>>>> & // Logical AND
>>>>>> ! // NOT
>>>>>> < // Less Than
>>>>>> > // Greater than
>>>>>> & // Modulus
>>>>>>
>>>>>> And of course, you can use ()'s to override the order of operations.
>>>>>>
>>>>>> See PermissionChecker.java for more info...
>>>>>>
>>>>>> Ken
>>>>>>
>>>>>> Mike Wright wrote:
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>> Hi Ken,
>>>>>>>
>>>>>>> I'm having trouble with <!if>, are there any examples or tests
>>>>>>> that demonstrate how to use it?
>>>>>>>
>>>>>>>
>>>>>>> Whne I tried this:
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>> "<br />(before)
>>>>>>>> <!if $boolean{true} >
>>>>>>>> "<br />true!
>>>>>>>> </!if>
>>>>>>>> <!if $boolean{false} >
>>>>>>>> "<br />false ?
>>>>>>>> </!if>
>>>>>>>> <!if $boolean{true} != $boolean{false} >
>>>>>>>> "<br />true does not equal false
>>>>>>>> </!if>
>>>>>>>> <!if $boolean{true} == $boolean{false} >
>>>>>>>> "<br />true equals false?!
>>>>>>>> </!if>
>>>>>>>> "<br />(afer)
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>> I get this:
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>> (before)
>>>>>>>> true!
>>>>>>>> true does not equal false
>>>>>>>> true equals false?!
>>>>>>>> (after)
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>> So why does the false expression evaluate to true?
>>>>>>>
>>>>>>>
>>>>>>> Also, if I try this:
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>> <!if #{sessionScope["hasMetadataComponentTypeBreadcrumb"]} >
>>>>>>>> "<br />conditional breadcrumb link rendered=true
>>>>>>>> </!if>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>> I get a traceback, about an element not being closed. If I try
>>>>>>> adding quotes, I get an expression that always evaluates to true.
>>>>>>>
>>>>>>>
>>>>>>> I've tried various other things, none of which have allowed me to
>>>>>>> conditionally wrap a set of elements. I grep'ed the admin-gui
>>>>>>> code and couldn't find any examples of using <!if>.
>>>>>>>
>>>>>>>
>>>>>>> Can you give concrete examples of how I can check a session
>>>>>>> variable for a true or false value in <!if>?
>>>>>>>
>>>>>>>
>>>>>>> In the meantime, I will use the two panelGroups with rendered
>>>>>>> attribute (complimentary) boolean expressions.
>>>>>>>
>>>>>>> Regards,
>>>>>>> Mike
>>>>>>> ---
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>> Ken Paulsen wrote:
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>> Hi Mike,
>>>>>>>>
>>>>>>>> For conditionally rendering components you can do anything you
>>>>>>>> can w/ vanilla JSF (i.e. rendered="false"... although you may
>>>>>>>> need to do $boolean{false} to properly convert it).
>>>>>>>>
>>>>>>>> You can also use the <if> component to use a boolean expression
>>>>>>>> to determine if its children should be rendered. However, if
>>>>>>>> poorly written components do not render all their children, or
>>>>>>>> expect only children of a particular type... they may not
>>>>>>>> properly work with the <if> component. (BTW, the if can be used
>>>>>>>> as a regular component or via special syntax: <!if [boolean
>>>>>>>> expression]> [conditionally rendered children] </if>).
>>>>>>>>
>>>>>>>> Looking at the Breadcrumbs source code (the source I have is
>>>>>>>> old... so perhaps its been fixed), it appears that:
>>>>>>>>
>>>>>>>> 1) They only allow Hyperlink components (bad)
>>>>>>>>
>>>>>>>> 2) They do not respect "rendered=false" components (bad)
>>>>>>>>
>>>>>>>> The code iterates over all children, casts each one unnecessarily
>>>>>>>> to a Hyperlink (it does use the "text" property, but could just
>>>>>>>> as easily use the "real" "value" property and do so via the
>>>>>>>> attribute map). It then unconditionally renders each one (which
>>>>>>>> may be hidden, so it won't show up) and immediately places the
>>>>>>>> separator (also unconditionally).
>>>>>>>>
>>>>>>>> So what you are attempting to do will not work because of the way
>>>>>>>> the component was written. You should file a bug against the
>>>>>>>> component and hope that it has already been fixed or that it will
>>>>>>>> soon.
>>>>>>>>
>>>>>>>> Good luck!
>>>>>>>>
>>>>>>>> Ken
>>>>>>>>
>>>>>>>> Mike Wright wrote:
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>> I'm trying to get the displayed breadcrumbs to accurately show
>>>>>>>>> the path I followed to get to a (shared) page, but in order to
>>>>>>>>> support the maximum number of links in the breadcrumbs, I see a
>>>>>>>>> problem when the path requires fewer links. e.g.
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>> good: Application Server > Integration Applications > JBI
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>> Components > Libraries > placeholder SL > jbi.xml
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>> bad: Application Server > Integration Applications > JBI
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>> Deployments > > placeholder SA > jbi.xml
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>> s/b: Application Server > Integration Applications > JBI
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>> Deployments > placeholder SA > jbi.xml
>>>>>>>>>
>>>>>>>>> I include a .jsf page from four other .jsf pages, with
>>>>>>>>> breadcrumbs in the included file. The "including" .jsf files
>>>>>>>>> set the text/link values for the variable breadcrumbs. I've
>>>>>>>>> tried also setting a boolean in the "including" .jsf files to
>>>>>>>>> use with a rendered attribute on the not-always-needed
>>>>>>>>> breadcrumb hyperlink and I've tried setting the text/link values
>>>>>>>>> to empty string when they are not needed, but I end up with an
>>>>>>>>> extra ">" separator. (I've tried various things for the
>>>>>>>>> argument of the rendered attribute; it appears that these don't
>>>>>>>>> evaluate to false?)
>>>>>>>>>
>>>>>>>>> What's the recommended jsftemplating "best practice" for
>>>>>>>>> expressing a variable number of conditionally rendered subelements?
>>>>>>>>>
>>>>>>>>> Is there a documented expression language that I can use and any
>>>>>>>>> example?
>>>>>>>>>
>>>>>>>>> Regards,
>>>>>>>>> Mike
>>>>>>>>>
>>>>>>>>>
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>> ------------------------------------------------------------------------
>>>>>>
>>>>>>
>>>>>> Subject:
>>>>>> JSFTemplating: Re: How to use 'if'
>>>>>> From:
>>>>>> Ken Paulsen <Ken.Paulsen_at_Sun.COM>
>>>>>> Date:
>>>>>> Mon, 04 Sep 2006 15:14:04 -0700
>>>>>> To:
>>>>>> Anissa Lam <Anissa.Lam_at_Sun.COM>
>>>>>>
>>>>>> To:
>>>>>> Anissa Lam <Anissa.Lam_at_Sun.COM>
>>>>>> CC:
>>>>>> dev_at_jsftemplating.dev.java.net
>>>>>>
>>>>>>
>>>>>>
>>>>>> Hi Anissa,
>>>>>>
>>>>>> I may support this the way you listed below, however, the support
>>>>>> doesn't exist right now. (FYI, the basic support for this in
>>>>>> handlers is there as handlers can be hierarchical and conditionally
>>>>>> execute "child" handlers... but the syntax is not robust enough to
>>>>>> create this use case.)
>>>>>>
>>>>>> There is "if" support a the "(Layout)Component" level, however.
>>>>>> This allows you to make conditional areas of a page. Something like:
>>>>>>
>>>>>> <if $attribute{"version"}=EE>
>>>>>> "This is the EE Version
>>>>>> </if>
>>>>>> <if !($attribute{"version"}=EE)>
>>>>>> "This is the PE Version
>>>>>> </if>
>>>>>>
>>>>>> I'd like to expand this to support "else" and to expand this to
>>>>>> handlers... please file an enhancement request on issue tracker if
>>>>>> you can. In the mean time, I hope the above syntax will support
>>>>>> what you need.
>>>>>>
>>>>>> Thanks!
>>>>>>
>>>>>> Ken
>>>>>>
>>>>>> Anissa Lam wrote:
>>>>>>
>>>>>>
>>>>>>
>>>>>>
>>>>>>> Hi Ken,
>>>>>>>
>>>>>>> I want to know if 'if' is supported in jsftemplating. If so,
>>>>>>> can you show us the syntax on how to use it ?
>>>>>>>
>>>>>>> Specificially, i am looking for something like this:
>>>>>>>
>>>>>>> <event>
>>>>>>> <! beforeEncode
>>>>>>> *if ( $attribute{"version"} == "EE") *{
>>>>>>> println(value="this is EE version");
>>>>>>> } ???? Can i use else here ?
>>>>>>> *if ($attribute{"version"} != "EE") *{
>>>>>>> println(value=" This is NOT EE version");
>>>>>>> }
>>>>>>> />
>>>>>>> </event>
>>>>>>>
>>>>>>> thanks
>>>>>>> Anissa
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>>
>
>



<sun:page>
    <sun:html>
        <sun:head id="head" />
        <sun:body>
            <sun:form id="form">
                "<p>&nbsp;</p>
                <!if $attribute{in}>
                    "<b>You typed: #{in}</b>
                </if>
                <!if !$attribute{in}>
                    "<b>Type something and click 'Go!'</b>
                </if>

                "<br /><br />
                <sun:textField id="in" value="#{requestScope.in}" />

                "<br /><br />
                <sun:hyperlink text="Go!" />
            </sun:form>
         </sun:body>
     </sun:html>
 </sun:page>