How to correctly implement a DAO in a GWT web app?

做~自己de王妃 提交于 2019-12-24 08:10:13

问题


I have a couple of questions to be answered relating DAOs and 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)

      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? Or should I send a request to a servlet that has said DAO and performs the data access itself?

  • Does GWT provide an easy to understand (for a beginner) and better way to access a MySQL database to get data?

(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.


回答1:


This is not how gwt will work .... you can not simply write DAO layer call at UI side,

GWT is divided into 3 parts - to write the code

  • Client - to write the UI code
  • Shared - to write shared code which will be used in client as well as server side for ex - Model/Pojo classes
  • Server - to write services / function / JDBC methods

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://stackoverflow.com/questions/54155300/how-to-correctly-implement-a-dao-in-a-gwt-web-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!