Not able to add HTML to <ui:define> - java

I have recently started working with JSF2.0 and Facelets, but have run into what I hope is an easy answer for most of you out there. When I am trying to add any HTML tag within a <ui:define> tag I receive the following error:
javax.faces.view.facelets.TagException: /content/home/test.xhtml #11,10 Tag Library supports namespace: http://java.sun.com/jsf/facelets, but no tag was defined for name: div
If I remove all of the HTML tags from the section the page displays correctly. Here is my simple page that I have been trying to get working:
<!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://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:jrc="http://com.comanche.web.components">
<ui:composition template="/templates/masterLayout.xhtml" xmlns="http://java.sun.com/jsf/facelets">
<ui:define name="windowTitle">Home</ui:define>
<ui:define name="content">
<div>I want to add HTML and am having lots of trouble.</div>
</ui:define>
</ui:composition>
</html>
I know I should be able to add HTML within the define tag. What do I need to do to get HTML in without any errors.

Your <ui:composition> declaration is using the wrong global XML namespace. You definied http://java.sun.com/jsf/facelets as global XML namespace while it should have been assigned to ui: XML namespace. The <div> tag doesn't exist in the Facelets taglib (which is what the exception is trying to tell you). You should have assigned http://www.w3.org/1999/xhtml as global XML namespace. Further, the <!DOCTYPE> and <html> will be ignored anyway. The sole content of the file should be the following:
<ui:composition template="/templates/masterLayout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:define name="windowTitle">Home</ui:define>
<ui:define name="content">
<div>I want to add HTML and am having lots of trouble.</div>
</ui:define>
</ui:composition>
Nothing before or after <ui:composition> in the very same file is necessary.
See also:
How to include another XHTML in XHTML using JSF 2.0 Facelets?

Related

Set only non-empty attributes in JSF template tags

I have defined a number of template tags in JSF 2.0, but have stumbled across a problem with conditionally setting attributes.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<ui:composition>
<p:inputText converterMessage="#{converterMessage}">
<ui:insert />
</p:inputText>
</ui:composition>
</html>
In the above case, when I set converterMessage, it works well. However, when I it out and call this component without attributes, then basically converterMessage is empty on the p:inputText, hence no message is shown when a converter error occurred.
Instead, I want the default message to be displayed in this case, so NOT setting converterMessage on the inputText.
Is there any way how to do this? I did try passing "null" but this, as expected, doesn't really solve the problem.

JSF/Facelets : CSS file resource is not found

I am trying to build a simple JSF application, but when I add css in the template/basic.xhtml using the code below, application server (both tomcat 7 and glassfish 4) resolve to RES_NOT_FOUND
<h:head>
<title><ui:insert name="title">Enrollment System</ui:insert></title>
<h:outputStylesheet library="common" name="css/reset.css" />
<h:outputStylesheet library="common" name="css/common.css" />
</h:head>
The index.xhtml contains simply
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="/templates/basic.xhtml">
<ui:define name="content">
<h1>Welcome, #{loginSession.privilege}</h1>
</ui:define>
</ui:composition>
</html>
I've read another few posts and also Mkyong's JSF tutorial that I should put css in some specific directory, but still failed to make it work.
Here are locations of where my files are put at:
enrollment/index.xhtml
enrollment/WEB-INF/templates/basic.xhtml
enrollment/resources/common/css/reset.css
enrollment/resources/common/css/common.css
Is there any other config that was missing to make it work?
Here is what I find in the stack which the null ResourceInfo is generated and returned.
Daemon Thread [http-bio-80-exec-3] (Suspended)
owns: SocketWrapper<E> (id=62)
ResourceManager.findResource(String, String, String, boolean, FacesContext) line: 190
ResourceManager.findResource(String, String, String, FacesContext) line: 180
ResourceHandlerImpl.createResource(String, String, String) line: 201
ResourceHandlerImpl.createResource(String, String) line: 181
StylesheetRenderer.encodeEnd(FacesContext, UIComponent) line: 97
getFromCache will search in a HashMap using the four parameter provided.
ResourceInfo info = getFromCache(resourceName, libraryName, localePrefix, contracts);
I've examine all the values in the HashMap, there is only two resources namely
"/"
"/WEB-INF/templates/basic.xhtml"
This probably is the cause as the resources are never added to the hashmap
I fixed the problem out of accident.
The fix is DO NOT select "Generate web.xml deployment descriptor" when creating project.

primefaces components not working with own css

i am using ready-made template(with css and j-queries) in my java ee app. all the primefaces components are rendered properly except the panelgrid control of primefaces 3.2.
it is displayed with border. i want it without border.
i have removed all the table styling from the css of custom ready-made template.
still the border is there.
when i remove the readymade template, the panelgrid is rendered perfectly without any border. how do i remove the border and what is the cause of this problem?
edited:
xhtml file:
<!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://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AP administration panel - A massive administration panel</title>
</h:head>
<h:body>
<div>
<h:form>
<p:panelGrid columns="2" style="border: none">
<h:outputText value="scrip symbol"/>
<p:inputText value=""/>
<p:commandButton value="submit"/>
</p:panelGrid>
</h:form>
</div>
</h:body>
</html>
When overriding PrimeFaces default styles, you have to specify a CSS selector of at least the same strength or to specify a stronger selector. The strength of a CSS selector (the cascading rules) is specified in the W3 CSS specification and clearly explained in this article: Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade.
Based on PrimeFaces own CSS, the following selectors should do:
.ui-panelgrid tr, .ui-panelgrid td {
border: none;
}
Just put them in a .css file which you include by <h:outputStylesheet> inside the beginning of the <h:body> so that it will be included after PrimeFaces own style.
<h:body>
<h:outputStylesheet name="layout.css" />
...
</h:body>
See also:
How to remove border from specific PrimeFaces p:panelGrid?
How do I override default PrimeFaces CSS with custom styles?
Update: As per your update, your CSS doesn't seem to be loaded at all. You should have noticed this by verifying the HTTP traffic in browser builtin webdeveloper toolset (press F12 in Chrome/IE9/Firebug) and seeing that it returned a HTTP 404 error. When using <h:outputStylesheet> you need to put the CSS file in the /resources folder of the webcontent. So you must have a /resources/css/mycss.css in order to be able to use <h:outputStylesheet name="css/mycss.css" />.
See also:
How to reference CSS / JS / image resource in Facelets template?

How include page in JSF?

i'm new in jsf, i would like to know how i can reuse others .jsf pages without 'copy paste' them.
In .jsp i do:
// head.jsp
<head> ... </head>
// top.jsp
<body> ... </body>
Then i include them in my new .jsp
// index.jsp
<%#include file="head.jsp" %>
<%#include file="top.jsp" %>
...
How can i do this with jsf ?
i'm trying this way:
<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>
<ui:include src="components/head.xhtml" />
</h:head>
<h:body>
<ui:include src="components/top.xhtml" />
</h:body>
</html>
But is not working..
Any idea ?
Best regards,
Valter Henrique.
Facelets is the default view technology for JSF2, so I would use its <ui:include> tag here. Make sure your paths are correct - they should start with webapp root, one containing WEB-INF - and also make sure the included facelets contain <ui:composition> tag around the included content. Anything outside this tag will be ignored.
Try looking for the "import" tag in the core library.
I think ui:include might be for facelets rather than plain jsf.

unable to use div tag inside composite components

I wrote the following code and save it as a separate file.
<!DOCTYPE html>
<ui:composition
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<div id="crb_header">
<br/>
<h4 align="right">Welcome : #{homebean.user}</h4>
<br/>
</div>
</ui:composition>
The above page is attached to the main page usingui:include and the following warning messages are visible at the end of the main page.
Warning: This page calls for XML namespace declared with prefix div but no taglibrary exists for that namespace.
Warning: This page calls for XML namespace declared with prefix h4 but no taglibrary exists for that namespace.
Warning: This page calls for XML namespace declared with prefix br but no taglibrary exists for that namespace.
Warning: This page calls for XML namespace declared with prefix br but no taglibrary exists for that namespace.
Can't we use html tags inside this ui:composition files?
Didn't you miss to include the xhtml namespace inside the ui:composition?
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">

Categories