问题描述:

I'm building a gwt app with gmaps. When I call a service in a button click, the console in eclipse shows me the following:

PS: The service is only for testing purposes. I've not implemented the real service!

[WARN] 404 - POST /findmeagasstationweb/inserirpostorpc (127.0.0.1) 1422 bytes

Request headers

Host: 127.0.0.1:8888

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: pt-pt,pt;q=0.8,en;q=0.5,en-us;q=0.3

Accept-Encoding: gzip, deflate

Connection: keep-alive

Referer: http://127.0.0.1:8888/FindMeAGasStationWeb.html?gwt.codesvr=127.0.0.1:9997

X-GWT-Permutation: HostedMode

X-GWT-Module-Base: http://127.0.0.1:8888/findmeagasstationweb/

Content-Type: text/x-gwt-rpc; charset=utf-8

Content-Length: 225

Pragma: no-cache

Cache-Control: no-cache

Response headers

Content-Type: text/html; charset=iso-8859-1

Content-Length: 1422

The following code of my app:

Defined Servlets

<!-- Servlets -->

<servlet>

<servlet-name>insereServlet</servlet-name>

<servlet-class>com.pz.findmeagasstationweb.server.InsereNovoPostoImpl</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>insereServlet</servlet-name>

<url-pattern>/inserirpostorpc/</url-pattern>

</servlet-mapping>

Class FindMeAGasStationWeb

package com.pz.findmeagasstationweb.client;

//imports are omitted

public class FindMeAGasStationWeb implements EntryPoint {

Label latLabel = new Label("Latitude");

TextBox latBox = new TextBox();

Label longLabel = new Label("Longitude");

TextBox longBox = new TextBox();

Label nameLabel = new Label("Nome do Posto");

TextBox nameBox = new TextBox();

Label descLabel = new Label("Descricao");

TextBox descBox = new TextBox();

Label sc95Label = new Label("Preco Gasolina 95");

TextBox sc95Box = new TextBox();

Label sc98Label = new Label("Preco Gasolina 98");

TextBox sc98Box = new TextBox();

Label gasoleoLabel = new Label("Preco Gasoleo");

TextBox gasoleoBox = new TextBox();

Button inserir = new Button("Inserir Posto");

Grid grid = new Grid(2, 8);

public int controlo;

public void onModuleLoad() {

final AsyncCallback<String> callback = new AsyncCallback<String>(){

public void onFailure(Throwable caught){

Window.alert("Falha a inserir os dados."+caught);

}

public void onSuccess(String result){

Window.alert(""+result);

}

};

grid.setWidget(0, 0, latLabel);

grid.setWidget(0, 1, latBox);

grid.setWidget(1, 0, longLabel);

grid.setWidget(1, 1, longBox);

grid.setWidget(0, 2, nameLabel);

grid.setWidget(0, 3, nameBox);

grid.setWidget(1, 2, descLabel);

grid.setWidget(1, 3, descBox);

grid.setWidget(0, 4, sc95Label);

grid.setWidget(0, 5, sc95Box);

grid.setWidget(1, 4, sc98Label);

grid.setWidget(1, 5, sc98Box);

grid.setWidget(0, 6, gasoleoLabel);

grid.setWidget(0, 7, gasoleoBox);

grid.setWidget(1, 7, inserir);

latBox.setEnabled(false);

longBox.setEnabled(false);

RootPanel.get().add(grid);

inserir.addClickHandler(new ClickHandler() {

public void onClick(ClickEvent event) {

//.inserirPosto(latBox.getText(), longBox.getText(), nameBox.getText(), descBox.getText(),

try{

InsereNovoPostoAsync getServico = (InsereNovoPostoAsync) GWT.create(InsereNovoPosto.class);

ServiceDefTarget serviceDef = (ServiceDefTarget) getServico;

serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()

+ "inserirpostorpc");

getServico.inserirPosto("1","2", "a", "b", "1", "2", "3", callback);

}

catch(Exception ex){

Window.alert(""+ex);

}

//getServico().inserirPosto(latBox.getText(), longBox.getText(), nameBox.getText(), descBox.getText(), sc95Box.getText(), sc98Box.getText(), gasoleoBox.getText(), callback);

/*

(latBox.getText(), longBox.getText(), nameBox.getText(),

descBox.getText(), sc95Box.getText(), sc98Box.getText(),

gasoleoBox.getText(), callback);

/*RootPanel.get().add(

new Label("Thanks for your submission."));

Window.alert("Submit name=" + nameBox.getText()

+ "\naddress=" + addrBox.getText() + "\nphone="

+ phoneBox.getText());*/

}

});

MapWidget mapWiget = new MapWidget(LatLng.newInstance(48.136559, 11.576318), 13);

mapWiget.setSize("1024px", "500px");

mapWiget.addControl(new SmallMapControl());

mapWiget.addControl(new MapTypeControl());

mapWiget.addMapClickHandler(new MapClickHandler() {

public void onClick(MapClickEvent e) {

MapWidget sender = e.getSender();

Overlay overlay = e.getOverlay();

LatLng point = e.getLatLng();

if (overlay != null && overlay instanceof Marker) {

sender.removeOverlay(overlay);

latBox.setText("");

longBox.setText("");

controlo = 0;

} else {

if(controlo==0){

String a[] = point.toString().split(",");

String b = a[0].toString();

b = b.substring(1);

String c = a[1].toString();

c = c.substring(0, c.length()-1);

latBox.setText(""+b);

longBox.setText(""+c);

sender.addOverlay(new Marker(point));

controlo=1;

}

}

}

});

RootPanel.get().add(mapWiget);

}

}

Service:

InsereNovoPosto

package com.pz.findmeagasstationweb.client;

import com.google.gwt.user.client.rpc.RemoteService;

import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

@RemoteServiceRelativePath("inserirpostorpc")

public interface InsereNovoPosto extends RemoteService {

public String inserirPosto(String lat, String longi, String nome,

String descricao, String sc95, String sc98, String gasoleo);

}

InsereNovoPostoAsync

package com.pz.findmeagasstationweb.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface InsereNovoPostoAsync {

public void inserirPosto(String lat, String longi, String nome,

String descricao, String sc95, String sc98, String gasoleo, AsyncCallback<String> callback);

}

InsereNovoPostoImpl

package com.pz.findmeagasstationweb.server;

import com.pz.findmeagasstationweb.client.InsereNovoPosto;

import com.google.gwt.user.client.Window;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")

public class InsereNovoPostoImpl extends RemoteServiceServlet implements InsereNovoPosto {

@Override

public String inserirPosto(String lat, String longi, String nome,

String descricao, String sc95, String sc98, String gasoleo) {

Window.alert(""+lat+"-"+longi+"-"+nome+"-"+descricao+"-"+sc95+"-"+sc98+"-"+gasoleo);

return nome;

}

}

findmeagasstationweb.gwt.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--

When updating your version of GWT, you should also update this DTD reference,

so that your app can take advantage of the latest GWT module capabilities.

-->

<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.5.0//EN"

"http://google-web-toolkit.googlecode.com/svn/tags/2.5.0/distro-source/core/src/gwt-module.dtd">

<module rename-to='findmeagasstationweb'>

<!-- Inherit the core Web Toolkit stuff. -->

<inherits name='com.google.gwt.user.User'/>

<!-- Inherit the default GWT style sheet. You can change -->

<!-- the theme of your GWT application by uncommenting -->

<!-- any one of the following lines. -->

<inherits name='com.google.gwt.user.theme.clean.Clean'/>

<!-- <inherits name='com.google.gwt.user.theme.standard.Standard'/> -->

<!-- <inherits name='com.google.gwt.user.theme.chrome.Chrome'/> -->

<!-- <inherits name='com.google.gwt.user.theme.dark.Dark'/> -->

<!-- Other module inherits -->

<inherits name='com.google.gwt.maps.GoogleMaps'/>

<script src='http://maps.google.com/maps?gwt=1&amp;file=api&amp;v=2' />

<!-- Specify the app entry point class. -->

<entry-point class='com.pz.findmeagasstationweb.client.FindMeAGasStationWeb'/>

<!-- Specify the paths for translatable code -->

<source path='client'/>

<source path='shared'/>

</module>

Any idea what's going wrong? Any help would be great... Thanks in advanced, and sorry for my bad english !

网友答案:

It is url pattern problem in web.xml, Please change it.

<url-pattern>/findmeagasstationweb/inserirpostorpc/</url-pattern>

To call RPC, you have written code like

  serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()
                        + "inserirpostorpc");

Here GWT.getModuleBaseURL() is "project module name" + inserirpostorpc.

网友答案:

You have mismatch w.r.t to url to which you are making the RPC request.

1) Your URL pattern in your rpc creation

serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()
                        + "inserirpostorpc");

OR

2) Your URL pattern in web.xml

<url-pattern>/inserirpostorpc/</url-pattern>

You can change either of them to avoid the 404 error. Drop the GWT.getModuleBaseURL() or change URL pattern in web.xml to have /findmeagasstationweb/inserirpostorpc/

Just maintain uniformity across all rpc creation and web.xml mapping.

网友答案:

Here the complete login rpc :

onModuleLoad:

private final String loginRpc="loginrpc";
private final LoginServiceAsync loginService = GWT.create(LoginService.class);
((ServiceDefTarget) loginService).setServiceEntryPoint(loginRpc);

    loginService.LoginRPC(
                new AsyncCallback<obj>() {

                    @Override
                    public void onFailure(Throwable caught) {
                        caught.printStackTrace();

                    }
                     @Override
                    public void onSuccess(Object obj) { 
//TODO
                    }}
                });

Web.xml

 <servlet>
    <servlet-name>loginrpc</servlet-name>
    <servlet-class>com.my.test.LoginServiceImpl</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>loginrpc</servlet-name>
    <url-pattern>/loginrpc</url-pattern>
  </servlet-mapping>
相关阅读:
Top