问题
I have a GWT application which when I deploy locally homepage after running the application is localhost:8888/myapp.html but when we deploy it to the server, after the application running its homepage is 107.20.239.198:8080/myapp/myapp.html.
Now all the relating paths my application fail. Places where I require the exact path, I cant get that. As now the path is different. For example when I click in a particular widget in my application -> It redirects to the path '/# + "place no.1" + "place no.2" '
But now it fails when deployed on the server as now it requires the path -> '/myapp/# + "place no.1" + "place no.2" '.
So how can I make it generic, so that I can handle both the conditions.
Can anyone help me with this ?
P.S. I hope I am able to express my problem well, else you can comment it so that I can explain it better.
回答1:
From your local url as you mentioned above, it looks like you are running it on AppEngine's server. Better you deploy your application on Tomcat first on your local. And make sure everything is working fine. These are the few methods which you can use to get the relative/absolute urls when your application starts up.
GWT.getHostPageBaseURL(); GWT.getModuleBaseURL(); GWT.getModuleName();
Hope it helps.
回答2:
You should use relative paths in your application, relative to your context. Using request.getContextPath() you will be able to obtain the correct context path. But if you will use "/" in your code, you will always refer to your context path, regardless on what is the context. So to create a link to a internal page you will use /internal.html. On your local machine will be transformed to localhost:8888/internal.html but on server it will be: 107.20.239.198:8080/myapp/internal.html.
回答3:
As mentioned by @Anuj Kulkarni you should really use relative path because otherwise you will always get these Problems. But I had 1 particular Problem where I couldn't do this. So if you like you can use:
if (!GWT.isProdMode())
Here you can define different urls for your development environment and deployment. But if you use this more than once in your code then it does mean you are doing something wrong.
来源:https://stackoverflow.com/questions/12209121/gwt-getting-path-of-the-application