JavaEE5 - EJB3

Document Sample
JavaEE5 - EJB3 Powered By Docstoc
					JavaEE 5 (JSF + JPA + EJB3) using Eclipse
http://gochev.blogspot.com/2009/10/javaee-5-jsf-jpa-ejb3-using-eclipse.html Posted by JOKe on 10/26/2009 05:56:00 PM in javaee Today I will show you how to create Enterprise Application using Java EE 5 and GlassFish. I will use - Eclipse 3.5 + WTP - GlassFish v. 2.1 - JSF Mojarra implementation. - EJB 3.0. - JPA Toplink essentials implementation. - MySQL No NetBeans or JDeveloper magic involved :) The prerequirement is: Add datasource in glassfish. Read this how you can make this here: http://gochev.blogspot.com/2009/10/creating-datasource-in-glassfish-v-21.html 1) First you need to add glassfish in your eclipse. - Go to Servers View - Right Click, New - Choose GlassFish v 2.1 if you dont have glassfish click on Download additional adapters link choose glassfish wait and restart eclipse. Than try again.

1

2

2) Create the database

CREATE TABLE `lesson`.`USERS` ( `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, `username` VARCHAR(45) NOT NULL, `password` VARCHAR(45) NOT NULL, `name` VARCHAR(45) NOT NULL, PRIMARY KEY (`id`) ) ENGINE = InnoDB;

3) Create EJB project

3

4

4) Add persistance.xml file in META-INF folder
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"> <persistence-unit name="toursUnit"> <jta-data-source>lessonMySQL</jta-data-source> <class>org.joke.model.User</class> </persistence-unit> </persistence>

5) Create Simple Entity org.joke.model.User
package org.joke.model; import import import import import import javax.persistence.Basic; javax.persistence.Entity; javax.persistence.GeneratedValue; javax.persistence.GenerationType; javax.persistence.Id; javax.persistence.Table;

@Entity @Table(name = "USERS") public class User { private private private private @Id Long id; String name; String username; String password;

5

@GeneratedValue(strategy = GenerationType.IDENTITY) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Basic public String getName() { return name; } public void setName(String name) { this.name = name; } @Basic public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Basic public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }

}

6) Create HelloService EJB that will use EntityManager - The local interface is:
package org.joke.service; import javax.ejb.Local; import org.joke.model.User; @Local public interface HelloService { public User login(String username,String password); }

-The implementation is:
package org.joke.service; import java.util.List;

6

import import import import

javax.ejb.Stateless; javax.persistence.EntityManager; javax.persistence.PersistenceContext; javax.persistence.Query;

import org.joke.model.User; /** * Session Bean implementation class HelloServiceImpl */ @Stateless public class HelloServiceImpl implements HelloService { @PersistenceContext(unitName = "toursUnit") EntityManager em; public HelloServiceImpl() { } @Override public User login(String username, String password) { Query query = em .createQuery("select u from User u where u.username = :username and u.password = :password"); query.setParameter("username", username); query.setParameter("password", password); List<User> result = query.getResultList(); if (result != null && result.size() > 0) { return result.get(0); //first user } return null; } }

7) Create Dynamic Web Project

7

8

8) Add JSF to this Dynamic Web Project - Edit web.xml and add FacesServlet
<servlet> <display-name>FacesServlet</display-name> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping>

9) I will use a very simple login form (login.jsp) and home.jsp where you will go if login is successfull.
<?xml version="1.0" encoding="UTF-8" ?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" version="2.1"> <jsp:directive.page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" /> <jsp:text> <![CDATA[ <?xml version="1.0" encoding="UTF-8" ?> ]]> </jsp:text> <jsp:text> <![CDATA[ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ]]> </jsp:text> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Hello JSF Page</title> </head> <body> <f:view>

9

<h:messages style="color:red;"></h:messages> <h:form id="loginForm"> <table> <tr> <td align="right"><h:outputText value="Username: " /></td> <td><h:inputText id="username" value="#{userBean.currentUser.username}" required="true"> <f:validateLength maximum="10" minimum="3" /> </h:inputText></td> <td><h:message for="username" style="color:red;" /></td> </tr> <tr> <td align="right"><h:outputText value="Password: " /></td> <td><h:inputSecret id="password" value="#{userBean.currentUser.password}" required="true" /></td> <td><h:message for="password" style="color:red;" /></td> </tr> <tr> <td colspan="3"><h:commandButton value="login" action="#{userBean.login}" /></td> </tr> </table> </h:form> </f:view> </body> </html> </jsp:root>

- The home.jsp looks like this:
<?xml version="1.0" encoding="UTF-8" ?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" version="2.1"> <jsp:directive.page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" /> <jsp:text> <![CDATA[ <?xml version="1.0" encoding="UTF-8" ?> ]]> </jsp:text> <jsp:text> <![CDATA[ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ]]> </jsp:text> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Hello JSF Page</title> </head> <body> <f:view> Welcome user </f:view> </body> </html> </jsp:root>

10

- and faces-config.xml looks like this:
<?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_1_2.xsd" version="1.2"> <navigation-rule> <from-view-id>/login.jsp</from-view-id> <navigation-case> <from-outcome>success</from-outcome> <to-view-id>/home.jsp</to-view-id> </navigation-case> </navigation-rule> <managed-bean> <managed-bean-name>userBean</managed-bean-name> <managed-bean-class>mbeans.UserBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> </faces-config>

-The UserBean source is:
package mbeans; import javax.ejb.EJB; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import org.joke.model.User; import org.joke.service.HelloService; public class UserBean { private User currentUser = new User(); @EJB private HelloService helloService ; public void setCurrentUser(User currentUser) { this.currentUser = currentUser; } public User getCurrentUser() { return currentUser; } public String login() { User result = helloService.login(currentUser.getUsername(), currentUser.getPassword()); if (result!=null) { currentUser = result; return "success"; } FacesContext.getCurrentInstance().addMessage( null, new FacesMessage("username and password not found")); return null; }

11

}

10) You can notice that I use HelloService and User model class in the Dynamic web project. To do this you have to add the service project as referenced by the web project. - Right click on the web project and choose Properties

-Go to Build path/Projects

-Add the services project

12

-Now check the UserBean it should not contain any errors. 11) Create Enterprise Applicatin and add Web and EJB module to this application

13

12) Right click on the Enterprise Application and choose Run on server. 13) go to http://localhost:8080/HelloWeb/login.jsf and test it :)

14

That's all. Note that we did not include any third party jar files in this projects. This is because GlassFish include JSF 1.2 Mojarra implementation and Toplink JPA implementation. *Update : full source code of the example you can download from: http://dl.getdropbox.com/u/887821/HelloJavaEE5.rar

15


				
DOCUMENT INFO
Shared By:
Tags:
Stats:
views:235
posted:12/10/2009
language:English
pages:15