简体   繁体   English

如何在 GWT Web 应用程序中正确实现 DAO?

[英]How to correctly implement a DAO in a GWT web app?

I have a couple of questions to be answered relating DAOs and GWT.我有几个关于 DAO 和 GWT 的问题需要回答。 I'm implementing a DAO class in the GWT project and I want to use it when a button is pressed, like this: (inside the .java GWT class)我正在 GWT 项目中实现一个 DAO 类,我想在按下按钮时使用它,如下所示:(在 .java GWT 类中)

      lookUpButton.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
              lookup();
            }
          });  
    ...    ...     ...
       private void lookup() {
          PersonDao dao = new PersonDao();
          Person m = dao.getPerson(3); //hard-coded the pk of the person
          resultsFlexTable.setText(1, 0, m.toString());

  • I get two problems here, the first is practical, when I compile the project, I get an error and it just says "failed, try again" in my browser and I cannot run it.我在这里遇到两个问题,第一个是实用的,当我编译项目时,我收到一个错误,它只是在我的浏览器中说“失败,再试一次”,但我无法运行它。

  • The second question I have is this: Is it really a good practice to use a DAO in a GWT class given that it compiles directly into AJAX?我的第二个问题是:鉴于 DAO 直接编译为 AJAX,在 GWT 类中使用 DAO 真的是一个好习惯吗? Or should I send a request to a servlet that has said DAO and performs the data access itself?或者我应该向一个 servlet 发送请求,该 servlet 已经说过 DAO 并自己执行数据访问?

  • Does GWT provide an easy to understand (for a beginner) and better way to access a MySQL database to get data? GWT 是否提供了一种易于理解(对于初学者)和更好的方式来访问 MySQL 数据库以获取数据?

(Context: I'm trying to build a basic search engine for a database and I need to access said data from a GWT widget. I'm learning Java web development, and I've learnt about .jsp, Servlets, and some more basic stuff like DAOs. For college, I have to build as a final project a web application which necessarily must be using the GWT Framework.) I've already tried the documentation but I cannot really get through this, I'm stuck. (上下文:我正在尝试为数据库构建一个基本的搜索引擎,我需要从 GWT 小部件访问所述数据。我正在学习 Java Web 开发,并且我已经了解了 .jsp、Servlets 等等像 DAO 这样的基本东西。对于大学,我必须构建一个 Web 应用程序作为最终项目,该应用程序必须使用 GWT 框架。)我已经尝试过文档,但我无法真正解决这个问题,我被卡住了。

This is not how gwt will work .... you can not simply write DAO layer call at UI side,这不是 gwt 的工作方式......你不能简单地在 UI 端编写 DAO 层调用,

GWT is divided into 3 parts - to write the code GWT分为3部分——写代码

  • Client - to write the UI code客户端- 编写 UI 代码
  • Shared - to write shared code which will be used in client as well as server side for ex - Model/Pojo classes共享- 编写将在客户端和服务器端使用的共享代码,例如模型/Pojo 类
  • Server - to write services / function / JDBC methods服务器- 编写服务/函数/JDBC 方法

It should always be this way -应该一直这样——

在此处输入图片说明

There are so many sample applications and examples available over sites.站点上提供了许多示例应用程序和示例。

There is one simple example here - https://github.com/davisford/gwt-demo/tree/master/src/main/java/com/example这里有一个简单的例子 - https://github.com/davisford/gwt-demo/tree/master/src/main/java/com/example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM