GWT 2 Spring 3 JPA 2 Hibernate 3.5教程

本分步指南将介绍如何使用开发一个简单的Web应用程序 Google的网络工具包 (GWT)用于富客户端,而Spring作为后端服务器端框架。 该示例Web应用程序将提供对数据库执行CRUD(创建检索更新删除)操作的功能。 对于数据访问层,我们将在Hibernate上使用JPA ,对于数据库,我们将使用Hypersonic 。 当然,您可以更改配置并使用所需的任何内容。 我们将Web应用程序部署到Apache – Tomcat实例。

我们首选的开发环境是Eclipse ,因此,必须先安装具有GWT支持的Eclipse 。 用于Eclipse的GWT插件的安装不在本教程的讨论范围内,因此将不予讨论。 但是,您将需要以下组件:

  1. 从这里 蚀
  2. 从这里开始 ,Eclipse的GWT插件
  3. 从这里发布Spring框架
  4. Hibernate持久性框架从这里发布
  5. 来自这里的 Hypersonic数据库
  6. 从这里开始 Apache Commons-Logging库
  7. 来自这里的 AOP Alliance (Java / J2EE AOP标准)库
  8. 从这里开始 SLF4J库
  9. 来自这里的 Apache log4j库
  10. 最后但并非最不重要的一点,从此处下载GWT – Spring“胶水”库spring4gwt

我们将使用Eclipse Galileo, GWT版本2.0.3, Spring版本3.0.1, Hibernate版本3.5.2, Hypersonic版本1.8.1.2, Apache Commons-Logging版本1.1.1, AOP Alliance (Java / J2EE AOP Standard)版本本教程的1.0, SLF4J 1.5.8版, Apache log4j 1.2.16版和spring4gwt 0.0.1版。

聊够了,让我们动手吧!

  1. 创建一个新的GWT项目,转到File? 新的Web应用程序项目
  2. 我们将项目命名为GWTSpring。 基本软件包将为com.javacodegeeks.gwtspring,也仅使用Google Web Toolkit,因此在向导窗口中取消选中“ Use Google App Engine ”。

让我们回顾一下有关GWT项目结构的一些事情

  1. / src文件夹包含应用程序的所有源文件
  • {package_name} .client子软件包包含仅适用于应用程序客户端的所有源文件
  • {package_name} .server子软件包包含仅对应用程序的服务器端部分可用的所有源文件
  • {package_name} .shared子包包含应用程序的客户端和服务器端均可使用的所有源文件
  • / test文件夹包含用于单元测试的所有源文件
  • / war文件夹包含用于创建有效的Web应用程序的基本文件
  • 为了在运行时正确地将Spring与GWT集成,我们必须为Web应用程序提供所有必需的库。 因此,复制下面在/ war / WEB-INF / lib下列出的文件(如果使用的是不同版本,请复制相关文件)

    从Spring发行

    • /dist/org.springframework.expression-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.beans-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.oxm-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.jms-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.jdbc-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.core-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.context-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.asm-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.aspects-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.transaction-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.context.support-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.aop-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.orm-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.instrument-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.instrument.tomcat-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.test-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.web-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.web.portlet-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.web.servlet-3.0.1.RELEASE-A.jar
    • /dist/org.springframework.web.struts-3.0.1.RELEASE-A.jar

    从Hibernate发行版

    • hibernate3.jar
    • /lib/required/antlr-2.7.6.jar
    • /lib/required/commons-collections-3.1.jar
    • /lib/required/dom4j-1.6.1.jar
    • /lib/required/javassist-3.9.0.GA.jar
    • /lib/required/jta-1.1.jar
    • /lib/required/slf4j-api-1.5.8.jar
    • /lib/jpa/hibernate-jpa-2.0-api-1.0.0.Final.jar
    • /lib/optional/c3p0/c3p0-0.9.1.jar

    从高超音速分布

    • /lib/hsqldb.jar

    从Apache Commons Logging发行版中

    • commons-logging-1.1.1.jar

    来自AOP联盟 (Java / J2EE AOP标准)发行版

    • aopalliance.jar

    从SLF4J发行版

    • slf4j-log4j12-1.5.8.jar

    从Apache log4j发行版

    • log4j-1.2.16.jar

    sping4gwt库

    • spring4gwt-0.0.1.jar

    现在,我们必须注意Eclipse项目的依赖性。 以下jar应包含在项目的Java构建路径中:

    • hibernate-jpa-2.0-api-1.0.0.Final.jar
    • org.springframework.beans-3.0.1.RELEASE-A.jar
    • org.springframework.context-3.0.1.RELEASE-A.jar
    • org.springframework.core-3.0.1.RELEASE-A.jar
    • org.springframework.orm-3.0.1.RELEASE-A.jar
    • org.springframework.transaction-3.0.1.RELEASE-A.jar

    下一步是为Web应用程序提供钩子,以便在启动时加载Spring上下文,并允许spring4gwt拦截客户端和服务器之间的RPC调用,并将其转换为Spring服务调用。

    在/ war / WEB-INF下找到web.xml文件,并添加以下内容:

    为了在启动时加载Spring上下文,

    <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    在Servlet部分,包括

    <servlet><servlet-name>springGwtRemoteServiceServlet</servlet-name><servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet</servlet-class>
    </servlet>

    在servlet映射部分,包括spring4gwt拦截RPC调用。

    <servlet-mapping><servlet-name>springGwtRemoteServiceServlet</servlet-name><url-pattern>/gwtspring/springGwtServices/*</url-pattern>
    </servlet-mapping>

    这里要注意的事情:

    1. 应该将springGwtRemoteServiceServlet servlet的servlet-mapping元素的url-pattern子元素更改为您的GWT模块名称,例如{module_name} / springGwtServices / *,该模块名称在{project_name} .gwt.xml文件中定义(此处为GWTSpring.gwt.xml)位于/ src文件夹下项目基本包的根目录
    2. 您可以将spring4gwt Servlet的名称(此处为springGwtRemoteServiceServlet)更改为任意名称

    要继续,我们必须创建persistence.xml文件,以描述使用JPA与数据库的连接。 pesistence.xml文件必须位于META-INF目录中,该目录又必须由Web应用程序在运行时(在类路径上)访问。 为了满足上述要求,我们必须在项目的/ war / WEB-INF / classes文件夹下创建META-INF文件夹。 为此,我们以“ resources”为名称创建一个新的source文件夹,并在其中创建META-INF文件夹。 最后,在/ resources / META-INF文件夹中创建persistence.xml文件。 下面提供了一个示例persistence.xml

    <persistence xmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"version="2.0"><persistence-unit name="MyPersistenceUnit" transaction-type="RESOURCE_LOCAL"><provider>org.hibernate.ejb.HibernatePersistence</provider><properties><property name="hibernate.hbm2ddl.auto" value="update"/><property name="hibernate.show_sql" value="false"/><property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/><property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/><property name="hibernate.connection.url" value="jdbc:hsqldb:mem:javacodegeeks"/><property name="hibernate.connection.username" value="sa"/><property name="hibernate.connection.password" value=""/><property name="hibernate.c3p0.min_size" value="5"/><property name="hibernate.c3p0.max_size" value="20"/><property name="hibernate.c3p0.timeout" value="300"/><property name="hibernate.c3p0.max_statements" value="50"/><property name="hibernate.c3p0.idle_test_period" value="3000"/></properties></persistence-unit></persistence>

    这里要注意的事情:

    1. 如果您打算将Web应用程序部署到支持JTA事务(例如JBoss)的J2EE应用服务器或使用其他数据库(例如Oracle , MySQL等),请参阅此处的 “ JBoss Spring JPA Hibernate教程”以了解替代配置。

    现在让我们创建将驱动Spring容器的applicationContext.xml文件。 在/ war / WEB-INF目录下创建文件。 下面提供了一个示例applicationContext.xml

    <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"><context:component-scan base-package="com.javacodegeeks.gwtspring"/><task:annotation-driven executor="myExecutor" scheduler="myScheduler"/><task:executor id="myExecutor" pool-size="5"/><task:scheduler id="myScheduler" pool-size="10"/><tx:annotation-driven/><bean class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" id="entityManagerFactory"><property name="persistenceUnitName" value="MyPersistenceUnit"/></bean><bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"><property name="entityManagerFactory" ref="entityManagerFactory"/></bean></beans>

    这里要注意的事情:

    1. 将context:component-scan元素的base-package属性更改为项目的基本包,以扫描Spring组件(服务,DAO等)。
    2. 根据persistence.xml文件中的指示,将entityManagerFactory bean的persistentUnitName属性的value属性更改为您的持久单元的名称
    3. 如果您打算将Web应用程序部署到支持JTA事务的J2EE应用程序服务器(例如JBoss),请参阅此处的 “ JBoss Sping JPA Hibernate教程”以了解替代配置。

    在本教程的最后一部分中,我们将介绍用于在客户端和服务器之间传输数据的数据传输对象(DTO),用于访问数据库的数据访问对象(DAO)和用于公开功能的Spring服务。到GWT Web客户端。

    DTO是客户端和服务器都可以使用的对象,因此您应该在“共享”程序包下创建一个“ dto”子程序包,然后将DTO放在那里。 我们将创建一个EmployeeDTO,其中包含有关员工的信息,如下所示

    package com.javacodegeeks.gwtspring.shared.dto;import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;@Entity
    @Table(name = "EMPLOYEE")
    public class EmployeeDTO 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 EmployeeDTO() {}public EmployeeDTO(int employeeId) {this.employeeId = employeeId;        }public EmployeeDTO(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;}}

    DAO对象将用于访问数据库并执行CRUD(创建检索更新删除)操作。 它是服务器端组件,因此应放置在我们项目的“服务器”子程序包下。 创建一个“ dao”子程序包,然后将DAO放在此处。 下面是一个示例DAO

    package com.javacodegeeks.gwtspring.server.dao;import javax.annotation.PostConstruct;
    import javax.persistence.EntityManagerFactory;import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;import com.javacodegeeks.gwtspring.shared.dto.EmployeeDTO;@Repository("employeeDAO")
    public class EmployeeDAO extends JpaDAO<Long, EmployeeDTO> {@AutowiredEntityManagerFactory entityManagerFactory;@PostConstructpublic void init() {super.setEntityManagerFactory(entityManagerFactory);}}

    如您所见,EmployeeDAO类扩展了基本的DAO类(JpaDAO)。 EmployeeDAO类可以包含有关EmployeeDTO对象的特定查询,但是所有CRUD操作都可以从基本DAO类(JpaDAO)处理。 在“ dao”子程序包下,将JpaDAO类与EmployeeDAO类放置在同一级别。 下面我们介绍JpaDAO类

    package com.javacodegeeks.gwtspring.server.dao; import java.lang.reflect.ParameterizedType; 
    import java.util.List; import javax.persistence.EntityManager; 
    import javax.persistence.PersistenceException; 
    import javax.persistence.Query; import org.springframework.orm.jpa.JpaCallback; 
    import org.springframework.orm.jpa.support.JpaDaoSupport; public abstract class JpaDAO<K, E> extends JpaDaoSupport { protected Class<E> entityClass; @SuppressWarnings("unchecked") public JpaDAO() { ParameterizedType genericSuperclass = (ParameterizedType) getClass() .getGenericSuperclass(); this.entityClass = (Class<E>) genericSuperclass .getActualTypeArguments()[1]; } public void persist(E entity) { getJpaTemplate().persist(entity); } public void remove(E entity) { getJpaTemplate().remove(entity); } public E merge(E entity) { return getJpaTemplate().merge(entity); } public void refresh(E entity) { getJpaTemplate().refresh(entity); } public E findById(K id) { return getJpaTemplate().find(entityClass, id); } public E flush(E entity) { getJpaTemplate().flush(); return entity; } @SuppressWarnings("unchecked") public List<E> findAll() { Object res = getJpaTemplate().execute(new JpaCallback() { public Object doInJpa(EntityManager em) throws PersistenceException { Query q = em.createQuery("SELECT h FROM " + entityClass.getName() + " h"); return q.getResultList(); } }); return (List<E>) res; } @SuppressWarnings("unchecked") public Integer removeAll() { return (Integer) getJpaTemplate().execute(new JpaCallback() { public Object doInJpa(EntityManager em) throws PersistenceException { Query q = em.createQuery("DELETE FROM " + entityClass.getName() + " h"); return q.executeUpdate(); } }); }}

    最后,我们将为GWT客户端创建服务接口和实现类。 客户端和服务器都应可以访问该服务接口,因此应将其放置在我们项目的“共享”子包下。 创建一个“服务”子程序包,并将服务接口放置在那里。 下面是一个示例接口类

    package com.javacodegeeks.gwtspring.shared.services;import com.google.gwt.user.client.rpc.RemoteService;
    import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;import com.javacodegeeks.gwtspring.shared.dto.EmployeeDTO;@RemoteServiceRelativePath("springGwtServices/employeeService")
    public interface EmployeeService extends RemoteService {public EmployeeDTO 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;}

    这里要注意的事情:

    1. GWT客户端必须能够对服务器端服务进行异步远程过程调用(RPC)。 因此,服务接口必须扩展RemoteService接口。 还必须提供指定接口的异步对应项以启用异步通信(请参见下文)
    2. 我们对接口进行注释,以便定义可访问服务的URL。 由于该服务是Spring服务,因此我们希望spring4gwt拦截RPC调用并执行Spring服务调用。 为此,我们定义了一个相对路径,该相对路径将由在我们的web.xml中声明的“ springGwtRemoteServiceServlet”处理,如上所示。
    3. 在“ RemoteServiceRelativePath”注释中声明的服务名称(此处为“ employeeService”)必须与Spring服务bean名称匹配。 我们将在服务实现类中定义Spring服务bean名称(请参见下文)

    服务接口的异步计数器部分如下

    package com.javacodegeeks.gwtspring.shared.services;import com.google.gwt.user.client.rpc.AsyncCallback;
    import com.javacodegeeks.gwtspring.shared.dto.EmployeeDTO;public interface EmployeeServiceAsync {void deleteEmployee(long employeeId, AsyncCallback<Void> callback);void findEmployee(long employeeId, AsyncCallback<EmployeeDTO> callback);void saveEmployee(long employeeId, String name, String surname,String jobDescription, AsyncCallback<Void> callback);void saveOrUpdateEmployee(long employeeId, String name, String surname,String jobDescription, AsyncCallback<Void> callback);void updateEmployee(long employeeId, String name, String surname,String jobDescription, AsyncCallback<Void> callback);}

    服务实现类是服务器端组件,因此我们必须将其放置在项目的“服务器”子包下。 创建“服务”子包并将其放在那里。 下面提供了一个示例服务实现类

    package com.javacodegeeks.gwtspring.server.services; import javax.annotation.PostConstruct; 
    import javax.annotation.PreDestroy; import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.stereotype.Service; 
    import org.springframework.transaction.annotation.Propagation; 
    import org.springframework.transaction.annotation.Transactional; import com.javacodegeeks.gwtspring.server.dao.EmployeeDAO; 
    import com.javacodegeeks.gwtspring.shared.dto.EmployeeDTO; 
    import com.javacodegeeks.gwtspring.shared.services.EmployeeService; @Service("employeeService") 
    public class EmployeeServiceImpl implements EmployeeService { @Autowired private EmployeeDAO employeeDAO; @PostConstruct public void init() throws Exception { } @PreDestroy public void destroy() { } public EmployeeDTO findEmployee(long employeeId) { return employeeDAO.findById(employeeId); } @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) public void saveEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception { EmployeeDTO employeeDTO = employeeDAO.findById(employeeId); if(employeeDTO == null) { employeeDTO = new EmployeeDTO(employeeId, name,surname, jobDescription); employeeDAO.persist(employeeDTO); } } @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) public void updateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception { EmployeeDTO employeeDTO = employeeDAO.findById(employeeId); if(employeeDTO != null) { employeeDTO.setEmployeeName(name); employeeDTO.setEmployeeSurname(surname); employeeDTO.setJob(jobDescription); } } @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) public void deleteEmployee(long employeeId) throws Exception { EmployeeDTO employeeDTO = employeeDAO.findById(employeeId); if(employeeDTO != null) employeeDAO.remove(employeeDTO); } @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) public void saveOrUpdateEmployee(long employeeId, String name, String surname, String jobDescription) throws Exception { EmployeeDTO employeeDTO = new EmployeeDTO(employeeId, name,surname, jobDescription); employeeDAO.merge(employeeDTO); } }

    这里要注意的事情:

    1. 我们使用@Service(“ employeeService”)构造型注释,以便声明该类以名称“ exampleService”表示Spring服务。 Spring容器将在启动时实例化所有服务。
    2. 我们使用@Autowire批注将DAO类的实例注入“ employeeService”。 为了正确实例化服务, Spring容器必须首先解析服务中所有可能的引用,因此实例化DAO类并将实例注入到“ employeeService”的适当字段–“ employeeDAO”字段。 如果您想知道,依赖项注入是根据类型(类)完成的,如果不满足则是根据名称完成的,这意味着如果我们定义了多个相同类型(类)的服务,则注入的服务将是名称相同的服务作为指定字段。
    3. 我们使用Java批注@PostConstruct和@PreDestroy来声明在初始化(完成所有依赖项注入)和服务的先前销毁后Spring容器将调用的方法。
    4. 对于需要对数据库执行更新操作的所有方法(INSERT,UPDATE,DELETE),我们使用@Transactional批注
    5. 我们不要在对数据库执行检索(FIND)操作的方法上使用@Transactional批注(包含延迟初始化的引用的对象除外-参见下文),和/或不执行数据库操作。 那是因为每次您调用一个注解为事务性的方法时, Spring容器都会涉及到调用JPA的实体管理器以及结果平台的事务管理器,从而定义了将要应用的事务行为,特别是引入了明显的性能损失。适用于低延迟/高吞吐量应用
    6. 对于对包含延迟初始化的引用的对象执行检索(FIND)操作的方法,应使用@Transactional批注,指定“ NESTED”传播类型,以使Spring可以为整个方法调用保持Hibernate会话打开
    7. 事务处理行为仅应用于对服务的客户端调用。 事务行为不适用于内部操作调用。 例如,如果客户端调用未注释为事务性的操作,而后者的实现将对同一服务的另一个操作的调用引入了注释为事务性的调用,则对于合并的操作,将不会应用任何事务行为

    我们几乎完成了!,我们必须开发GWT用户界面才能访问我们的Spring服务。 尽管GWT用户界面开发不在本教程的讨论范围之内,我们仍将提供一个基本的用户界面,以展示几个Spring服务调用。

    找到您的GWT应用程序的入口点。 该文件的名称应类似于{project_name} .java(在我们的情况下为GWTSpring.java),并位于“ client”子包或主包下。 更改入口点类,如下所示

    package com.javacodegeeks.gwtspring.client;import com.google.gwt.core.client.EntryPoint;
    import com.google.gwt.core.client.GWT;
    import com.google.gwt.event.dom.client.ClickEvent;
    import com.google.gwt.event.dom.client.ClickHandler;
    import com.google.gwt.event.dom.client.KeyCodes;
    import com.google.gwt.event.dom.client.KeyUpEvent;
    import com.google.gwt.event.dom.client.KeyUpHandler;
    import com.google.gwt.user.client.rpc.AsyncCallback;
    import com.google.gwt.user.client.ui.Button;
    import com.google.gwt.user.client.ui.DialogBox;
    import com.google.gwt.user.client.ui.HTML;
    import com.google.gwt.user.client.ui.Label;
    import com.google.gwt.user.client.ui.RootPanel;
    import com.google.gwt.user.client.ui.TextBox;
    import com.google.gwt.user.client.ui.VerticalPanel;
    import com.javacodegeeks.gwtspring.shared.dto.EmployeeDTO;
    import com.javacodegeeks.gwtspring.shared.services.EmployeeService;
    import com.javacodegeeks.gwtspring.shared.services.EmployeeServiceAsync;/*** Entry point classes define <code>onModuleLoad()</code>.*/
    public class GWTSpring implements EntryPoint {/*** The message displayed to the user when the server cannot be reached or* returns an error.*/private static final String SERVER_ERROR = "An error occurred while "+ "attempting to contact the server. Please check your network "+ "connection and try again. The error is : ";/*** Create a remote service proxy to talk to the server-side Employee service.*/private final EmployeeServiceAsync employeeService = GWT.create(EmployeeService.class);/*** This is the entry point method.*/public void onModuleLoad() {final Button saveOrUpdateButton = new Button("SaveOrUpdate");final Button retrieveButton = new Button("Retrieve");final TextBox employeeInfoField = new TextBox();employeeInfoField.setText("Employee Info");final TextBox employeeIdField = new TextBox();final Label errorLabel = new Label();// We can add style names to widgetssaveOrUpdateButton.addStyleName("sendButton");retrieveButton.addStyleName("sendButton");// Add the nameField and sendButton to the RootPanel// Use RootPanel.get() to get the entire body elementRootPanel.get("employeeInfoFieldContainer").add(employeeInfoField);RootPanel.get("updateEmployeeButtonContainer").add(saveOrUpdateButton);RootPanel.get("employeeIdFieldContainer").add(employeeIdField);RootPanel.get("retrieveEmployeeButtonContainer").add(retrieveButton);RootPanel.get("errorLabelContainer").add(errorLabel);// Focus the cursor on the name field when the app loadsemployeeInfoField.setFocus(true);employeeInfoField.selectAll();// Create the popup dialog boxfinal DialogBox dialogBox = new DialogBox();dialogBox.setText("Remote Procedure Call");dialogBox.setAnimationEnabled(true);final Button closeButton = new Button("Close");// We can set the id of a widget by accessing its ElementcloseButton.getElement().setId("closeButton");final Label textToServerLabel = new Label();final HTML serverResponseLabel = new HTML();VerticalPanel dialogVPanel = new VerticalPanel();dialogVPanel.addStyleName("dialogVPanel");dialogVPanel.add(new HTML("<b>Sending request to the server:</b>"));dialogVPanel.add(textToServerLabel);dialogVPanel.add(new HTML("
    <b>Server replies:</b>"));dialogVPanel.add(serverResponseLabel);dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);dialogVPanel.add(closeButton);dialogBox.setWidget(dialogVPanel);// Add a handler to close the DialogBoxcloseButton.addClickHandler(new ClickHandler() {public void onClick(ClickEvent event) {dialogBox.hide();saveOrUpdateButton.setEnabled(true);saveOrUpdateButton.setFocus(true);retrieveButton.setEnabled(true);}});// Create a handler for the saveOrUpdateButton and employeeInfoFieldclass SaveOrUpdateEmployeeHandler implements ClickHandler, KeyUpHandler {/*** Fired when the user clicks on the saveOrUpdateButton.*/public void onClick(ClickEvent event) {sendEmployeeInfoToServer();}/*** Fired when the user types in the employeeInfoField.*/public void onKeyUp(KeyUpEvent event) {if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {sendEmployeeInfoToServer();}}/*** Send the employee info from the employeeInfoField to the server and wait for a response.*/private void sendEmployeeInfoToServer() {// First, we validate the input.errorLabel.setText("");String textToServer = employeeInfoField.getText();// Then, we send the input to the server.saveOrUpdateButton.setEnabled(false);textToServerLabel.setText(textToServer);serverResponseLabel.setText("");String[] employeeInfo = textToServer.split(" ");long employeeId = Long.parseLong(employeeInfo[0]);String employeeName = employeeInfo[1];String employeeSurname = employeeInfo[2];String employeeJobTitle = employeeInfo[3];employeeService.saveOrUpdateEmployee(employeeId, employeeName, employeeSurname, employeeJobTitle, new AsyncCallback<Void>() {public void onFailure(Throwable caught) {// Show the RPC error message to the userdialogBox.setText("Remote Procedure Call - Failure");serverResponseLabel.addStyleName("serverResponseLabelError");serverResponseLabel.setHTML(SERVER_ERROR + caught.toString());dialogBox.center();closeButton.setFocus(true);}public void onSuccess(Void noAnswer) {dialogBox.setText("Remote Procedure Call");serverResponseLabel.removeStyleName("serverResponseLabelError");serverResponseLabel.setHTML("OK");dialogBox.center();closeButton.setFocus(true);}});}}// Create a handler for the retrieveButton and employeeIdFieldclass RetrieveEmployeeHandler implements ClickHandler, KeyUpHandler {/*** Fired when the user clicks on the retrieveButton.*/public void onClick(ClickEvent event) {sendEmployeeIdToServer();}/*** Fired when the user types in the employeeIdField.*/public void onKeyUp(KeyUpEvent event) {if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {sendEmployeeIdToServer();}}/*** Send the id from the employeeIdField to the server and wait for a response.*/private void sendEmployeeIdToServer() {// First, we validate the input.errorLabel.setText("");String textToServer = employeeIdField.getText();// Then, we send the input to the server.retrieveButton.setEnabled(false);textToServerLabel.setText(textToServer);serverResponseLabel.setText("");employeeService.findEmployee(Long.parseLong(textToServer),  new AsyncCallback<EmployeeDTO>() {public void onFailure(Throwable caught) {// Show the RPC error message to the userdialogBox.setText("Remote Procedure Call - Failure");serverResponseLabel.addStyleName("serverResponseLabelError");serverResponseLabel.setHTML(SERVER_ERROR + caught.toString());dialogBox.center();closeButton.setFocus(true);}public void onSuccess(EmployeeDTO employeeDTO) {dialogBox.setText("Remote Procedure Call");serverResponseLabel.removeStyleName("serverResponseLabelError");if(employeeDTO != null)serverResponseLabel.setHTML("Employee Information Id : " + employeeDTO.getEmployeeId() + " Name : " + employeeDTO.getEmployeeName() + " Surname : " + employeeDTO.getEmployeeSurname() + " Job Title : " + employeeDTO.getJob());elseserverResponseLabel.setHTML("No employee with the specified id found");dialogBox.center();closeButton.setFocus(true);}});}}// Add a handler to send the employee info to the serverSaveOrUpdateEmployeeHandler saveOrUpdateEmployeehandler = new SaveOrUpdateEmployeeHandler();saveOrUpdateButton.addClickHandler(saveOrUpdateEmployeehandler);employeeInfoField.addKeyUpHandler(saveOrUpdateEmployeehandler);// Add a handler to send the employee id to the serverRetrieveEmployeeHandler retrieveEmployeehandler = new RetrieveEmployeeHandler();retrieveButton.addClickHandler(retrieveEmployeehandler);employeeIdField.addKeyUpHandler(retrieveEmployeehandler);}
    }

    如您所见, Spring服务调用对客户端透明地执行,就像经典的GWT服务调用一样。

    最后找到您的项目的主页。 该文件的名称应为{project_name} .html,在我们的情况下为GWTSpring.html,位于项目的/ war文件夹下。 更改主网页,如下所示

    <!doctype html>
    <!-- The DOCTYPE declaration above will set the    -->
    <!-- browser's rendering engine into               -->
    <!-- "Standards Mode". Replacing this declaration  -->
    <!-- with a "Quirks Mode" doctype may lead to some -->
    <!-- differences in layout.                        --><html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8"><!--                                                               --><!-- Consider inlining CSS to reduce the number of requested files --><!--                                                               --><link type="text/css" rel="stylesheet" href="GWTSpring.css"><!--                                           --><!-- Any title is fine                         --><!--                                           --><title>Spring GWT Web Application Starter Project</title><!--                                           --><!-- This script loads your compiled module.   --><!-- If you add any GWT meta tags, they must   --><!-- be added before this line.                --><!--                                           --><script type="text/javascript" language="javascript" src="gwtspring/gwtspring.nocache.js"></script></head><!--                                           --><!-- The body can have arbitrary html, or      --><!-- you can leave the body empty if you want  --><!-- to create a completely dynamic UI.        --><!--                                           --><body><!-- OPTIONAL: include this if you want history support --><iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe><!-- RECOMMENDED if your web app will not function without JavaScript enabled --><noscript><div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif">Your web browser must have JavaScript enabledin order for this application to display correctly.</div></noscript><h1>Spring GWT Web Application Starter Project</h1><table align="center"><tr><td colspan="2" style="font-weight:bold;">Please enter employee info (id name surname job):</td>        </tr><tr><td id="employeeInfoFieldContainer"></td><td id="updateEmployeeButtonContainer"></td></tr><tr><tr><td colspan="2" style="font-weight:bold;">Please enter employee id:</td>        </tr><tr><td id="employeeIdFieldContainer"></td><td id="retrieveEmployeeButtonContainer"></td></tr><tr><td colspan="2" style="color:red;" id="errorLabelContainer"></td></tr></table></body>
    </html>

    要编译应用程序,请右键单击项目名称,然后选择“运行方式?”。 编译GWT应用程序

    要部署Web应用程序,只需将/ war文件夹复制到Apache – Tomcat “ webapps”文件夹中。 您可以将war文件夹的名称更改为任意名称,最好在项目名称后重命名,例如GWTSpring

    要启动该应用程序,请将浏览器指向以下地址

    http:// localhost:8080 / GWTSpring /

    如果一切顺利,您应该会看到您的主页。 应显示两个文本框,每个文本框后面都有一个按钮。 在第一个文本框中,您可以将雇员保存或更新到数据库。 作为输入,提供ID,名称,姓氏和职位描述,并用空格字符分隔。 单击“ SaveOrUpdate”按钮,将提供的信息存储到数据库中。 对于现有员工条目(相同的ID),将执行更新。 第二个文本框用于检索现有员工条目。 提供员工ID,然后单击“检索”按钮。 如果该员工存在,则应该看到该员工的ID,姓名,姓氏和职位描述。

    很少,那是一个很大的教程!

    您可以从此处下载项目(不包括开头所述的必需的第三方库)

    希望你喜欢

    贾斯汀

    相关文章 :
    • GWT Spring和Hibernate进入数据网格世界
    • Spring 3 HornetQ 2.1集成教程
    • Spring3 RESTful Web服务
    • GWT 2 Spring 3 JPA 2 Hibernate 3.5教程– Eclipse和Maven 2展示
    • 带有Spring和Maven教程的JAX–WS
    相关片段:
    • 在Spring容器中声明bean
    • 使用JPA持久化对象
    • JPA中的一对多双向映射
    • JPA CRUD示例
    • 使用Hibernate持久化对象

翻译自: https://www.javacodegeeks.com/2010/05/gwt-2-spring-3-jpa-2-hibernate-35-2.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/374802.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

洛谷P1014 [NOIP1999 普及组] Cantor 表

现代数学的著名证明之一是 Georg Cantor 证明了有理数是可枚举的。他是用下面这一张表来证明这一命题的&#xff1a; 代码 import java.util.*; public class Main{public static void main(String[] args){//int x1 0;int i 0;Scanner sc new Scanner(System.in);int n s…

3522: [Poi2014]Hotel( 树形dp )

枚举中点x( 即选出的三个点 a , b , c 满足 dist( x , a ) dist( x , b ) dist( x , c ) ) , 然后以 x 为 root 做 dfs , 显然两个位于 x 的同一颗子树内的点是不可能被同时选到的 . 我们对 x 的每一颗子树进行 dfs , 记录下当前子树中的点到 x 距离为 d ( 1 < d < n )…

第一冲刺阶段工作总结02

1.昨天&#xff1a; 实验简单的安卓程序&#xff0c;开始具体的设计软件界面。 2.今天&#xff1a; 继续设计软件页面&#xff0c;由于安卓虚拟机过于迟缓&#xff0c;配置真机&#xff0c;学习如何在真机上运行程序。 3.遇到的困难&#xff1a; 真机配置不知道怎样配置&#x…

JBoss 4.2.x Spring 3 JPA Hibernate教程第2部分

我们将继续有关Spring 3 &#xff0c; Hibernate &#xff0c; JPA和JBoss 4.2.x – 4.3集成的教程 。 最后一步是创建一个Spring服务&#xff0c;以向最终用户公开功能。 我们必须创建一个接口类和相关的实现类。 首先是接口类&#xff1a; package com.mycomp.myproject.se…

洛谷P1035 [NOIP2002 普及组] 级数求和

代码 import java.util.Scanner;public class Main {public static void main(String args[]){Scanner sc new Scanner(System.in);int k sc.nextInt();int n 0;double Sn 0;while(Sn<k){n;Sn Sn 1.0/n;}System.out.println(n);} }这样写while循环体这需要每次加上1/…

『Luogu OJ』『C++』Level 1-1

关卡1-1&#xff0c;3道题 洛谷的第一个任务 任务说明&#xff1a;勇敢的迈出第一步&#xff0c;了解下语言和洛谷。跟着书本和老师走&#xff0c;不会难的。 要完成这个任务&#xff0c;请将以下的题目都AC掉&#xff08;即通过这道题目&#xff09;&#xff1a; 1.AB Problem…

Java中的Google ClientLogin实用程序

Google API的身份验证和授权是当今需要与Google服务集成和信息交换的应用程序中的常见功能。 尽管大多数Google身份验证过程是针对Web应用程序量身定制的&#xff0c;但它也可用于桌面和已安装的应用程序。 对于桌面应用程序&#xff0c;Google建议使用称为ClientLogin的身份验…

九度OJ1486 /POJ 1029/2012北京大学研究生复试上机

wa到死&#xff01;wa到死&#xff01;这是一个看着简单&#xff0c;坑及其多的题&#xff01; 坑一&#xff1a;POJ上是单组输入&#xff0c;九度上是多组输入&#xff0c;妈蛋要是研究生复试遇到这种大坑肯定死掉啊&#xff01;而且对于codeforces比较习惯的 同学肯定会觉得巨…

P1046 [NOIP2005 普及组] 陶陶摘苹果

题目描述 陶陶家的院子里有一棵苹果树&#xff0c;每到秋天树上就会结出 1010 个苹果。苹果成熟的时候&#xff0c;陶陶就会跑去摘苹果。陶陶有个 3030 厘米高的板凳&#xff0c;当她不能直接用手摘到苹果的时候&#xff0c;就会踩到板凳上再试试。 现在已知 1010 个苹果到地面…

新手不了解Xcode和mac系统可能犯得错误和我的建议

我是学iOS刚入门的新手&#xff0c;本人装的时黑苹果&#xff0c;我是喜欢完美的人&#xff0c;但黑苹果又是不完美的系统&#xff0c;比如关不了机啊&#xff0c;和显卡驱动不了啊&#xff0c;当自己的电脑出现白屏和卡顿的时候气的没脾气。我是一个新手。开始学的时java但我喜…

改善Java应用程序性能的快速技巧

曾经遇到过性能问题吗&#xff1f; 我也是。 如果我的经理再喊一次“ faaaaster”&#xff0c;我一生都会有听力障碍。 顺便说一句&#xff0c;我能听到所有声音中的德语发音吗&#xff1f; ;-) 您可以相信仍然有人无知地在谈论垃圾收集器&#xff08;得到它吗&#xff1f;&…

P1047 [NOIP2005 普及组] 校门外的树

某校大门外长度为 ll 的马路上有一排树&#xff0c;每两棵相邻的树之间的间隔都是 11 米。我们可以把马路看成一个数轴&#xff0c;马路的一端在数轴 00 的位置&#xff0c;另一端在 ll 的位置&#xff1b;数轴上的每个整数点&#xff0c;即 0,1,2,\dots,l0,1,2,…,l&#xff0…

团队开发——个人工作总结04

昨天对要用到的SQL语句进行了研究&#xff0c;分别得到了以下结果&#xff1a; 1.这段语句是为用户登录服务的&#xff0c;通过JSP的到的用户名username和密码passdword作为条件查询数据库&#xff0c;如果有查询结果&#xff0c;则返回true select * from [login] where usern…

Nginx的几种常见的几种启动方式

1.默认方式启动 直接执行Nginx的二进制文件即可 /usr/local/nginx/sbin/nginx 这时默认读取配置文件&#xff0c;配置文件目录 /usr/local/nginx/conf/nginx.conf 2.指定配置文件的启动方式 /usr/local/nginx/sbin/nginx -c /tmp/nginx.conf转载于:https://www.cnblogs.com/Leo…

yii2阅读随笔14

继续来看Event.php /*** Triggers a class-level event.* 触发类级别事件。* This method will cause invocation of event handlers that are attached to the named event* for the specified class and all its parent classes.* 触发某个类或者对象的某个事件* param strin…

P1059 [NOIP2006 普及组] 明明的随机数

题目描述 明明想在学校中请一些同学一起做一项问卷调查&#xff0c;为了实验的客观性&#xff0c;他先用计算机生成了N个1到1000之间的随机整数(N≤100)&#xff0c;对于其中重复的数字&#xff0c;只保留一个&#xff0c;把其余相同的数去掉&#xff0c;不同的数对应着不同的学…

基本的EJB参考,注入和查找

在本系列的第一部分中 &#xff0c;我们介绍了Enterprise JavaBeans v。3.0规范提供的机制&#xff0c;用于定义EJB组件&#xff0c;声明对EJB的引用并通过依赖项注入或程序化JNDI查找将它们连接起来。 在此博客文章中&#xff0c;我们将研究一些基本示例以了解如何使用EJB API…

ViewPager使用笔记

1.ViewPager.setCurrentItem(position)&#xff0c;即使已设置动画&#xff0c;但是没有动画效果 原因&#xff1a;因为ViewPager滑动之前的时间间隔太短&#xff0c;可以通过反射&#xff0c;去修改ViewPager自动滑动时间&#xff0c;代码实现如下 1 public class ViewPagerSc…

IOS开发之Swift学习笔记

1.因为存储属性要求初始化&#xff0c;我们可以使用lazy修饰符来延迟初始化。转载于:https://www.cnblogs.com/luntai/p/5430223.html

力扣1两数之和

给定一个整数数组 nums 和一个整数目标值 target&#xff0c;请你在该数组中找出 和为目标值 target 的那 两个 整数&#xff0c;并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是&#xff0c;数组中同一个元素在答案里不能重复出现。 你可以按任意顺序返回…