Customising Richfaces skins in JBoss 7 through CSS overriding - jsf

In my richfaces 4.2.0 application, all the pages use the same template:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>MY APPLICATION</title>
<link href="/myapp/resources/css/stylesheet.css" rel="stylesheet" type="text/css"/>
</h:head>
<h:body class="form" >
<div id="page">
.....
</div>
</h:body>
The file stylesheet.css is used to define some global styles as well as to override some richfaces classes. My application uses automatic skinning (org.richfaces.enableControlSkinning = true) as well: in brief, stylesheet.css has been designed to override everything produced by RF which was not looking as desired.
Everything worked fine under JBoss 6 because the RF styles (ECSS) were imported before stylesheet.css and hence got overridden. Under JBoss 7 (EAP 6) happens exactly the contrary, so stylesheet.css has simply no effect.
Do you know if (and how :)) I could influence this behavior?
Thanks a lot

After making some researches on the Web I found out that a good practice should be to place the custom css (using h:outputStylesheet) at the end of the page (or better of the template). This way they are imported at the end of the head section of the generated HTML.

The only way I have found reliably to work is to add an !important behind each overridden RichFaces rule. But this is rather dirty.

Related

h:body not rerendered when using FullAjaxExceptionHandler

I'm using the OmniFaces FullAjaxExceptionHandler to display error pages. The error pages are shown correctly, but I'm having issues with the styling of those pages.
My application is using a template which has CSS classes defined on the body element. These classes are different for normal and error pages:
Normal page:
<h:body styleClass="main-body layout-compact">
Error page:
<h:body styleClass="exception-body error-page">
When the FullAjaxExceptionHandler processes an exception, a forward to the error page is performed (based on the <error-page> mechanism in web.xml). Apparently this does not rerender the <h:body> tag, because when checking the HTML output, I can see that the <body> tag still contains the CSS classes from the normal page (instead of the classes of the error page).
It seems that the content of the original <h:body> is replaced with the content of the error page <h:body> instead of just replacing the full <h:body>. I don't know if this is default JSF / FullAjaxExceptionHandler behaviour.
Is there any way to have the <h:body> rendered with the correct CSS classes? Moving the CSS classes away from <h:body> is not an option.
This is unfortunately "by design". JSF doesn't replace the entire document when performing ajax navigation, but it only replaces the children of individual <head> and <body> elements, leaving the parents untouched. This is done so for historical reasons; older Internet Explorer versions namely doesn't support replacing them altogether.
What I have done myself is to simply put the style into the <main> element instead. The <header> and <footer> are usually identical anyway in the final HTML output. Basically:
<html>
<head>
<title>...</title>
</head>
<body>
<header>...</header>
<main class="#{page.type}">...</main>
<footer>...</footer>
</body>
</html>
If you really need to have the <body class> modified, then your best bet is to do so via JavaScript embedded in the error page template.
<h:outputScript rendered="#{faces.ajaxRequest}">
document.body.className = "exception-body error-page";
</h:outputScript>
Note: #{faces} is only available since OmniFaces 2.5, if you're using an older version, use instead #{facesContext.partialViewContext.ajaxRequest}).

OmniFaces CombinedResourceHandler does not combine JavaScript resources

I would like to use the OmniFaces CombinedResourceHandler to stream resources in one go.
I registered it in faces-config.xml without any additional configuration parameters as described in CombinedResourceHandler documentation.
While it works fine with CSS resources, it does nothing with JavaScript resources. Here are my tests:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com /jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:o="http://omnifaces.org/ui">
<h:head>
<title>CombinedResourceHandlerTest</title>
<h:outputStylesheet name="css/bootstrap-3.3.5/bootstrap.css"/>
<h:outputStylesheet name="css/main.css" />
<h:outputScript name="js/jquery/jquery.min.js"/>
<h:outputScript name="js/bootstrap-3.3.5/bootstrap.min.js"/>
</h:head>
<h:body>
<f:view>
<h2>CombinedResourceHandlerTest</h2>
</f:view>
</h:body>
Output:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="j_idt2">
<title>CombinedResourceHandlerTest</title>
<script type="text/javascript" src="/testApp/javax.faces.resource/js/jquery/jquery.min.js"></script>
<script type="text/javascript" src="/testApp/javax.faces.resource/js/bootstrap-3.3.5/bootstrap.min.js"></script>
<link type="text/css" rel="stylesheet" href="/testApp/javax.faces.resource/eNpLLi7WT8rPLykuKUos0DXWM9YzRfD1kouLa4BYPzcxMw_EAQCLpxEP.css?ln=omnifaces.combined&v=1480321351184">
</head>
Tried with attribute target="head":
<h:head>
<h:outputScript name="js/jquery/jquery.min.js" target="head"/>
</h:head>
...
Output: (scripts are completly missing):
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="j_idt2">
<title>CombinedResourceHandlerTest</title>
<link type="text/css" rel="stylesheet" href="/testApp/javax.faces.resource/eNpLLi7WT8rPLykuKUos0DXWM9YzRfD1kouLa4BYPzcxMw_EAQCLpxEP.css?ln=omnifaces.combined&v=1480321351184">
</head>
...
</html>
The scripts are also missing when i move them on the top of the body:
<h:body>
<h:outputScript name="js/jquery/jquery.min.js" target="head"/>
....
</h:body>
After a look into the source i also tried with
<o:deferredScript name="js/jquery/jquery.min.js"/>
After inspecting the output for this case, I saw that the combinend script only contains the first script in order and the console shows "ReferenceError: OmniFaces is not defined":
<body>
<script type="text/javascript">OmniFaces.DeferredScript.add('/testApp/javax.faces.resource/eNpLL81JLE7OsMoq1s8qLE0tqoRSermZeXpZxQDDagwa.js?ln=omnifaces.combined&v=0');</script>
</body>
And I noticed, that even jsf.js is not included when having the CombinedResourceHandler active. the browser console tells "mojarra is not defined".
What am I doing wrong? Thanks in advance!
My environment is: Mojarra 2.2.12, Omnifaces 2.5.1, Tomcat 8.
I reproduced a very similar issue last weekend. The cause boiled down to that Mojarra was initialized twice on a Tomcat 8 server and thus corrupted the one and other. You can confirm this by looking at the server log and notice that among others Mojarra version, OmniFaces version and PrimeFaces version are logged twice.
Please doubleverify if you have only one Mojarra instance and that you do not have the ConfigureListener entry in web.xml like below, as it's by default autoregistered already.
<!-- You MUST remove this one from web.xml! -->
<!-- This is actually a workaround for buggy GlassFish3 and Jetty servers. -->
<!-- When leaving this in and you're targeting Tomcat, you'll run into trouble. -->
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
See also:
Configuration of com.sun.faces.config.ConfigureListener
How to properly install and configure JSF libraries via Maven?
For anyone else who runs into this issue I was also running into this issue using Jboss EAP 7.1.
I accidentally had the OmniFacesCombinedResourceHandler declared in a faces-config.xml in a Web Fragment JAR and in the faces-config.xml of the web application itself. Having this declared twice caused the same symptoms as the issue listed above. Once I removed it from the webapp faces-config.xml it started working.
I raised the issue on the OmniFaces issues to possibly detect and raise an error if this situation occurs: https://github.com/omnifaces/omnifaces/issues/504

JSF Primefaces datatable appearance [duplicate]

I created a JSF page with PrimeFaces components. The project runs fine, but the PrimeFaces UI look and feel is completely missing. I'm only noticing below message in server log:
One or more resources has the target of 'head' but not 'head' component has been defined within the view
What does this mean and how can I fix the PrimeFaces UI styling?
This means that you're using plain HTML <head> instead of JSF <h:head> in your XHTML template. The JSF <h:head> allows automatic inclusion of CSS/JS resources in the generated HTML <head> via #ResourceDependency annotations. PrimeFaces as being a jQuery based JSF component library needs to auto-include some jQuery/UI JS/CSS files and this really requires a <h:head>.
So, search for a
<head>
<title>Some title</title>
...
</head>
in your templates and replace it by
<h:head>
<title>Some title</title>
...
</h:head>
See also:
What's the difference between <h:head> and <head> in Java Facelets?
Unable to understand <h:head> behaviour
How to programmatically add JS and CSS resources to <h:head>
How to include another XHTML in XHTML using JSF 2.0 Facelets?

Why JSF needs XHTML instead of HTML? [duplicate]

Facelets relies on XML namespaces to work with XHTML. How are HTML 4, and as far as I know, HTML 5 do not support namespaces. Also HTML 5 has some new elements that are not available in XHTML. Even HTML 4 and XHTML have some differences regarding elements and attributes they support.
The question is: Is it possible to render HTML 4/5 documents using Facelets? If so, how?
Since Facelets is a XML based view technology which eats and emits in essence XML markup, you cannot use it with a HTML4 doctype. The HTML4 doctype describes several elements which cannot be self-closing, like <link>, <meta>, <br> and <hr>. However, with XML you're forced to close them like <link/>, <meta/>, etc. So using a HTML4 doctype is absolutely not an option for Facelets (that is, when you respect the standards and/or fear the w3 validator, it will however work perfectly on the most if not all webbrowsers).
HTML5, on the other hand, allows XML markup. This is specified in chapter 3.2.2 - Elements:
Example:
<link type="text/css" href="style.css"/>
Authors may optionally choose to use this same syntax for void elements in the HTML syntax as well. Some authors also choose to include whitespace before the slash, however this is not necessary. (Using whitespace in that fashion is a convention inherited from the compatibility guidelines in XHTML 1.0, Appendix C.)
I myself use <!DOCTYPE html> all the way, also with JSF/Facelets, even without a <?xml?> declaration in top of the page. It works perfectly in all browsers. With a XHTML doctype you should as per the specification be using a Content-Type of application/xhtml+xml which would only make MSIE to choke (it doesn't understand it). And since that's still one of the most widely used browsers... Replacing the XHTML content type by text/html is considered harmful, you also don't want to do this.
As per your arguments:
HTML 5 do not support namespaces.
This doesn't matter. The namespaces are only of interest for the XML based server side view technology (like as Facelets) which in turn can generate pure HTML with those tags. The following example is legitimately valid for Facelets:
<!DOCTYPE html>
<html lang="en"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Title</title>
</h:head>
<h:body>
<h:outputText value="#{bean.text}" />
</h:body>
</html>
This renders legitimately valid HTML5 (for the client side):
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>
</head>
<body>
Some text
</body>
</html>
You see, Facelets already removes the XHTML declarations since they have no meaning in the client side.
And,
Also HTML 5 has some new elements that are not available in XHTML
this make also no sense. It's all about the generated output. Which can be HTML5 as good. Your only problem may be the browser support and the availability of 3rd party JSF components which renders HTML5 specific elements. Since JSF 2.2, it's possible to use the new passthrough elements feature to turn custom elements into a JSF component. Simply give the HTML5 element a jsf:id attribute. It'll transparently internally be interpreted as a UIPanel instance in the JSF component tree (like <h:panelGroup>).
<!DOCTYPE html>
<html lang="en"
xmlns:jsf="http://xmlns.jcp.org/jsf"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
>
<h:head>
<title>Title</title>
</h:head>
<h:body>
<header jsf:id="header">Header</header>
<nav jsf:id="nav">Nav</nav>
<main jsf:id="main">Main</main>
<footer jsf:id="footer">Footer</footer>
</h:body>
</html>
You can even reference it from ajax as in <f:ajax render="main">.
Actually, XHTML is overhyped. Its sole intent is to ease HTML development using XML based tools which can manipulate/transform/generate HTML pages on the server side (like as Facelets). But some starters also use it without using any XML tool and output it plain as-is, because it's "so cool" -for some unclear reason.
Don't get me wrong. XHTML is great as server side view technology. But simply not as client side markup technology. It has utterly no value at the client side.
See also:
Our XHTML wiki page
How should a <!DOCTYPE> section look in JSF? HTML5 or XHTML?
JavaServer Faces 2.2 and HTML5 support, why is XHTML still being used
On a related note, check out this IBM developerWorks article: JSF 2 fu: HTML5 composite components, Part 1
MyFaces has an extension for html5. Try this http://myfaces.apache.org/html5/
I've read, that this should be possible, but I did not do it myself, yet. Maybe you should just use HTML 5 inside the xHTML wrapper code. I will see, if I can find the source of information I've again.
[EDIT]
Seems like, there has been some work at MyFaces to support HTML5 rendering during Google's summer of code. I don't know if it should be used in a productive way, yet.
MyFaces Wiki
java-doc
Jazzon talk, download presentation
Jazzon talk, Exploring HTML 5 with JSF 2
Please give us a feedback, if you get it to work.
[/EDIT]
http://wiki.whatwg.org/wiki/HTML_vs._XHTML has some useful information on how namespaces can be used in HTML5 to assist migration from XHTML. Perhaps you can try applying the namespace as it suggests and see what occurs?

JSF 2.2 html5 friendly page

Some sources indicate that it's possible to use non-obtrusive jsf:id attributes in a JSF2.2 page.
https://weblogs.java.net/blog/edburns/archive/2012/11/01/html5-friendly-markup-jsf-22
http://www.apress.com/9781430244257
The taglib descriptors use different urls.
From the weblog:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:jsf="http://java.sun.com/jsf">
<head jsf:id="head">
From the book:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:jsf="http://xmlns.jcp.org/jsf">
<head jsf:id="head>
However, while using the newest JSF2.2 implementation (2.2.0-m15), both urls for the tag descriptors are unreachable (CANNOT_FIND_FACELET_TAGLIB), resulting in a partially unparsed html page.
Where to find the correct urls for the jsf tag library? Is there some kind of index for those urls?
To use jsf:id use http://xmlns.jcp.org/jsf namespace. This is applicable for form input. It is not used on head tag. For example, the following code declares the namespace with the short name jsf:
<html ... xmlns:jsf="http://xmlns.jcp.org/jsf"
...
<input type="email" jsf:id="email" name="email"
value="#{reservationBean.email}" required="required"/>
Here, the jsf prefix is placed on the id attribute so that the HTML5 input tag's attributes are treated as part of the Facelets page.
The latter is the correct definition. I don't think the head tag is used that way with JSF. Use the JSF html tag library.
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:jsf="http://xmlns.jcp.org/jsf">
<h:head></h:head>
....
You typically use jsf:id for HTML5 input components. Refer to the Java EE 7 Tutorial section on HTML5/JSF pass-through for information and an example application.

Resources