-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello,
I am a completely newbe to JAVA-Technologie, and i want to create a sample Application using ZK, Hibernate and MySQL database.
I setted up ZK in Eclipse and I got it to work with Tomcat Application Server. ZK Hello-World Applications are running into the Eclipse-Environment using Tomcat.
Then i integrated Hibernate. I designed the following Entity-Class for experimental:
----------------------------------------------------------------
package entities;
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
public User() {
super();
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private long id;
@Column(length=50,nullable=false)
private String name;
@Column(length=30,nullable=false)
private String email;
@Column(length=30,nullable=false)
private String password;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
-------------------------------------------------------------------------
To test this entity-Class i wrote a Test-Class that's working:
-------------------------------------------------------------------------
package entities.test;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import entities.User;
public class UserTest {
public static void main(String args[]) {
User myUser;
myUser = new User();
//myUser.setId(1);
myUser.setName("TEST");
myUser.setPassword("test123");
myUser.setEmail("[email protected]");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA Hibernate");
EntityManager manager = emf.createEntityManager();
EntityTransaction tx = manager.getTransaction();
try {
tx.begin();
manager.persist(myUser);
tx.commit();
emf.close();
} catch(RuntimeException exc) {
try {
exc.printStackTrace();
tx.rollback();
} catch (RuntimeException exRb) {
System.out.println("Rollback nicht möglich");
}
throw exc;
}
finally {
manager.close();
}
}
}
-------------------------------------------------------------------------
Now i want to put ZK and Hibernate together. Therefore i wrote a "DAO-Class" to be called in ZK-Script:
-------------------------------------------------------------------------
package entities.dao;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import entities.User;
public class UserDaoImpl {
public UserDaoImpl() {
super();
// TODO Auto-generated constructor stub
}
public void test() {
User myUser;
myUser = new User();
//myUser.setId(1);
myUser.setName("TEST");
myUser.setPassword("test123");
myUser.setEmail("[email protected]");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA Hibernate");
EntityManager manager = emf.createEntityManager();
EntityTransaction tx = manager.getTransaction();
try {
tx.begin();
manager.persist(myUser);
tx.commit();
emf.close();
} catch(RuntimeException exc) {
try {
exc.printStackTrace();
tx.rollback();
} catch (RuntimeException exRb) {
System.out.println("Rollback nicht möglich");
}
throw exc;
}
finally {
manager.close();
}
}
}
----------------------------------------------------------------------
To test this i wrote a simple ZUL-File userRegistration.zul:
----------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<p:zk xmlns:p="http://www.zkoss.org/2005/zul" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd ">
<?variable-resolver class="org.zkoss.zkplus.jndi.JndiVariableResolver" ?>
<p:zscript>
userDaoLocalBean.test();
</p:zscript>
<p:window title="Benutzer registrieren" border="normal" width="500px" sizable="false">
<grid width="90%">
<rows>
<row>
Name: <textbox />
</row>
<row>
E-Mail: <textbox />
</row>
<row>
Password: <textbox />
</row>
<row>
<p:button label="Registrieren"></p:button>
</row>
</rows>
</grid>
</p:window>
</p:zk>
------------------------------------------------------------------------
According to http://www.zkoss.org/smalltalks/jndi/ i tried to register this class in web.xml. I added the following to inside the <web-app>-Tag:
--------------------------------------
<ejb-local-ref>
<ejb-ref-name>userDaoLocalBean</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local-home>entities.dao.UserDaoImpl</local-home>
<local>entities.dao.UserDaoImpl</local>
</ejb-local-ref>
-------------------------------------
I really didn't understand EJB at all, i in the ZUL-File
<?variable-resolver class="org.zkoss.zkplus.jndi.JndiVariableResolver" ?>
is not correct. Isn't it?
Is it, that i have to pass arg[0] and arg[1] correctly as described in http://www.zkoss.org/smalltalks/jndi/? But how?
This is, what i got, when i run this ZUL-Script on the Server:
-----
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.zkoss.zk.ui.UiException: Sourced file: inline evaluation of: ``userDaoLocalBean.test();'' : Attempt to resolve method: test() on undefined variable or class name: userDaoLocalBean : at Line: 1 : in file: inline evaluation of: ``userDaoLocalBean.test();'' : userDaoLocalBean .test ( )
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
java.lang.reflect.Constructor.newInstance(Constructor.java:513)
org.zkoss.lang.Classes.newInstance(Classes.java:78)
org.zkoss.lang.Exceptions.wrap(Exceptions.java:164)
org.zkoss.zk.ui.UiException$Aide.wrap(UiException.java:46)
org.zkoss.zk.scripting.bsh.BSHInterpreter.exec(BSHInterpreter.java:103)
org.zkoss.zk.scripting.util.GenericInterpreter.interpret(GenericInterpreter.java:292)
org.zkoss.zk.ui.impl.PageImpl.interpret(PageImpl.java:866)
org.zkoss.zk.ui.impl.UiEngineImpl.execNonComponent(UiEngineImpl.java:608)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:503)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:465)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:512)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:481)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:465)
org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:359)
org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:292)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:227)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:167)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
Sourced file: inline evaluation of: ``userDaoLocalBean.test();'' : Attempt to resolve method: test() on undefined variable or class name: userDaoLocalBean : at Line: 1 : in file: inline evaluation of: ``userDaoLocalBean.test();'' : userDaoLocalBean .test ( )
bsh.UtilEvalError.toEvalError(Unknown Source)
bsh.UtilEvalError.toEvalError(Unknown Source)
bsh.BSHMethodInvocation.eval(Unknown Source)
bsh.BSHPrimaryExpression.eval(Unknown Source)
bsh.BSHPrimaryExpression.eval(Unknown Source)
bsh.Interpreter.eval(Unknown Source)
bsh.Interpreter.eval(Unknown Source)
org.zkoss.zk.scripting.bsh.BSHInterpreter.exec(BSHInterpreter.java:100)
org.zkoss.zk.scripting.util.GenericInterpreter.interpret(GenericInterpreter.java:292)
org.zkoss.zk.ui.impl.PageImpl.interpret(PageImpl.java:866)
org.zkoss.zk.ui.impl.UiEngineImpl.execNonComponent(UiEngineImpl.java:608)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:503)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:465)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:512)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:481)
org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:465)
org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:359)
org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:292)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:227)
org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:167)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.18 logs.
----
I hope me someone could help me getting started
Thanks and greetings.
Hi mh1:
http://www.zkoss.org/smalltalks/jndi/
This small talk is for user who familiar JBoss and want to use ZK with it.
In this case EJB and JBoss should be setup first.
http://www.laliluna.de/ejb-3-tutorial-jboss.html
Then declaring an EJB reference in jboss-web.xml.
Which is important here to solve resource reference like
"userDaoLocalBean.test();" in zul.
There is another example which using Spring instead of JBoss in our small talk.
http://www.zkoss.org/smalltalks/contact/contact.dsp
hope that this will help.
/davidchen
Asked: 2008-08-10 11:26:15 +0800
Seen: 403 times
Last updated: Aug 13 '08