I trying to create a custom tag the Java way, by extending TagSupport. I'm want to use the custom tag to render some html in a xhtml file which also used RichFaces and JSF.
All the tutorials I've seen regarding custom tags you register then in a jsp page via:
<% taglib prefix="example" tagdir="/WEB-INF/tag-descriptor.tld" %>
However this does not work in xhtml documents. I've seen the custom xhtml tags based on xhtml snippets, but I really need logic in the code.
So how do you register a jsp tag lib in jsf? Is this possible? And if not, is there anyway to script java in a xhtml page as you do with jsp?
For a Facelet (.xhtml) you can't register a JSP tag lib.
Facelets is a completely different technology than JSP and those two are not compatible in any way where it concerns artifacts that are specific to each technology (like javax.servlet.jsp.tagext.TagSupport).
However, what's behind a custom JSP tag for JSF is a component, and you can of course author these in Java for Facelets. It boils down to creating a component directly in Java and then registering this in a *-taglib.xml (for JSF 2.2, this registration in an XML file won't be needed anymore).
See Simple Java based JSF custom component for more info.
Related
I am in the process of upgrading an old web application from MyFaces 1.1 to version 2.2. I am new to JSF and still have a lot to learn about it (struggling a lot along the way). Because of this I often don't understand why a specific tag was used from some 3rd party tag library instead of the core JSF libraries.
For example, the application uses the t:stylesheet tag from the Tomahawk component library in the head of the page even though there seems to be a h:outputStylesheet tag in the html library of JSF.
I was wondering if the outputStylesheet tag wasn't available in JSF 1.1 and was only added later or if there is another reason for choosing the Tomahawk tag over the JSF html tag.
Is there a way to determine which version of JSF added a specific tag to the core libraries? Apart from replacing the "2.1" in http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/h/outputStylesheet.html until I reach a the URL that isn't available?
I have migrated my application from JSF 1.2 to 2.2.
It used XML namespaces on java.sun.com domain like xmlns:f="http://java.sun.com/jsf/core". However, Oracle's Java EE 7 tutorial is using XML namespaces on xmlns.jcp.org domain like
xmlns:f="http://xmlns.jcp.org/jsf/core".
Which one is recommended and why was this changed?
Which one is recommended?
Go ahead with XML namespaces on xmlns.jcp.org domain. This was newly introduced since Java EE 7 in 2013 (which covers a.o. JSF 2.2, Servlet 3.1, CDI 1.1, etc). Do note that this not only affects Facelets files, but also XML configuration files such as faces-config.xml, web.xml, beans.xml, etc.
The old XML namespaces on java.sun.com are still there for backwards compatibility, but the support will eventually disappear in a future Java EE version. You should migrate your code base as soon as you can. It should be a trivial task using "find and replace in all files" facility offered by the average IDE.
Only older Mojarra 2.2.0 / 2.2.1 versions have had bugs related to the XML namespace changes, but those should not manifest in newer versions. See also a.o.
Using new xmlns.jcp.org namespace on composites causes java.lang.NullPointerException at java.util.concurrent.ConcurrentHashMap.putIfAbsent
f:viewParam doesn't pass required parameter when new xmlns.jcp.org namespace is used
The metadata component needs to be nested within a f:metadata tag. Suggestion: enclose the necessary components within <f:metadata>
and why was this changed?
Because Java is not from Sun anymore since 2010. Note that they were smart to not make it java.oracle.com or something tight coupled to the currently owning company. It's now nicely and independently tied to the JCP (Java Community Process), the one really responsible for managing the Java (EE) specifications.
I made a brief summary of all the new official oracle namespaces:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
xmlns:cc="http://xmlns.jcp.org/jsf/composite"
xmlns:fn="http://xmlns.jcp.org/jsp/jstl/functions"
xmlns:jsf="http://xmlns.jcp.org/jsf">
<!-- Content here -->
</html>
I use this as template for all of my .xhtml files. Details can be found in the official facelet tag library: JavaServer Faces 2.2 Facelets Tag Library Documentation
Hope this helps :)
For future visitors having namespace confusion/issue:
I would like to highlight the general way to find out which namespace to use:
If you want to use tags from JSF HTML tag library or JSF core tag library then open the JSF implementation JAR (like Oracle Mojarra, Apache MyFaces- myfaces-impl-2.3.1.jar) and find the tag library's .tld or .xml file (you can find it under META-INF directory) and use the namespace mentioned over there.
If you want to use the RichFaces or PrimeFaces then open their implementation JAR (like richfaces-components-ui-4.0.0.Final.jar, or primefaces-6.2.jar) and do same thing as above.
If implementation has .tld (like rich.tld) then you can use the value of <uri> element for example <uri>http://richfaces.org/rich</uri>. And if implementation has .xml (like rich.taglib.xml) then you can use the value of <namespace> element for example <namespace>http://richfaces.org/rich</namespace>
What I have mentioned above is strictly related to JSF but holds good in general as well. Key thing is that if you use the namespace from the implementation JAR then you will never run into issues.
In my Adf application I need to use a javascript component called ckeditor (http://ckeditor.com/). Ideally I would like to use it on a facelet page (not a jsp page). It seems like this can be achieved by using ck-jsf-editor (https://code.google.com/p/ck-jsf-editor/). The problem is that I can't figure out how to add this library/taglib to my Adf application.
Anyone knows the best way to do this?
In JDeveloper, you need to add the JAR that contains the JSF component to the project properties under the JSP tag libraries section.
If you are on JDeveloper 11.1.2 or 12c then your ADF application can already use facelets.
I have a JSF 1.2 based webapp from which I copied the JSTL import
xmlns:c="http://java.sun.com/jstl/core"
You can also find this import in many places on the Internet. I can't remember whether the above worked in my former web project or not, in any case, it does not work in my current JSF 2.0 based webapp. I got a warning from the container saying:
Warning: This page calls for XML namespace http://java.sun.com/jstl/core declared with prefix c but no taglibrary exists for that namespace.
I then changed the import to
xmlns:c="http://java.sun.com/jsp/jstl/core"
... which removed all headaches using <c:forEach> in a JSF/RichFaces-based webapp.
What's the trick here? Have there been any changes to the JSTL import URL from JSF 1.2 to 2.x? Or is the former line generally wrong (in JSF)? What's the difference between the two?
Check our JSTL wiki page: https://stackoverflow.com/tags/jstl/info You can get this kind of page whenever you hover the jstl tag below your question until a black box pops up and then click the info link.
It is true that Facelets 1.x and 2.x uses different namespaces for the JSTL tag library. It was less or more a namespace bug in Facelets 1.x and has been fixed for Facelets 2.x.
The real JSTL 1.0 taglib uses the URI http://java.sun.com/jstl/core.
The real JSTL 1.1/1.2 taglib uses the URI http://java.sun.com/jsp/jstl/core.
Facelets 1.x uses the URI http://java.sun.com/jstl/core.
Facelets 2.x uses the URI http://java.sun.com/jsp/jstl/core.
Facelets 2.2+ uses the URI http://xmlns.jcp.org/jsp/jstl/core.
You can find all Facelets 2.x tags in the Facelets tag library documentation. Facelets don't ship with the full tag set as the real JSTL taglib. Only a few of the <c:xxx> and the full set of <fn:xxx> are copied into Facelets. The <fmt:xxx>, <sql:xxx> and <xml:xxx> tags are not taken over in Facelets.
JSTL is now part of EL.
It is not dependent on JSF implementation but depends on what servlet version your servelet container is compliant for.So these must be running of different servers or different versions of same server.
Short answer is from 2.4 upwards which jboss 7 is .
If you go through JSTL doc here you will find out any way.
Other wise here is good link.
And an other one here from BalusC.
Something like the following
xmlns:jsp="http://java.sun.com/JSP/Page"
seems to not work, any hint? alternatively how can I inject beans into JSF lifecycle flow at startup without the filter usage?
What is the namespace to use for
adding JSP taglib support into
facelets pages?
You cannot. Facelets are not JSPs and do not use JSP tag libraries. From the project page:
The difference is under the hood where
all the burden of the JSP Vendor API
is removed to more greatly enhance JSF
performance and provide easy
plug-and-go development.
how can I inject beans into JSF
Add them to your WEB-INF/faces-config.xml file using managed-bean elements.