gwt-2.8.2下载
大家好,
在本文中,我们将演示如何正确集成GWT和EJB3 ,以实现示例项目,使用maven进行构建并将其部署在JBoss 5.1应用服务器上。 实际上,您可以轻松地更改maven构建文件中的依赖关系,并将项目部署到您喜欢的应用程序服务器中。 为了尽可能通用,我们将使用命令行风格的maven和gedit文本编辑器。
首先,我们必须创建pom项目。 我们将其称为ejb-gwt。
mkdir ejb-gwt
cd ejb-gwt
gedit pom.xml
pom.xml
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven4.0.0.xsd"><modelversion>4.0.0</modelversion><groupid>com.javacodegeeks</groupid><artifactid>ejb-gwt</artifactid><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><name>ejb-gwt</name><url>http://maven.apache.org</url><properties><project.build.sourceencoding>UTF-8</project.build.sourceencoding></properties><dependencies></dependencies><modules></modules><build><plugins><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-compiler-plugin</artifactid><configuration>1.6<target>1.6</target></configuration></plugin></plugins></build>
</project>
然后,我们将创建包含ejb类的模块。 我们将其称为ejb-jar。
mvn archetype:create -DgroupId=com.javacodegeeks -DartifactId=ejb-jar
cd ejb-jar/src/main
mkdir resources
cd resources
mkdir META-INF
cd META-INF
gedit persistence.xml
我们将配置persistence.xml以使用jboss的默认数据源。
persistence.xml
<persistence version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/persistence" xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"><persistence-unit name="MyPersistenceUnit"><provider>org.hibernate.ejb.HibernatePersistence</provider><jta-data-source>java:/DefaultDS</jta-data-source><properties><property name="hibernate.hbm2ddl.auto" value="update"><property name="hibernate.show_sql" value="true"><property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"></property></property></property></properties></persistence-unit>
</persistence>
在src / java中,您将放置您的ejb代码。 在我们的示例中,我们有一个实体bean
Employee.java
package com.javacodegeeks.ejb.entity;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;@Entity
@Table(name = "EMPLOYEE")
public class Employee implements java.io.Serializable {private static final long serialVersionUID = 7440297955003302414L;@Id@Column(name="employee_id")private long employeeId;@Column(name="employee_name", nullable = false, length=30)private String employeeName;@Column(name="employee_surname", nullable = false, length=30)private String employeeSurname;@Column(name="job", length=50)private String job;public Employee() {}public Employee(int employeeId) {this.employeeId = employeeId; }public Employee(long employeeId, String employeeName, String employeeSurname,String job) {this.employeeId = employeeId;this.employeeName = employeeName;this.employeeSurname = employeeSurname;this.job = job;}public long getEmployeeId() {return employeeId;}public void setEmployeeId(long employeeId) {this.employeeId = employeeId;}public String getEmployeeName() {return employeeName;}public void setEmployeeName(String employeeName) {this.employeeName = employeeName;}public String getEmployeeSurname() {return employeeSurname;}public void setEmployeeSurname(String employeeSurname) {this.employeeSurname = employeeSurname;}public String getJob() {return job;}public void setJob(String job) {this.job = job;}
}
会话bean的本地接口。
EmployeeService.java
package com.javacodegeeks.ejb.session;import com.javacodegeeks.ejb.entity.Employee;public interface EmployeeService {public Employee findEmployee(long employeeId);public void saveEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception;public void updateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception;public void saveOrUpdateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception;public void deleteEmployee(long employeeId) throws Exception;
}
最后确定会话bean。
EmployeeServiceBean.java
package com.javacodegeeks.ejb.session;import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;import com.javacodegeeks.ejb.entity.Employee;@Stateless(name = "employeeService")
@Local(EmployeeService.class)
public class EmployeeServiceBean implements EmployeeService {@PersistenceContextprivate EntityManager entityManager;public void deleteEmployee(long employeeId) throws Exception {entityManager.remove(entityManager.find(Employee.class, employeeId));}public Employee findEmployee(long employeeId) {return entityManager.find(Employee.class, employeeId);}public void saveEmployee(long employeeId, String name, String surname,String jobDescription) throws Exception {Employee emp = new Employee();emp.setEmployeeId(employeeId);emp.setEmployeeName(name);emp.setEmployeeSurname(surname);emp.setJob(jobDescription);entityManager.persist(emp);}public void saveOrUpdateEmployee(long employeeId, String name,String surname, String jobDescription) throws Exception {Employee emp = new Employee();emp.setEmployeeId(employeeId);emp.setEmployeeName(name);emp.setEmployeeSurname(surname);emp.setJob(jobDescription);entityManager.merge(emp);}public void updateEmployee(long employeeId, String name, String surname,String jobDescription) throws Exception {Employee emp = entityManager.find(Employee.class, employeeId);emp.setEmployeeName(name);emp.setEmployeeSurname(surname);emp.setJob(jobDescription);entityManager.merge(emp);}}
ejb-jar模块的pom.xml应该是:
pom.xml
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelversion>4.0.0</modelversion><parent><artifactid>ejb-gwt</artifactid><groupid>com.javacodegeeks</groupid><version>0.0.1-SNAPSHOT</version></parent><groupid>com.javacodegeeks</groupid><artifactid>ejb-jar</artifactid><version>0.0.1-SNAPSHOT</version><name>ejb-jar</name><packaging>ejb</packaging><url>http://maven.apache.org</url><repositories><repository><id>jboss-maven2</id><url>http://repository.jboss.com/maven2</url></repository></repositories><dependencies><dependency><groupid>org.jboss.jbossas</groupid><artifactid>jboss-as-client</artifactid><version>5.1.0.GA</version><type>pom</type><scope>provided</scope></dependency><dependency><groupid>junit</groupid><artifactid>junit</artifactid><version>4.7</version><scope>test</scope></dependency> </dependencies><build><plugins><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-ejb-plugin</artifactid><configuration><ejbversion>3.0</ejbversion></configuration></plugin></plugins>
</build>
</project>
现在,我们将基于gwt-maven-plugin原型生成我们的Web项目。
mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo -DarchetypeArtifactId=gwt-maven-plugin -DarchetypeVersion=1.2 -DgroupId=com.javacodegeeks -DartifactId=war
cd war
gedit pom.xml
pom.xml
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelversion>4.0.0</modelversion><parent><artifactid>ejb-gwt</artifactid><groupid>com.javacodegeeks</groupid><version>0.0.1-SNAPSHOT</version></parent><groupid>com.javacodegeeks</groupid><artifactid>war</artifactid><packaging>war</packaging><version>0.0.1-SNAPSHOT</version> <properties><gwt.version>2.0.4</gwt.version><maven.compiler.source>1.6</maven.compiler.source><maven.compiler.target>1.6</maven.compiler.target></properties><dependencies><dependency><groupid>com.google.gwt</groupid><artifactid>gwt-servlet</artifactid><version>${gwt.version}</version><scope>runtime</scope></dependency><dependency><groupid>com.google.gwt</groupid><artifactid>gwt-user</artifactid><version>${gwt.version}</version> <scope>provided</scope></dependency><dependency><groupid>org.jboss.jbossas</groupid><artifactid>jboss-as-client</artifactid><version>5.1.0.GA</version><type>pom</type><scope>provided</scope></dependency><dependency><groupid>com.javacodegeeks</groupid><artifactid>ejb-jar</artifactid><version>0.0.1-SNAPSHOT</version><type>ejb</type><scope>provided</scope></dependency><dependency><groupid>junit</groupid><artifactid>junit</artifactid><version>4.7</version><scope>test</scope></dependency></dependencies><build><finalname>war</finalname><outputdirectory>war/WEB-INF/classes</outputdirectory><plugins><plugin><groupid>org.codehaus.mojo</groupid><artifactid>gwt-maven-plugin</artifactid><version>1.2</version><executions><execution><goals><goal>compile</goal><goal>generateAsync</goal><goal>test</goal></goals></execution></executions><configuration><runtarget>com.javacodegeeks.war.Application/Application.html</runtarget></configuration></plugin><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-compiler-plugin</artifactid><version>2.0.2</version><configuration>${maven.compiler.source}<target>${maven.compiler.target}</target></configuration></plugin></plugins></build></project>
cd src/main/webapp/WEB-INF
gedit web.xml
web.xml
<web-app version="2.5" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name>GWT-Maven-Archetype</display-name> <servlet><servlet-name>emp</servlet-name><servlet-class>com.javacodegeeks.war.server.services.EmployeeServiceGWTImpl</servlet-class></servlet><servlet-mapping><servlet-name>emp</servlet-name><url-pattern>/com.javacodegeeks.war.Application/emp</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list>
</web-app>
如果我们想在servlet容器中进行ejb注入,则web.xml应该适用于2.5规范。
现在,让我们看一下gwt服务。
EmployeeServiceGWT.java
package com.javacodegeeks.war.shared.services;import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import com.javacodegeeks.war.shared.entity.EmployeeUtil;@RemoteServiceRelativePath("emp")
public interface EmployeeServiceGWT extends RemoteService {public EmployeeUtil findEmployee(long employeeId);public void saveEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception;public void updateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception;public void saveOrUpdateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception;public void deleteEmployee(long employeeId) throws Exception;
}
注意,RemoteServiceRelativePath与我们在web.xml中定义的servlet路径/com.javacodegeeks.war.Application/emp相关。
EmployeeServiceGWTImpl.java
package com.javacodegeeks.war.server.services;import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.javacodegeeks.ejb.entity.Employee;
import com.javacodegeeks.ejb.session.EmployeeService;
import com.javacodegeeks.war.shared.entity.EmployeeUtil;
import com.javacodegeeks.war.shared.services.EmployeeServiceGWT;public class EmployeeServiceGWTImpl extends RemoteServiceServlet implements EmployeeServiceGWT{/*** */private static final long serialVersionUID = 5995064321382986251L;private EmployeeService employeeService;@Resource(name="java:UserTransaction") UserTransaction ut;@EJBpublic void setEmployeeService(EmployeeService employeeService) {this.employeeService = employeeService;}public void deleteEmployee(long employeeId) throws Exception {employeeService.deleteEmployee(employeeId);}public EmployeeUtil findEmployee(long employeeId) {Employee emp = employeeService.findEmployee(employeeId);if(emp != null) {try {ut.begin();EmployeeUtil util = new EmployeeUtil(emp.getEmployeeId(), emp.getEmployeeName(), emp.getEmployeeSurname(), emp.getJob());ut.commit();return util;} catch (Exception e) {}}return null;}public void saveEmployee(long employeeId, String name, String surname,String jobDescription) throws Exception {employeeService.saveEmployee(employeeId, name, surname, jobDescription);}public void saveOrUpdateEmployee(long employeeId, String name,String surname, String jobDescription) throws Exception {employeeService.saveOrUpdateEmployee(employeeId, name, surname, jobDescription);}public void updateEmployee(long employeeId, String name, String surname,String jobDescription) throws Exception {employeeService.updateEmployee(employeeId, name, surname, jobDescription);}}
如您所见,我们使用注解@EJB注入employeeService会话bean。
请注意,出于基本原因,我们使用EmployeeUtil类 :
GWT无法处理懒惰的初始化关系,因此我们将代理集合替换为实际集合。该替换应在事务范围内。
package com.javacodegeeks.war.shared.entity;import java.io.Serializable;public class EmployeeUtil implements Serializable {/*** */private static final long serialVersionUID = -2732740011239267035L;private long employeeId;private String employeeName;private String employeeSurname;private String job;public EmployeeUtil() {}public EmployeeUtil(int employeeId) {this.employeeId = employeeId; }public EmployeeUtil(long employeeId, String employeeName, String employeeSurname,String job) {this.employeeId = employeeId;this.employeeName = employeeName;this.employeeSurname = employeeSurname;this.job = job;}public long getEmployeeId() {return employeeId;}public void setEmployeeId(long employeeId) {this.employeeId = employeeId;}public String getEmployeeName() {return employeeName;}public void setEmployeeName(String employeeName) {this.employeeName = employeeName;}public String getEmployeeSurname() {return employeeSurname;}public void setEmployeeSurname(String employeeSurname) {this.employeeSurname = employeeSurname;}public String getJob() {return job;}public void setJob(String job) {this.job = job;}}
本文的源代码中提供了gwt接口的其余源代码。
现在,我们已迈出最后一步,要建立耳朵。我们将创建一个模块来做到这一点。
mvn archetype:create -DgroupId=com.javacodegeeks -DartifactId=ear
cd ear
rm -rf src
gedit pom.xml
pom.xml
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelversion>4.0.0</modelversion><parent><artifactid>ejb-gwt</artifactid><groupid>com.javacodegeeks</groupid><version>0.0.1-SNAPSHOT</version></parent><groupid>com.javacodegeeks</groupid><artifactid>ear</artifactid><version>0.0.1-SNAPSHOT</version><packaging>ear</packaging><name>ear</name><description>ear for gwt ejb3</description><dependencies><dependency><groupid>com.javacodegeeks</groupid><artifactid>ejb-jar</artifactid><version>0.0.1-SNAPSHOT</version><type>ejb</type></dependency><dependency><groupid>com.javacodegeeks</groupid><artifactid>war</artifactid><version>0.0.1-SNAPSHOT</version><type>war</type></dependency></dependencies><build><plugins><plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-ear-plugin</artifactid><configuration><generateapplicationxml>true</generateapplicationxml><modules><webmodule><groupid>com.javacodegeeks</groupid><artifactid>war</artifactid><contextroot>ejb-gwt</contextroot></webmodule><ejbmodule><groupid>com.javacodegeeks</groupid><artifactid>ejb-jar</artifactid></ejbmodule></modules></configuration></plugin></plugins></build>
</project>
要构建项目,请转到根项目并输入:
mvn安装
本文的源代码位于此处 。
谢谢
。/拍
相关文章 :
- GWT 2 Spring 3 JPA 2 Hibernate 3.5教程
- GWT Spring和Hibernate进入数据网格世界
- Spring 3 RESTful Web服务
- GWT 2 Spring 3 JPA 2 Hibernate 3.5教程– Eclipse和Maven 2展示
- 使用Spring AspectJ和Maven进行面向方面的编程
- 具有Spring和Maven教程的JAX–WS
翻译自: https://www.javacodegeeks.com/2010/09/gwt-ejb3-maven-jboss-51-integration.html
gwt-2.8.2下载