我通过使用GWT框架在RPC模式中创建了自己的方法。现在,我需要添加另一种方法。
所以,我为RPC的每个部分编写了这段代码:
package org.sinfonet.client;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
@RemoteServiceRelativePath("gwtservice")
public interface GWTService extends RemoteService {
public String checkLogin(String nickname, String password);
public boolean anotherFunction(String nickname);
}
#########################################################
package org.sinfonet.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface GWTServiceAsync {
public void checkLogin(String nickname, String password, AsyncCallback<String> callback);
public void anotherFunction(String nickname, AsyncCallback<java.lang.Boolean> asyncCallback);
}
#########################################################
package org.sinfonet.server;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.util.ArrayList;
import org.sinfonet.client.GWTService;
import org.sinfonet.mgmt.Configuration;
import org.sinfonet.mgmt.Database;
public class GWTServiceImpl extends RemoteServiceServlet implements GWTService {
public String checkLogin(String nickname, String password) {
Database mydb=Configuration.getDatabase();
mydb.connetti();
// faccio md5 ed escape
String log_check_user=nickname;
String log_check_pass=password;
// controllo che l'utente esista
ArrayList<String[]> db_result=null;
db_result=mydb.selectQuery("SELECT nickname FROM users WHERE nickname='"+log_check_user+"' AND password='"+log_check_pass+"'");
if(db_result.size()!=0) {
return "YES";
}
// sconnessione al database
mydb.disconnetti();
return "NO";
}
public boolean anotherFunction(String nickname) {
// somethings others
return true;
}
}
#########################################################
final AsyncCallback<java.lang.Boolean> callCheckLogin = new AsyncCallback<java.lang.Boolean>() {
public void onSuccess(boolean result) {
if(result) {
designLogout(menu_login_label1.getText());
} else {
menu_err.setText("Username e password non validi");
}
}
};
// Listen for the button clicks
menu_login_button.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
// Make remote call. Control flow will continue immediately and later
// 'callback' will be invoked when the RPC completes.
getService().anotherFunction(menu_login_input1.getText(), callCheckLogin);
}
});
正如你所看到的,我添加了anotherFunction()方法(boolean),但是Netbeans告诉我,我需要实现关于allCheckLogin的所有abracts方法,但我不会这样做:)我该如何解决这个问题呢? / p>
答案 0 :(得分:5)
所以Netbeans抱怨缺少onFailure
方法,对吧?如果您不想每次都实现该方法,请自己编写一个抽象类,如:
public abstract class BaseAsyncCallback<T> implements AsyncCallback<T> {
@Override
public void onFailure(Throwable caught) {
// Perform generic failure handling
}
}
然后您可以将代码更改为:
final AsyncCallback<java.lang.Boolean> callCheckLogin =
new BaseAsyncCallback<java.lang.Boolean>() {
public void onSuccess(java.lang.Boolean result) {
...
}
};
现在您不再需要实现onFailure
,除非您需要执行其他错误处理。