Reuse variable outside of composition - passing variables up in jsf? - jsf

I'm using jsf 1.2 and have got some weird javascript which gets repeated at several places in my project.
I do not want to repeat myself.. but i found no way to do something similar to this:
<ui:composition>
<c:set var="confirmBlockJS" value="if (confirm('#{tk:encodeJS(confirmMessage)}')) {#{confirmed}} else{ #{notConfirmed}}"/>
</ui:composition>
Now i want to use the set variable but obv this will fail.
<ui:include src="/blocks/tech/confirmBlock.xhtml">
<ui:param name="confirmMessage" value="#{appTexts['action.logout.title']}"/>
<ui:param name="confirmed" value="return false;"/>
<ui:param name="notConfirmed" value=" #{doJS}"/>
</ui:include>
<h:outputLink id="logout" value="#" rendered="#{renderLogout}"
onclick='#{confirmBlockJS};'
styleClass="_allowEnterKey">
<h:outputText value="#{appTexts['action.logout']}"></h:outputText>
</h:outputLink>
confirmBlockJS is obviously empty at that point, which is actually in my eyes a good thing. But is there no way to pass the variable up? I tried ui:param without luck.
SOLUTION
Use ui:decorate when you want to access variable set with c:set outside of the page which uses the ui:decorate. It will still be visible. In the case of ui:include the c:set will not be visible outside, but ui:param should pass it up (e.g. use ui:param instead of c:set in the ui:include composition)

If you didn't want to inline it as an attribute value, then <ui:decorate> would be the solution for you.
An EL function is closest what you can get to work for this particular functional requirement:
<h:outputLink id="logout" value="#" rendered="#{renderLogout}"
onclick="#{js:confirmBlock(appTexts['action.logout.title'], 'return false;', doJS)}"
styleClass="_allowEnterKey">
<h:outputText value="#{appTexts['action.logout']}"></h:outputText>
</h:outputLink>
or:
<ui:param name="confirmBlock" value="#{js:confirmBlock(appTexts['action.logout.title'], 'return false;', doJS)}" />
<h:outputLink id="logout" value="#" rendered="#{renderLogout}"
onclick="#{confirmBlock}"
styleClass="_allowEnterKey">
<h:outputText value="#{appTexts['action.logout']}"></h:outputText>
</h:outputLink>
Note that generating JS code by Java/JSF/EL this way is a bit a smell. Is it really not possible to make it a simple JS function which you put in a .js file and parameterize it with arguments?

Related

Recursion with include and h:datatable [duplicate]

This question already has an answer here:
JSTL c:if doesn't work inside a JSF h:dataTable
(1 answer)
Closed 7 years ago.
I'm having some troubles trying to recursively include pages. The problem is that all the include actually take place but there is something going on with the h:dataTable that makes it so the second included datatable content isn't displayed.. I'm guessing the problem comes from the var post being concurrent with itself. It works without the datatable.
So from a page.xhtml I'm calling an include for post_view2.xhtml:
<ui:include src="/template/includes/post_view2.xhtml" >
<ui:param name="postList" value="#{watchThread.listeFirstPosts}"></ui:param>
<ui:param name="count" value="8"></ui:param>
</ui:include>
Count is set to 8.
This is post_view2.xhtml:
<ui:composition xmlns=....>
<h:dataTable var="post" value="#{postList}" styleClass="fullWidth">
<h:column>
#{post.posts.size()} <!-- gives a size higher than 0 -->
<h:outputText value="#{watchThread.sanitize(post.content)}"/>
<c:if test="${count gt 0}">
<ui:include src="/template/includes/post_view2.xhtml">
<ui:param name="postList" value="#{post.replies}"></ui:param>
<ui:param name="count" value="${count-1}"></ui:param>
</ui:include>
</c:if>
</h:column>
</h:dataTable>
</ui:composition>
Notice that at the first include the post var is equal to a list. On the second include I'm trying to make it equal to another list and I think that is what is causing the problem. I don't know why nor how to solve it.
On a side note I'm using jstl c:if but if there is another solution that is pure jsf, primefaces included I'd take it.
I found a bit of a workaround but I'll let the question as unanswered.
Instead of using DataTable I'm using jsp tag forEach and it works.
so instead of this:
<h:dataTable var="post" value="#{postList}">
I have :
<c:forEach var="post" items="#{postList}">
I don't know why this works and dataTable doesn't. If anyone knows please expand.

jsf set ui:param or use a value that changes dynamically in ui:repeat

I want to update an ui:param value or use something similar in ui:repeat loops with a condition. The idea is something as follows (is just an aprox, not the final implementation but I guess is understandable):
<ui:param name="marginLeftNode" value="#{myBean.initialMarginValue}" />
<ui:repeat value="#{myBean.viewWrapper.linkMap.entrySet().toArray()}" var="map">
<ui:repeat var="link" value="#{map.value}" varStatus="status">
<li style="margin-left: #{marginLeftNode}%" class="ico-#{link.getStyleCss()}-ayuda">
#{link.getTitle()}
</li>
<!-- Code conditions that doesn't works in any way as I've readed in the links after code -->
<c:if test="#{marginLeftNode gt 4}">
<ui:param name="marginLeftNode" value="#{myBean.viewWrapper.nodeList.get(status.index).depth}" />
</c:if>
<ui:fragment rendered="#{marginLeftNode gt 4}">
<ui:param name="marginLeftNode" value="#{myBean.viewWrapper.nodeList.get(status.index).depth}" />
</ui:fragment>
<!-- End code conditions: these are the two solutions c:if and ui:fragmen I tried -->
</ui:repeat>
</ui:repeat>
I can't use c:if inside ui:repeat because doesn't works (Specify conditional rendering of element inside <ui:repeat>? The <c:if> does not seem to work) and I can't use ui:fragment with ui:param because it doesn't works too (Conditional variable definition in JSF)
So, any idea how to solve that?
First of all, avoid of use JSTL if you can use JSF. Here is an answer that explains how JSTL and JSF are executed in different steps -> https://stackoverflow.com/a/3343681/4253629
To redefine conditionally a ui:param, for example, you can do this:
<ui:param
name="marginLeftNode"
value="#{marginLeftNode gt 4 ? myBean.viewWrapper.nodeList.get(status.index).depth : marginLeftNode }"/>
Probably exists another solution, but this works.
Greetings
Changing the ui:param inside the loop has no real value. The real purpose of a ui:param is to pass runtime variables into a template client or included file. By the time your parameter has been passed in, there's little value in changing it. If all you want is to conditionally alter the value of the variable after it's been passed, you could just use JSTL's c:set to set a page-scoped variable that you could then use
<ui:repeat value="#{myBean.viewWrapper.linkMap.entrySet().toArray()}" var="map">
<ui:repeat var="link" value="#{map.value}" varStatus="status">
<li style="margin-left: #{marginLeftNode}%" class="ico-#{link.getStyleCss()}-ayuda">
#{link.getTitle()}
</li>
<c:if test="#{marginLeftNode gt 4}">
<c:set var="marginLeftNode" value="#{myBean.viewWrapper.nodeList.get(status.index).depth}"/>
</c:if>
</ui:repeat>
You could then access your set variable as #{marginLeftNode} anywhere within that view

JSF 2 f:param inside of ui:repeat

as the question above mentiones, i need to create "dynamic" params for a
<ui:composition>
<h:link>
<h:outputText value="link with params" />
<ui:repeat var="parameter" value="#{bean.getCurrentParameter}"> //customClass
test: #{parameter.name} #{parameter.value} //output is fine
<f:param name="#{parameter.name}" value="#{parameter.value}" />
</ui:repeat>
</h:link>
</ui:composition>
unfortunately the "test" returns all values correctly, but when I hover the link, there is not a single parameter set ("page.xhtml" instead of "page.xhtml?param1=ddd&param2=sss...")
To unterstand why I need this, I want to get all parameters of the current page and add/remove one (the link clicked on is the one I want to remove/add).
to I need to generate for each link its own parameters (when param1=1,2 by default, one link has e.g. "param1=1,2,3" (appends 3) and the other one has "param1=1,2,4" (appends 4))
Classic taghandlers vs component tags issue. <ui:repeat/> is a component tag that runs after the view tree has been built while <f:param/> is a tag handler that is placed in the view tree during view build. What this means is that <f:param/> is parsed and processed before <ui:repeat/> ever makes it into the page. As a result var="parameter" is not available for use when <f:param/> needs it.
To fix, use the <c:forEach/> tag instead:
<h:link>
<h:outputText value="link with params" />
<c:forEach var="parameter" items="#{bean.getCurrentParameter}">
test: #{parameter.name} #{parameter.value}
<f:param name="#{parameter.name}" value="#{parameter.value}" />
</c:forEach>
</h:link>

Passing EL method expression as attribute of custom Facelets tagfile

I created a custom JSF tag:
<ui:composition>
<h:panelGroup>
<rich:dataScroller id="#{id}" for="#{table}" execute="#{table}"
page="#{scrollerPage}" render="#{table}-sc1" maxPages="5"
fastControls="hide" oncomplete="#{onCompl}" scrollListener="#{scrollListenerBean[scrollListenerMethod]}" />
<h:inputText value="#{scrollerPage}" id="#{table}-sc1" size="2">
<f:convertNumber integerOnly="true" />
</h:inputText>
<h:outputText styleClass="outputText"
value=" of #{scrollPagesCount} " />
<h:commandButton value="GO! " />
</h:panelGroup>
</ui:composition>
To pass the listener method, I used the solution suggested in a quite old blog:
<my:dataScroller id="idDS1" table="table1"
scrollerPage="#{bean.navigationHelper.scrollerPage}"
scrollPagesCount="#{bean.navigationHelper.scrollPagesCount}"
onCompl="initForm();"
scrollListenerBean="#{bean}"
scrollListenerMethod="aMethod" />
My questions are: is this the best way to do this? How can I make the method optional?
Thanks a lot for any Help! bye!
My questions are: is this the best way to do this?
That's the only way anyway, provided that you can only use standard JSF/EL facilities and you cannot create a custom taghandler.
You could however create a custom taghandler to convert the value expression to a method expression. The OmniFaces JSF utility library has a <o:methodParam> for exactly this purpose. See also the <o:methodParam> demo page.
You could then end up like:
<my:dataScroller ... scrollListener="#{bean.aMethod}" />
and
<o:methodParam name="scrollListenerMethod" value="#{scrollListener}" />
<rich:dataScroller ... scrollListener="#{scrollListenerMethod}" />
See also:
Dynamic ui include and commandButton
How can I make the method optional?
Theoretically, you could use JSTL tags to build the view conditionally. Something like:
<h:someComponent>
<c:if test="#{not empty fooAttribute}">
<f:attribute name="foo" value="#{fooAttriubte}" />
</c:if>
</h:someComponent>
But that's in the particular case of a special method expression listener attribute unfortunately not possible. There's no such thing as <rich:scrollListener> or something which allows you binding a RichFaces specific scrollListener as a separate tag to the <rich:dataScroller>. Best what you could do without creating custom taghandlers is duplicating the whole <rich:dataScroller> in two <c:if>s (or a <c:choose>); one with and other without scrollListener. This is too clumsy. You'd really better create a custom <my:richScrollListener> taghandler for this which you could then place in a <c:if>.

Use c:set inside ui:repeat does not work

I have a JSF 2 application with the following code:
<c:set var="number" value="0" scope="view" />
<ui:repeat value="${items}" var="item" varStatus="itemIndex">
<c:if test="${item.boolProperty == true}">
<c:set var="number" value="${number + 1}" scope="view" />
</c:if>
<h:outputText value="#{item.name}" />: <h:outputText value="#{number}" />
</ui:repeat>
I want to increase the number depending on the property of the item in the loop. However, the condition seems not to work, since the value of number always stays 0. If I remove the condition, the number is incremented only once, or it is always 0 before incrementing, therefore it outputs 1. Could it be possible the number var changes not to affect the number var that is outside the loop? I believe the scope attribute takes care of that.
JSTL tags and JSF components doesn't run in sync as you'd expect from the coding. JSTL tags runs during building of the JSF view, while JSF components runs during rendering of the JSF view. See for a detailed explanation also JSTL in JSF2 Facelets... makes sense?
What you want to achieve is unfortunately not possible with JSF components. The <ui:param> comes close, but it functions merely as an alias for a more complex EL expression, while the <c:set> actually sets something in the desired scope (the view scope which you've there is by the way wrong).
Your best bet is to change the model or wrap the model in another model so that you end up as
<ui:repeat value="${items}" var="item" varStatus="itemIndex">
<h:outputText value="#{item.name}" />: <h:outputText value="#{item.number}" />
</ui:repeat>

Resources