views:

279

answers:

5

Hi,

I have a question for which I'm sure there must a simple solution. I'm writing a small GWT application where I want to achieve this:

www.domain.com : should serve the welcome page

www.domain.com/xyz : should serve page xyz, where xyz is just a key for an item in a database. If there is an item associated with key xyz, I'll load that and show a page, otherwise I'll show a 404 error page.

I was trying to modify the web.xml file accordingly but I just couldn't make it work. I could make it work with an url-pattern if the key in question is after another /, for example: www.domain.com/search/xyz. However, I'd like to have the key xyz directly following the root / (http://www.domain.com/xyz).

Something like

    <servlet-mapping> 
            <servlet-name>main</servlet-name> 
            <url-pattern>/</url-pattern> 
    </servlet-mapping> 

doesn't seem to work as I don't know how to address the main page index.html (as it's not an actual servlet) which will then load my main GWT module.

I could make it work with a bad work around (see below): Redirecting a 404 exception to index.html and then doing the look up in the main entry point, but I'm sure that's not the best practice, also for SEO purposes.

Can anyone give me a hint on how to configure the web.xml with GWT for my purpose?

Thanks a lot.

Mike

Work-around via 404:

Web.xml:

<web-app> 
  <error-page> 
     <exception-type>404</exception-type> 
     <location>/index.html</location> 
  </error-page> 
  <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
  </welcome-file-list> 
</web-app> 

index.html --> Main Entry point --> onModuleLoad():

String path = Window.Location.getPath(); 
if (path == null || path.length() == 0 || path.equalsIgnoreCase("/") 
|| path.equalsIgnoreCase("/index.html")) { 
    ... // load main page 
} else { 
    lookup(path.substring(1)); // that's key xyz 
+2  A: 

You don't!

GWT is meant fro creating single-page webapplications (RIA) that don't do hyperlinking to all sorts of loose pages. You essentially bootstrap the GWT app when loading it in the initial page and dynamically generate content in the app by using widgets, handling events and requesting server data through either GWT-RPC or simple HTTP requests (for instance when accessing a REST service on some other domain).

I introduced a colleague to GWT two weeks ago and let him dig around for a while when he got stuck on how to "link to a new page?". I explained GWT is not geared towards page-based navigational websites, but for building applications that use the browser as their runtime.

Hope this can shed a little light on your problem :)

Regards, -- Jeroen

Jeroen
Thanks a lot, Jeroen. Please see my comment on JP's answer. Maybe you have a solution for this?
Mike Brecht
+2  A: 

Jeroen is right. You don't.

However, if you want to force it, you can. Essentially every new url will have to have an HTML page that contains the GWT widget. Thus, causing the GWT widget to then have to reload itself on every new url. Then it would configure its UI accordingly based upon the URL. You can use a REST framework such as Jersey or Restlet to make things easier.

But, you only want to do this if you intend to distribute these new links or urls outside of your GWT app. However, GWT supports URL changes through anchors (not sure if this is the proper terminology), see the documentation on History for more information. Do not 'link' within your app to other URLs on your domain that contain the same GWT widget. Doing anything that will cause a page refresh and your GWT widget to reload would be very inefficient. Use ClickHandlers.

To conclude, don't do this. =)

JP
Thanks JP. I actually really want to distribute these links to the outside world. Let me explain my application in a bit more detail:- Users can create question forms. They go to the homepage (e.g. www.questionform.com), create a form and save it- Then they are given a URL where they can find their form (e.g. www.questionform.com/a3d2D39s), which they can send to their co-workers to fill out- The form questions are stored in a database in Google App Engine- So there are not static HTML pages, but rather, the form "gets created" when the user enters the URLHow can I achieve that? Thanks
Mike Brecht
A: 

I guess what I'm trying to achieve has to do with URL rewriting. I'd like to use nice URLs instead of parameters. Is it possible to use

www.domain.com/A1B2C3

instead of

www.domain.com/index.html?key=A1B2C3

I'm using Google App Engine and GWT. All in Java.

Thanks a lot

Mike Brecht
A: 

Try UrlRewriteFilter: http://tuckey.org/urlrewrite/ it is a plain ol' JEE filter, so it should work.

Jeroen
A: 

How about instead of www.domain.com/?key=A1B2C3 you use GWT's way of doing it: www.domain.com/#key=A1B2C3 you can do this via the History mechanism.

VogonPoet