How to iterate over a collection or array of values to display data using jsf 2.2 DataTable and facelets? - xhtml

I want to send a mysql request and display the results in a datatable.
I'm using jsf 2.2 and Glassfish server 4.1.1. I've checked several threads in this website but didn't manage to find what I'm doing wrong, which started trying to adapt the project of this website:
http://www.java2s.com/Tutorials/Java/JSF/1900__JSF_DataTable_Add_Delete.htm
So basically I used the code in the UserBean.java but instead of using the demo.xhtml I'm using my own xhtml which is based in it as well:
<!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">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
</h:head>
<h:body>
<h:dataTable value="#{book.bookList}" var="o"
styleClass="book-table"
headerClass="book-table-header"
rowClasses="book-table-odd-row,book-table-even-row">
<h:column>
<f:facet name="header">Book No</f:facet>#{o.bookNo}
</h:column>
<h:column>
<f:facet name="header">Product Name</f:facet>#{o.productName}
</h:column>
<h:column>
<f:facet name="header">Price</f:facet>#{o.price}
</h:column>
<h:column>
<f:facet name="header">Quantity</f:facet>#{o.qty}
</h:column>
<h:column>
<f:facet name="header">Action</f:facet>
<h:commandLink value="Delete" action="#{book.deleteAction(o)}" />
</h:column>
</h:dataTable>
</h:body>
</html>
But instead of displaying the table like this:
Book No Product Name Price Quantity Action
1 CSS 123.12 1 Delete
2 HTML 321.12 2 Delete
3 SQL 12333.3 8 Delete
4 Javascript 1233.33 3 Delete
5 Web 123.22 10 Delete
I get this:
Book No#{o.bookNo} Product Name#{o.productName} Price#{o.price} Quantity#{o.qty} Action
So I'm guessing I'm missing some jar or library? the project includes plenty of them while I only have this one: mysql-connector-java-8.0.15.jar.
For starters I'd like to know which libraries are required to do this operation, since I just don't want to copy/paste everything without knowing what it's used for.
Thanks in advance.

Related

Errors with xhtml

I started to learn xhtml as it's the first topic from the book I'm reading and tried creating a simple form to enter just a name.
This is half of what I copied from the book only keeping the input text field:
<?xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<head>
<title>Enter Customer Name</title>
</head>
<body>
<h:outputStylesheet library="css" name="styles.css"/>
<h:messages/>
<h:form id="test">
<h:outputLabel for="firstname" value="first name:"></h:outputLabel>
<h:inputText id="firstname" label="first name" value="#{test.firstname}" required="true">
<f:validateLength minimum="2" maximum="10">
</f:validateLength>
</h:inputText>
</h:form>
</body>
</html>
But when I open it int he browser, this is what I get
The output
What am I doing wrong?
Your document isn't XHTML.
The Doctype claims it is, but then you have a bunch of errors and non-XHTML content.
The browser is parsing the document in XML mode, which is a good first step if you were actually writing XHTML.
As pointed out in a comment:
The xmlns attribute is not OK. It should be "http://www.w3.org/1999/xhtml" rather than "http://w3.org/1999/xhtml"
Since you got it wrong, your default namespace isn't recognised as XHTML and the browser treats it as an unknown XML format with no special handling. This is why you get view of the XML node tree.
When you correct that, your document begins to be parsed as XHTML.
now all i get is a blank screen
This is because the only XHTML elements you have are the root element, the head and its descendants, and the body.
Everything else (e.g. h:form) is not XHTML. You have imported it from the http://java.sun.com/jsf/html namespace (except for those parts you imported from the http://java.sun.com/jsf/core namespace). (Note that this is forbidden by your choice of Doctype).
Web browsers do not know what to do with elements from those namespaces, so they get inserted into the DOM but are not rendered.
You need to either:
Determine what tool those elements are designed for use with and use that instead of a web browser. (It is possible that that tool outputs code you can use with a web browser).
Write code using languages designed for use with a web browser. That could be real XHTML, but the benefits of it only apply to rare edge cases and you would almost certainly be better off writing HTML.

Issue overriding Primefaces5.3 css? [duplicate]

What is the most correct way to include another XHTML page in an XHTML page? I have been trying different ways, none of them are working.
<ui:include>
Most basic way is <ui:include>. The included content must be placed inside <ui:composition>.
Kickoff example of the master page /page.xhtml:
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Include demo</title>
</h:head>
<h:body>
<h1>Master page</h1>
<p>Master page blah blah lorem ipsum</p>
<ui:include src="/WEB-INF/include.xhtml" />
</h:body>
</html>
The include page /WEB-INF/include.xhtml (yes, this is the file in its entirety, any tags outside <ui:composition> are unnecessary as they are ignored by Facelets anyway):
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h2>Include page</h2>
<p>Include page blah blah lorem ipsum</p>
</ui:composition>
This needs to be opened by /page.xhtml. Do note that you don't need to repeat <html>, <h:head> and <h:body> inside the include file as that would otherwise result in invalid HTML.
You can use a dynamic EL expression in <ui:include src>. See also How to ajax-refresh dynamic include content by navigation menu? (JSF SPA).
<ui:define>/<ui:insert>
A more advanced way of including is templating. This includes basically the other way round. The master template page should use <ui:insert> to declare places to insert defined template content. The template client page which is using the master template page should use <ui:define> to define the template content which is to be inserted.
Master template page /WEB-INF/template.xhtml (as a design hint: the header, menu and footer can in turn even be <ui:include> files):
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title><ui:insert name="title">Default title</ui:insert></title>
</h:head>
<h:body>
<div id="header">Header</div>
<div id="menu">Menu</div>
<div id="content"><ui:insert name="content">Default content</ui:insert></div>
<div id="footer">Footer</div>
</h:body>
</html>
Template client page /page.xhtml (note the template attribute; also here, this is the file in its entirety):
<ui:composition template="/WEB-INF/template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<ui:define name="title">
New page title here
</ui:define>
<ui:define name="content">
<h1>New content here</h1>
<p>Blah blah</p>
</ui:define>
</ui:composition>
This needs to be opened by /page.xhtml. If there is no <ui:define>, then the default content inside <ui:insert> will be displayed instead, if any.
<ui:param>
You can pass parameters to <ui:include> or <ui:composition template> by <ui:param>.
<ui:include ...>
<ui:param name="foo" value="#{bean.foo}" />
</ui:include>
<ui:composition template="...">
<ui:param name="foo" value="#{bean.foo}" />
...
</ui:composition >
Inside the include/template file, it'll be available as #{foo}. In case you need to pass "many" parameters to <ui:include>, then you'd better consider registering the include file as a tagfile, so that you can ultimately use it like so <my:tagname foo="#{bean.foo}">. See also When to use <ui:include>, tag files, composite components and/or custom components?
You can even pass whole beans, methods and parameters via <ui:param>. See also JSF 2: how to pass an action including an argument to be invoked to a Facelets sub view (using ui:include and ui:param)?
Design hints
The files which aren't supposed to be publicly accessible by just entering/guessing its URL, need to be placed in /WEB-INF folder, like as the include file and the template file in above example. See also Which XHTML files do I need to put in /WEB-INF and which not?
There doesn't need to be any markup (HTML code) outside <ui:composition> and <ui:define>. You can put any, but they will be ignored by Facelets. Putting markup in there is only useful for web designers. See also Is there a way to run a JSF page without building the whole project?
The HTML5 doctype is the recommended doctype these days, "in spite of" that it's a XHTML file. You should see XHTML as a language which allows you to produce HTML output using a XML based tool. See also Is it possible to use JSF+Facelets with HTML 4/5? and JavaServer Faces 2.2 and HTML5 support, why is XHTML still being used.
CSS/JS/image files can be included as dynamically relocatable/localized/versioned resources. See also How to reference CSS / JS / image resource in Facelets template?
You can put Facelets files in a reusable JAR file. See also Structure for multiple JSF projects with shared code.
For real world examples of advanced Facelets templating, check the src/main/webapp folder of Java EE Kickoff App source code and OmniFaces showcase site source code.
Included page:
<!-- opening and closing tags of included page -->
<ui:composition ...>
</ui:composition>
Including page:
<!--the inclusion line in the including page with the content-->
<ui:include src="yourFile.xhtml"/>
You start your included xhtml file with ui:composition as shown above.
You include that file with ui:include in the including xhtml file as also shown above.

How do I display a carousel using Products.Carousel on my custom page template?

I have a custom page template for my home page.
It does not display the carousel.
The template is as follows:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
lang="en"
metal:use-macro="context/main_template/macros/master"
i18n:domain="plone">
<body>
<metal:content-core fill-slot="content-core">
Hello
</metal:content-core>
</body>
</html>
The configure.zcml snippet looks like this:
<browser:page
for="Products.CMFPlone.browser.interfaces.INavigationRoot"
name="homepage"
template="homepage.pt"
layer=".interfaces.IThemeSpecific"
permission="zope2.View"
/>
I tried:
<tal:block tal:replace="structure here/carousel/##banner-base" />
but get an AttributeError for banner-base.
The correct context cannot be found, if a default-page is set to the folder or siteroot, like "front-page" is by default.
To resolve that, change its view to yours or any other view, which isn't a content-item, like 'Summary view' f.e.
The view has to be satisfy the is_view_template criterion as enforced by the update() method of the Carousel viewlet.
To do this simply use the setLayout() of the folder.

Tomahawk : t:outputText not working

Am using JSF 1.2 and am trying to implement Captcha as per this link
So, my first objective is to get "Hello world" text using t:outputText to check if Tomahawk is working fine or not. My JSF 1.2 based Servlet deploys fine in Jboss AS 5.1.0. But, when I hit my Servlet, nothing is getting displayed on the page. Nor there are any Exceptions in the logs.
Below is my xhtml page code
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:t="http://myfaces.apache.org/tomahawk">
<ui:composition>
<head>
<title>CAPTCHA</title>
</head>
<body>
<h:form id="captchatest">
<t:outputText value="This text is coming from tomahawk"></t:outputText>
</h:form>
</body>
</ui:composition>
</html>
Am using below jars in WEB-INF/lib
batik-awt-util-1.6-1.jar,
batik-ext-1.6-1.jar,
batik-gui-util-1.6-1.jar,
batik-util-1.6-1.jar,
commons-beanutils.jar,
commons-codec-1.3.jar,
commons-collections.jar,
commons-digester.jar,
commons-el-1.0.jar,
commons-fileupload-1.2.1.jar,
commons-io-1.3.2.jar,
commons-logging.jar,
commons-validator.jar,
el-ri.jar,
itext-1.4.8.jar,
jsf-api-1.2_13.jar,
jsf-facelets-1.1.15.B1.jar,
jsf-impl-1.2_13.jar,
myfaces.jar,
oro-2.0.8.jar,
standard-1.1.2.jar,
tomahawk-1.1.6.jar
I have below entry in faces-config.xml
<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
<locale-config />
</application>
When I view the page source in Browser, I just get whatever contents are there in my xhtml page instead of Tomahawk tags (t:outputText) getting parsed to HTML code.
Why am I not able to print the value of t:outputText in Browser?
Regards,
I solved it by removing these jars
el-ri.jar, oro-2.0.8.jar and standard-1.1.2.jar
And I added this in xhtml page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

Changing the ASP.NET XHTML Rendering Mode

I'm using html provided by a designer to create a master page.
The html doctype is set to be HTML 4.0 Strict. The meta tags in the html do not have closing end tags (they end with > rather than />) and this html is compliant using the W3 validator tool.
For example:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
However, ASP.NET changes the head attributes before rendering the page and modifies the end tags, as below
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Now the addition of the /> at the end of the tag causes errors in the W3 validator.
How do I prevent this from happening, and ensure the html rendered is as the designers intended?
You can change how ASP.NET renders HTML by forcing it to use a particular XHTML conformance rendering mode. I'm not sure what other side effects changing the renderer may have on the HTML output produced by ASP.NET. This can be set in the web.config with the following.
<system.web>
<!-- other elements here -->
<xhtmlConformance
mode="Legacy" />
</system.web>
There are other caveats to using the "Legacy" rendering mode, see the following MSDN resources for other rendering modes and details.
XHTML Standards in Visual Studio and ASP.NET
How to: Configure XHTML Rendering in ASP.NET Web Sites

Resources