Java 在GWT web应用程序中使用外部对象具体需要什么

Java 在GWT web应用程序中使用外部对象具体需要什么,java,gwt,gwt-rpc,Java,Gwt,Gwt Rpc,实际的代码是在一个没有互联网接入的服务器上,所以我将在这里使用伪代码来解释我试图实现的目标 我有一个叫做MyColhmi的项目 其中包含一个GWT网站,其中包含多个客户端/服务器/共享子包 对于这个例子,我在mycolhmi/com/src/foods/cookies/client/ShowCookieList.java中工作 我在同一个工作区中还有一个名为IGotYerCookies的项目,其中包含一个数据库DAO/DTO,允许访问“最佳”cookies表。dao和dto分别称为com.src

实际的代码是在一个没有互联网接入的服务器上,所以我将在这里使用伪代码来解释我试图实现的目标

我有一个叫做MyColhmi的项目 其中包含一个GWT网站,其中包含多个客户端/服务器/共享子包 对于这个例子,我在mycolhmi/com/src/foods/cookies/client/ShowCookieList.java中工作

我在同一个工作区中还有一个名为IGotYerCookies的项目,其中包含一个数据库DAO/DTO,允许访问“最佳”cookies表。dao和dto分别称为com.src.gotyercookies.database.TheBestCookiesDAO.java和com.src.gotyercookies.database.TheBestCookiesDTO.java 我需要调用BestCookiesDao.getBestCookie(),它返回包含ShowCookieList.java中前3个Cookie的列表

为了让ShowCookieList.java使用BestCookiesDAO和BestCookiesDTO(类路径内容除外),我需要做什么

我已经为DAO和DTO编写了Service和ServiceAsync类,并将它们放在mycolhmi/com/src/foods/cookies/shared中。 我还为DAO和DTO编写了ServiceAsyncImpl,并将它们与IGotYerCookies中最好的CookiesDAO/DTO放在同一个目录中

当DTO从回调中返回时,我无法思考如何处理它,或者如何设置和安排实际调用以获得一个返回


最后,服务器是Impl类必须去的地方,共享是服务和ServiceAsync结束的地方。然后我不得不在web.xml文件中添加一个条目。

您将无法在客户端使用这些类。最简单的解决方案是使用GWT RPC访问这些对象。阅读本页有关如何使用GWT RPC的内容

这是如何在客户端访问它的

BestCookiesServiceAsync service = (BestCookiesServiceAsync) GWT.create(BestCookiesService.class);
客户端中的最佳服务接口

@RemoteServiceRelativePath("BestCookiesService")
public interface BestCookiesService extends RemoteService {

   // methods
   // Cookie should implement IsSerializable
   Cookie[] getBestCookies(); 

}
BestCookiesServiceAsync接口(以前版本的副本,但带有异步回调)

最佳CookiesServiceImpl类。它是服务器端第一个接口的实现

public class BestCookiesServiceImpl extends RemoteServiceServlet implements BestCookiesService {

    // you can access your DAOs here
    @Override
    public Cookie[] getBestCookies() {
         BestCookiesDAO dao = getBestCookiesDAO();
         BestCookiesList list = dao.getBestCookies();

         Cookie[] array = new Cookie[list.size()];
         for (int i = 0; i < list.size(); i++) {
             array[i] = new Cookie(list.get(i).getIngredients());
         }

         return array;
    }

}
公共类BestCookiesServiceImpl扩展RemoteServiceServlet实现BestCookiesService{
//您可以在这里访问DAO
@凌驾
公共Cookie[]getBestCookies(){
BestCookiesDAO=getBestCookiesDAO();
BestCookiesList=dao.getBestCookies();
Cookie[]数组=新Cookie[list.size()];
对于(int i=0;i
嗯,这就是我要找的。。我想,如何设置一个RPC(尽管java只有方法,但我们称之为过程调用..grrrr)来查看外部对象。我将添加更多细节。从ShowCookieList内部调用getBestCookie的返回类型是什么,它返回一个DTO列表,其中包含前3个Cookie的成分和名称。那么,从ShowCookieList对象内部,我如何获取DTO列表并访问其方法来显示其中包含的名称和成分呢?在您的BestCookies服务中,如果我将一个BestCookies方法放入getBestCookies()中它错误地说它找不到BestCookiedTo的入口点。最好在客户端包中创建一些Cookie类,其中包含您需要的字段。您可以在BestCookiesServiceImpl中执行,因为您可以访问那里的DAO和DTO。我将BestCookiesServiceImpl放在哪里,我将它放在MyColHMI/com/src/foods/cookies/server区域,但它无法访问BestCookiesService来实现,可以看到它,但GWT编译器无法看到它。。。BestCookiesService位于mycolhmi/com/src/foods/cookies/client中。。它应该是共享的吗?
public class BestCookiesServiceImpl extends RemoteServiceServlet implements BestCookiesService {

    // you can access your DAOs here
    @Override
    public Cookie[] getBestCookies() {
         BestCookiesDAO dao = getBestCookiesDAO();
         BestCookiesList list = dao.getBestCookies();

         Cookie[] array = new Cookie[list.size()];
         for (int i = 0; i < list.size(); i++) {
             array[i] = new Cookie(list.get(i).getIngredients());
         }

         return array;
    }

}