the name of the driver class for the datasource is missing - jsf

From How to set up a JDBC Connection Pool on Glassfish I would like to connect MyQueue to the Birds JDBC resource in Glassfish. I'm getting an error similar to: The name of the driver class for the datasource is missing; the only difference being that I'm using MySQl instead of PostgreSql:
birds resource:
successful ping:
(the ping does indicate that the connection properties are correct?)
facelets:
<!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">
<h:head></h:head>
<h:body>
This and everything before will be ignored
<ui:composition template="template.xhtml">
<ui:define name="navigation">
<ui:include src="menu.xhtml"/>
</ui:define>
<ui:define name="main">
<h1>bird</h1>
#{myQueue.next}
</ui:define>
</ui:composition>
This and everything after will be ignored
</h:body>
</html>
and the bean:
package dur;
import java.io.Serializable;
import java.util.logging.Logger;
import javax.inject.Named;
import javax.ejb.Singleton;
import javax.enterprise.context.ApplicationScoped;
//import javax.inject.Singleton;
#Named
#ApplicationScoped
#Singleton
public class MyQueue implements Serializable {
private static final long serialVersionUID = 403250971215465050L;
private final Logger log = Logger.getLogger(MyQueue.class.getName());
private int next = 1;
public MyQueue() {
}
public int getNext() {
log.info("next\t" + next);
return next++;
}
}
The bean and facelet are functioning correctly, I just want to connect MyQueue to the database. I would like to use JPA to connect to the database.
The option to edit the field is grayed out.
--------------------------------------------edit---------------------------------
Netbeans did some magic and created the connection (I think) by right clicking the enterprise app and select new -> glassfish > jdbc connection pool.
sun-resources.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Resource Definitions //EN" "http://www.sun.com/software/appserver/dtds/sun-resources_1_3.dtd">
<resources>
<jdbc-resource enabled="true" jndi-name="jdbc/birdsPool" object-type="user" pool-name="birdsPool">
<description/>
</jdbc-resource>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="birdsPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.ConnectionPoolDataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="URL" value="jdbc:mysql://localhost:3306/legacy?zeroDateTimeBehavior=convertToNull"/>
<property name="User" value="user"/>
<property name="Password" value="gtjropjre"/>
</jdbc-connection-pool>
</resources>
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="EnterpriseBirdsJPA-warPU" transaction-type="JTA">
<jta-data-source>jdbc/birdsPool</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
I'm a bit uneasy with sun-resources.xml and would prefer to see that configuration directly on glassfish. I'm not exactly sure that this will work, but it allowed netbeans to then create an entity class from this connection, so that seems like progress. Again, though, that sun-resources.xml isn't actually in glassfish makes this a less than perfect solution.

I had a similar problem, it is basically netbeans problem (that is why everything seems to be fine in the glassfish admin console).
To solve:
1. add mysql-connector-java-5.1.23-bin.jar to "{$installation_folder}\NetBeans 8.0.1\ide\modules\ext"
2. restart netbeans
now it should work as expected
this bug report is also relevant

To solve the same issue you had, I had to add a "driverClass" additional properties in your connection pool (beside "password", "user", "URL").
Its value would be "com.mysql.jdbc.Driver".
Source : http://www.blogarama.com/programming-blogs/194794-wings-hermes-berins-infosec-blog/259237-glassfish-netbeans-name-driver-class-for-datasource-missing

Related

Does the component-type name needs to be unique for custom components?

I am new to Custom components topic starting with JSF 2.2
However, I am first considering the case with JSF2.0
Consider the snippet:
#FacesComponent(value = "components.WelcomeComponent1", createTag = true)
public class WelcomeComponent extends UIComponentBase {
}
Do notice value = "components.WelcomeComponent1"
referencing it in the UI wireframe-
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:t="http://xmlns.jcp.org/jsf/component">
<h:head>
<title></title>
</h:head>
<h:body>
<t:welcomeComponent value="Welcome" to="Rafael Nadal"/>
</h:body>
</html>
Consider the second case with the same name of the class as in first case but in a different package with a different component-type name:
#FacesComponent("components.WelcomeComponent")
public class WelcomeComponent extends UIComponentBase {
// code goes here
}
Do notice the "components.WelcomeComponent"
taglib.xml in WEB_INF
<namespace>http://atp.welcome.org/welcome</namespace>
<tag>
<tag-name>welcome</tag-name>
<component>
<component-type>components.WelcomeComponent</component-type>
</component>
<attribute>
<name>id</name>
<type>java.lang.String</type>
<description>Component id</description>
<required>false</required>
</attribute>
</tag>
</facelet-taglib>
and referencing the latter -
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:t="http://atp.welcome.org/welcome">
<h:head>
<title></title>
</h:head>
<h:body>
<t:welcome value="Welcome" to=""/>
</h:body>
</html>
Both of the above examples are part of the same web app & work fine.
However if I change the component-type name in the second case to make it the same as in first case,
#FacesComponent("components.WelcomeComponent1")
I do get-
Warning: This page calls for XML namespace
http://xmlns.jcp.org/jsf/component declared with prefix t but no
taglibrary exists for that namespace
Clearly the entry/tag declared in taglib.xml is preferred.
So, the component-type name needs to be unique in each case. Right?
I know it very well that the namespace http://xmlns.jcp.org/jsf/component was introduced with JSF2.2.
Short answer is yes, it should be unique. JSF thinks of it as a name for your custom component (in the similar way as a #ManagedBean). Basically, component-type allows Application instance to create new instances of UIComponent subclasses (your and every other custom component) by using defined component-type and createComponent() method.

EL in JSF-maven project not working

i'm trying to get my JSF web application to run on JBoss. Til now everthing worked fine, but i can't call methods of my backing bean.
Backing bean:
#Named
#ConversationScoped
public class WelcomePM implements Serializable {
public String getHelloStatement() {
return "Backing bean works!";
}
}
When i tried to invoke the method in a xhtml-page, nothing happened.
Page:
<?xml version="1.0" encoding="UTF-8"?>
<!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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<h:outputText value="JSF works!"></h:outputText>
<h:outputText value="#{welcomePM.getHelloStatement()}" />
</h:body>
</html>
The browser only shows the string 'JSF works!' but not the string 'Backing bean works!' as i expected. So i tried to exchange #{welcomePM.getHelloStatement()} with something the server can't resolve like #{fooPM.getBar()} expecting to get an exception. But there was no exception at all (neither in the browser nor in the server logs). So i believe, that the server doesn't even try to resolve the EL-expression. What am i doing wrong?
Have you tried #{welcomePM.helloStatement} ?

JSF-ViewScope bean reinstantiated in every request

I have seen this bug described in many places but always the causes are different. Post like this one states that the problem of re-instantiation only occurs, when you include a tag handler library on your page. However, I have an empty project with a page like the following
<?xml version="1.0" encoding="UTF-8"?>
<!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">
<head>
<title></title>
</head>
<body >
<h:form>
<f:view >
<h:commandButton id="otxMainPanelTextBt" value="click"
action="#{otherController.doSome}"/>
</f:view>
</h:form>
</body>
</html>
With the backing bean like this
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean(name = "otherController")
#ViewScoped
public class Other implements Serializable {
private static final long serialVersionUID = -6493758917750576706L;
public String doSome() {
System.out.println(this);
return "";
}
}
And the following dependences
<dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-impl</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-api</artifactId>
<version>2.0.2</version>
</dependency>
and every time I click on the button a different object is created. Apparently this page is as simple as possible and I have not triggered any of the possible causes of the bugs, so It always happen or am I missing something?
I tested changing the dependencies to 2.2.0 and it works as expected but unfortunately due to project restrictions, I need to keep the version 2.0.2 of JSF.
Any help would be highly appreciated.
Thanks!!
Actually, I have found that the instance remain the same. The problems was that when clicking the button I always saw different hashCodes printed in the toString of the method like this.
constructing the instance
de.controller.Other#118ea91
de.controller.Other#1e8f930
de.controller.Other#1a38f3c
and this led me to think there were different instances.
Although is the same instance, i believe this behaviour is incorrect because the hashCode of an object is not supposed to change during its lifetime.

simple JSF commandButton not hitting the action

I have the simplest little JSF example (JSF2 with GlassFish) and I can't figure out why the command button is not hitting the action method. This is what I have ... when I click the button, nothing happens.
What am I doing wrong?
testForm.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:form>
<h:messages />
<p/>
<h:inputText />
<p/>
<h:commandButton value="test1" action="#{testController.action1}" />
</h:form>
</html>
faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<managed-bean>
<managed-bean-name>testController</managed-bean-name>
<managed-bean-class>com.app.controller.TestController</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
TestController.java
package com.app.controller;
public class TestController {
public String action1() {
return "testPage2";
}
}
Eureka! After rebuilding the Eclipse project from scratch I realize what I did wrong. Apache MyFaces is in the project path and the app is being deployed on GlassFish which has it's own JSF implementation. The two JSF implementations don't want to play nicely together.
What a pain. And, you know, I made this exact same mistake once before. Eclipse should warn you about this or there should be some error reported in the GlassFish log or the h:messages tag.
1)For JSF2.0, Its not required to configure managed bean in Facesconfig.xml.
2)can use #managedban annotation.
package com.app.controller;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean(name = "testController")
#SessionScoped
public class TestController {
public String action1() {
return "testPage2";
}
/** Constructor, getters and setters*/
}

Private/scoped variable in JSF2/Facelets <ui:component>?

I might not be thinking correctly in terms of visual components in JSF, but I guess that's part of my question. My question is around the seeming lack of scope around variables declared within JSF <ui:component> implementations.
So, say I have /resources/comp/myPanel.xhtml:
<?xml version="1.0" encoding="UTF-8" ?>
<ui:component 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:c="http://java.sun.com/jsp/jstl/core"
xmlns:cc="http://java.sun.com/jsf/composite">
<cc:interface>
</cc:interface>
<cc:implementation>
<f:loadBundle var="bundle" basename="panelOnly.bundle" />
<h:outputText value="#{bundle.myText}" />
</cc:implementation>
</ui:component>
And there is a resource bundle that gets loaded in that component, panelOnly/bundle.properties:
myText = This is a panel resource
And then I have a page that places the myPanel component, mainPage.xhtml:
<?xml version="1.0" encoding="UTF-8" ?>
<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:comp="http://java.sun.com/jsf/composite/comp">
<h:body>
<f:view>
<f:loadBundle basename="mainPage.bundle" var="bundle" />
<comp:myPanel />
<h:outputText value="#{bundle.myText}" />
</f:view>
</h:body>
</html>
and there is a resource bundle that gets loaded in the main page, mainPage/bundle.properties:
myText = This is a main page resource
Now, I would assume that my page should render as:
This is a panel resource
This is a main page resource
But, instead, I get:
This is a panel resource
This is a panel resource
And I assume that is because I clobbered what the "bundle" symbol refers to in my component so that when the mainPage.xhtml tries to resolve that value, it looks to the component's "bundle" object and not the original mainPage's.
My workaround to date has been to just use unique named variables within my components that would never clash with variables on my main pages. But I would prefer if there was a way to coax JSF to recognize anything declared in my component as locally scoped variables and not clobber the caller's symbols.
I think there are and other tags that one can use to make locally scoped variables under #{cc.attrs...}. If you could enumerate my local scoping options in your answer, that would be very helpful. I suspect my <f:loadBundle> is a special case, and maybe there isn't a workaround for that one as it was not designed with <ui:component> in mind.
Thanks!
P.S. I'm running Mojarra 2.1.1 (FCS 20110408)
(edited for formatting and copy and paste bugs 6/15/2011)
Unfortunately, that's how <f:loadBundle> works. It's an one-time setting for the entire view. And any subsequent <f:loadBundle> calls in the same view will just override the previous one.
Your best bet is to manage it by a backing component.
<cc:interface componentType="myPanel">
with
#FacesComponent(value="myPanel")
public class MyPanel extends UIComponentBase implements NamingContainer {
private ResourceBundle bundle;
public MyPanel() {
bundle = ResourceBundle.getBundle("panelOnly.bundle",
FacesContext.getCurrentInstance().getViewRoot().getLocale());
}
#Override
public String getFamily() {
return "javax.faces.NamingContainer";
}
public ResourceBundle getBundle() {
return bundle;
}
}
which can be used as
<cc:implementation>
<h:outputText value="#{cc.bundle.myText}" />
</cc:implementation>

Resources