GWT异步调用总是失败

时间:2013-07-10 21:30:34

标签: java gwt callback

我的gwt项目有些问题,我使用eclipselink和hsqldb作为数据库。

这是我的代码: 的 Project.java:

package com.example.client;
public class Project implements EntryPoint {

private final EmployeeServiceAsync eService = (EmployeeServiceAsync) GWT.create(EmployeeService.class);

[...] some GWT code


@Override
public void onModuleLoad() {

        eService.createemployee(new AsyncCallback<Void>() {
            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Fail!");
            }
            @Override
            public void onSuccess(Void result) {
            //nothing
            }
        });

但每次发出此警告都会失败:

WARNING: No file found for: /project/employeeService

那我怎么能正确地调用这个方法呢?

EmployeeService.java

package com.example.client.service;
@RemoteServiceRelativePath("employeeService")
public interface EmployeeService extends RemoteService{

public void createemployee();
}

EmployeeServiceAsync.java

package com.example.client.service;
public interface EmployeeServiceAsync {

void createemployee(AsyncCallback<Void> callback);
}

EmployeeServiceImpl

package com.example.server.ServiceImpl;
public class EmployeeServiceImpl extends RemoteServiceServlet implements EmployeeService {

private static final long serialVersionUID = 1L;

public void createemployee() {

    javax.persistence.EntityManagerFactory emf = Persistence.createEntityManagerFactory("ronfPU");
    javax.persistence.EntityManager em = emf.createEntityManager();


    try {
 // Create new Employee
    em.getTransaction().begin();
    Employee e1 = new Employee();
    e1.setName("admin");
    e1.setSurname("admin");
    e1.setUsername("admin");
    e1.setPassword("admin");
    em.persist(a1);
    em.getTransaction().commit();
        } finally {
    em.close();
    }   
  }
}

Employee类存储在com.example.shared.entity中;我认为persistence.xml和project.gwt.xml没问题,但我不确定 web.xml 代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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-app_2_5.xsd"
     version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">

<!-- Servlets -->
<servlet>
<servlet-name>employeeServiceImpl</servlet-name>
<servlet-class>
  com.example.server.ServiceImpl.EmployeeServiceImpl
</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>employeeServiceImpl</servlet-name>
</servlet-mapping>

<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>Project.html</welcome-file>
<url-pattern>/com.example.client.Project/employeeService</url-pattern>
</welcome-file-list>

</web-app>

首先,如果我理解正确的话,应该来自@RemoteServiceRelativePath("employeeService"); 而<servlet-class>派生自存储在服务器端的类,它扩展了RemoteServiceServlet; <servlet-mapping>应该与<servlet-name>相同,而在<url-pattern>中,我不确定我写的是什么。

您如何建议正确运行此代码?提前谢谢!

1 个答案:

答案 0 :(得分:1)

为客户端服务异步存根生成代码时,它会获取@RemoteServiceRelativePath注释的值,并将其作为GWT.getModuleBaseURL()的前缀(这在the javadoc for RemoteServiceRelativePath中有解释)

GWT.getModuleBaseURL()是您的nocache.js文件所在的“文件夹”。在标准设置中,这直接取决于模块的名称(project.gwt.xml和您放入的包)或您拥有的rename-to参数。根据错误消息,您的http://…/project/位于nocache.jsproject/project.nocache.js

您必须调整web.xml文件中的<url-pattern>以匹配该网址,因此在您的情况下应为/project/employeeService

简单地说:使用@RemoteServiceRelativePath配置客户端代码将调用的URL,并使用<url-pattern>配置您的服务“侦听”的URL;你必须使它们匹配才能让它们相互交谈。