inserting a faces (JSF) EL element on an iframe - jsf

I have an iframe like this
<iframe src="path/of/destiny"></iframe>
and I want to write something like this
<iframe src="path/of/destiny?#{myParameter}"></iframe>

That should be possible if you're using Facelets as view technology. But since you're asking this question, I assume that you're still using the legacy JSP as view technology. JSP doesn't allow deferred EL (#{}) in template text as Facelets does. You can however use standard EL (${}) in template text.
<iframe src="path/of/destiny?${myParameter}"></iframe>
You only need to take into account that whenever this has to be obtained as a JSF managed bean property like ${bean.myParameter}, that this bean is already been created and available in the scope. You can ensure this by calling #{bean.something} in a fullworthy JSF component somewhere before the line with the <iframe>. The #{} will create the bean if not created yet.

Related

Should I use ui:fragment with the render attribute to conditionally render HTML tags in Facelets with JSF 2.2?

I am in the process of upgrading an old project from JSF 1.1 to JSF 2.2. Specifically, I am upgrading the JSF implementation from MyFaces 1.1 to MyFaces 2.2.12, replacing JSPs with Facelets and upgrading/replacing outdated tag libraries. I am mainly using Migrating from JSF 1.2 to JSF 2.0 as a guide.
The project used some tag library called htmLib with the namespace http://jsftutorials.net/htmLib in it's JSP pages. I can't find any documentation about this tag library anymore, neither on the jsftutorials webpage nor elsewhere, but apparently it was used to include plain HTML tags like <div> or <table> in JSP pages.
Since plain HTML tags can now be used in XML Facelets with JSF2, I am right now removing all occurences of tags from the htmLib taglib like <htm:div>...</htm:div> and replace them with plain HTML tags like <div>...</div>.
However, some of the tags used from htmLib contain the render attribute for conditional rendering, like this:
<htmLib:h4 render="someCondition">
...
</htmLib:h4>
Because plain HTML tags don't have a render attribute for this purpose, I was searching for an alternative way to conditionally render plain HTML tags and stumbled upon this answer on SO: How to conditionally render plain HTML elements like <div>s?
So, my idea is to replace a construct like the one above with something like
<ui:fragment render="someCondition">
<h4>
...
</h4>
</ui:fragment>
My questions:
Is wrapping HTML tags inside a <ui:fragment> tag with the render
attribute the recommended way to conditionally render HTML tags, or
is this method only valid and recommended for the case in the linked question?
Are there other ways to conditionally render plain HTML tags in Facelets that should be preferred?
Does the <ui:fragment> wrapping method work, no matter what kind of plain HTML is contained within it?
Can conditionally rendered <ui:fragment> blocks be nested?
There's no limitation in that. Not even for wrapping ui:fragment.
Basically:
In order just to control the inner content, with no extra HTML generation use
ui:fragment.
To generate an extra HTML span element, use h:panelGroup.
To generate an extra HTML div element, use h:panelGroup layout="block".
The HTML you have inside isn't a problem. JSF, being a server side framework, performs all the HTML building/rendering job in the server, so the JSF/facelet tags get translated to HTML before the response being sent. All the plain HTML you use inside will remain HTML.
However, beware of using tag handlers inside UI Components when migrating from 1.x. Tag handlers (ui:include, c:if, c:forEach) evaluate before the UI Components (which tipically contain rendered clauses). This has been a source of conflict in JSF 2.

binding bean method to rendered jsf tag

I would like to know is there any way to hide/show links to certain pages by using the rendered jsf tag in conjunction with a bean method.
for eg. rendered="#{sidebarController.isGrantedAcess("Admin")} doesn't work

Where can i find the Servlet generated by the JSF file?

When i try to run my jsf file i get this warning:
12:27:49,357 WARNING [javax.enterprise.resource.webcontainer.jsf.renderkit] (http-localhost- 127.0.0.1-8080-7) JSF1090: Navigation case not resolved for component j_idt24.
In order to fix this problem I need to find out which one is the j_idt24 component, And I'm not sure how to do it, so I figured that I would probably find it in the generated servlet file(Am i right?) , So where can i find the generated Servlet file, or what would be a better way?
-Java
You're confusing JSF with JSP. JSF is a MVC framework which can for the "V" part use either JSP, or Facelets or something entirely different.
What you're stating is true for JSP, but not necessarily for JSF. In JSF2, JSP is succeeded by Facelets which is compiled to a XML document, not a Servlet class. You're also confusing "JSF source code" with "JSF component tree". Those autogenerated IDs are not visible in the compiled XML document of Facelets nor Servlet class of JSP. They are only created during generating the HTML output based on the JSF component tree in server's memory during view render time (that JSF component tree is in turn created based on that XML document or Servlet class during view build time).
Coming back to your concrete problem, this warning will occur when you specify an invalid outcome in <h:link> or <h:button> component. Easiest way to naildown the culprit is to give every single <h:link> and <h:button> a fixed ID so that JSF doesn't need to autogenerate them so that you can just do rightclick, View Source in browser and do a Ctrl+F.
<h:link id="fooLink" value="Foo" outcome="foo" />
An alternative is to add <ui:debug> and explore the JSF component tree which is presented "plain text" in the debug popup and then trackback the found component to its declaration in the JSF (XHTML) source code.
See also:
WARNING JSF1090: Navigation case not resolved for component j_idt51
how to debug JSF/EL

JSF set property on ManagedBean in included Faceltes page

I am new to JSF and getting very confused doing something trivial. I am making up this example here to elaborate what I am want to do:
I have a xhtml fragment, say, stockQuoteFragment.xhtml, which is backed by a ManagedBean, say, StockQuoteService.java. StockQuoteService.java has property stockID and a method getStockQuote() which has all the logic to get the stockQuote for the value set on stockID property. stockQuoteFragment.xhtml displays #stockQuoteService.stockQuote.
Now I have another page Home.xhtml page with backing bean HomeBackingBean.java with a method getUserFavoriteStockID(). I want to include content of stockQuoteFragment.xhtml in Home.xhtml passing in the value of #homeBackingBean.userFavoriteStockID to StockQuoteService.setStockID().
I am not sure how to do this in JSF/Facelets. With simple JSPs I could do this easily with a JSP include and include parameters
No.
...According to TLD or attribute directive in tag file, attribute var does not accept any expressions.
I have just tried it.
But if you use pure XML based JSF with tags, you can easily use <ui:param> as discussed here. I use JSF in JSP and for me there is no help (<c:set> is mostly useless).
Can I just do this in Home.xhtml before I ui:include stockQuoteFragment.xhtml into it:
<c:set var="#{StockQuoteService.stockID}" value="#homeBackingBean.userFavoriteStockID"/>
will that work?

How to not render whole block in JSF?

Is there a JSF 2.1 component which lets me conditionally render (or not render) all its content? Something like
<h:component rendered="#{user.loggedIn}">
...a bunch of jsf components and HTML code...
...even more HTML code...
</h:component>
I am using PrimeFaces 3M4 as this may influence your answer!
<h:panelGroup>
If you set attribute layout="block", you will have a <div> tag
Otherwise, you have a <span> tag.
In general most of jsf components support the render attribute (never bumped in some that does not),
container components like h:panelGrid or h:panelGroup supports the rendered attribute and if its set to false all its child will be hidden too
Same goes for the primefaces components ,and if not it probably a bug (i think there was an issue with tabview of primefaces)
Here's a link for primefaces user guide, you can find supported attributes of all primefaces components there User’s Guide for 3.0.M4

Resources