开发者

In GWT I'd like to load the main page on www.domain.com and a dynamic page on www.domain.com/xyz - I just can't make it work

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 


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 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. =)


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜