Problem with multiple entry Points in the same module

有些话、适合烂在心里 提交于 2019-11-29 02:07:07

The correct way is to have a single entry point per module, that sticks the appropriate widgets in the appropriate divs:

RootPanel panel = RootPanel.get("someHomeWidget");
if (panel) panel.add(new HomeWidget());

panel = RootPanel.get("adminWidget");
if (panel) panel.add(new AdminWidget());

That way it just scans the page looking for any divs you have and inserts the appropriate widget. So your HTML page determines what widgets are displayed when and the GWT code is ready to handle any situation. There's nothing about the above that stinks, it's the way your entry point should be written.

The alternative is if your admin area and normally area are totally different (eg: you want to load them at separate times) then they should be separate modules, with separate entry points.

I also wanted to use multiple pages in a GWT toy app and I think I figured it out. It took some massaging of the deployment descriptor (myApp.gwt.xml), but here's what I did.

  • Made another class implementing EntryPoint and added some code that added to a div only in the new page.
  • Copied the original gwt.xml and changed two things:
    • The module-rename-to - I changed to "anothergwtapp"
    • The entry point specified the new class.
  • When I compile the project, there is another directory in the "war" folder called (wait for it...) "anothergwtapp". It contained the "anothergwtapp.nocache.js" that is the GWT magic.
  • Finally, I copied the orginal HTML page and replaced the "stockwatcher/stockwatcher.nocache.js" with "anothergwtapp/anothergwtapp.nocache.js" (yes, I'm very new - the tutorial is still on my machine)
    • I changed the new HTML to be a little different (new divs for the new entry point's onload to populate) and I added a simple href to the new page in the first page.

It worked. Just duplicate the gwt.xml and provide a new name for the module to go along with the new app page. I looked at some of the other links and I may have actually done what was described, but there were too many words and redirects and such (i.e. I didn't really read anything). I am using the latest GWT plugin for Galileo so maybe IJWs now.

Dont consider Admin and home page as different pages. Concept of pages is not applicable to GWT, as there is only one single page, ie single entrypoint. If you want to give effect of different pages, then use URL rewriting features of GWT.

If you do want to use different Entrypoints, then as said in above comment, use different modules.

It's usually better to only have one EntryPoint. Multiple EntryPoints in one module all start at the same time and that can sometimes do things you did not expect.

You have plenty of options in how to handle it separately: - Have 2 different compilations one for Admin and one for the Home application. - Use the history tokens to indicate that you want Admin or Home - Check a JS variable to show one or the other - Check the presence of a specific DIV id to show Admin or Home (RootPanel.get(id)) - Use URL parameters to indicate the application. - ... etc

Maximilian Eberl

There is a simple (tricky) way to achieve this:

Make a Main class Your entry point.

<module rename-to='gwt'>
  <inherits name='com.google.gwt.user.User'/>
  <entry-point class='com.example.client.Main'/>
  <source path='client'/>
  <source path='shared'/>
</module>;<br/>

Create this Main.java to work like a dispatcher:

package com.example.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;

public class Main implements EntryPoint {

  public void onModuleLoad() {

    String url = Window.Location.getHref();
    if ( url.indexOf("?install")>-1 ) {
      Install install = Install.getInstance();
      RootPanel.get().add(install);      
    else if ( url.indexOf("?admin")>-1 ) {
      Admin admin = Admin.getInstance();
      RootPanel.get().add(admin);    
    } else {
      Application app = Application.getInstance();
      RootPanel.get().add(app);      
    }
  }
}

Now the different classes Application, Admin and Install work like seperate units.

Here is for example a simple Install:

package comexample.client;

import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;

public class Install extends FlowPanel {

  /** Singleton stuff - to access Main from all subclasses! */
  private static Install singelton;
  public static Install getInstance() {
    if (singelton == null) {singelton = new Install();}
    return singelton;
  }

  /** Constructor - called by Main.onModuleLoad() */
  private Install() {
    this.add(new HTML("<h1>Do whatever You have to do!</h1>"));
  }
}

You don't need the Singleton stuff (getInstance), but it is very handy in big applications.

Now in the /war-directory create directories named install and admin and in very of them create an HTML page like this:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0; URL=/index.html?install">
</head>
<body></body>
</html>

So when the user directs his Brower to http://www.example.com/install he will be redirected to http://www.example.com/index?install and index.html is bound to Main.java which will dispatch the request and load Install.java

I have a solution for this on my blog. You can download a sample maven project that has multiple entry points and uses url-rewriting. Take a look: http://zenoconsulting.wikidot.com/blog:16

Did you try this framework yet? http://gwtmultipage.org/ Claudius

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