GWT - Many Class not supported, but still works
Im making my own application by using GWT framework. I say that many Class are not supported (like Calendar, Connection, Statement, DriverManager, ResultSet, etc etc) but i can use it (they works).
Is it normal? :) And should they be replaced?
EXAMPLE :
when application start, it loads some FlowPanel with some buttons. When i click on the menu_login_button i do the asynch call
package org.sinfonet.client;
public class PageMenuLogin extends FlowPanel {
private PageCenter center;
public PageMenuLogin(PageCenter center) {
this.center=center;
designLogin();
}
public void designLogin() {
final InlineLabel menu_login_label1=new InlineLabel("Username");
menu_login_label1.setStyleName("menu_span");
this.add(menu_login_label1);
final TextBox menu_login_input1 = new TextBox();
menu_login_input1.setText("admin");
this.add(menu_login_input1);
final InlineLabel menu_login_label2=new InlineLabel("Password");
menu_login_label2.setStyleName("menu_span");
this.add(menu_login_label2);
final TextBox menu_login_input2 = new TextBox();
menu_login_input2.setText("pass");
this.add(menu_login_input2);
Button menu_login_button=new Button("Login");
this.add(menu_login_button);
PageMenuLogin.this.center.getContent().clear();
PageMenuLogin.this.center.getContent().add(new TitleContent("Homepage"));
PageMenuLogin.this.center.getContent().add(new Main());
final AsyncCallback<java.lang.Boolean> callCheckLogin = new AsyncCallback<java.lang.Boolean>() {
public void onSuccess(Boolean result) {
if(resu开发者_高级运维lt) {
designLogout(menu_login_input1.getText());
} else {
menu_err.setText("Username e password non validi");
}
}
public void onFailure(Throwable caught) {
menu_login_label1.setText("Comunicazione Fallita");
}
};
menu_login_button.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event) {
getService().checkLogin(menu_login_input1.getText(), menu_login_input2.getText(), callCheckLogin);
}
});
}
public void designLogout(String login_user) {
PageMenuLogin.this.center.getContent().add(new Profile(login_user));
}
public static GWTServiceAsync getService() {
return GWT.create(GWTService.class);
}
}
right now, under you can see the asynch call, where (as you said) i can put every kind of server calls (database, resultset, statement, ecc)
package org.sinfonet.server;
public class GWTServiceImpl extends RemoteServiceServlet implements GWTService {
public boolean 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 true;
}
// sconnessione al database
mydb.disconnetti();
return false;
}
}
if the function return true, i can load the Profile class (which is client side).
package org.sinfonet.client.profile;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import org.sinfonet.server.mgmt.Configuration;
import org.sinfonet.server.mgmt.Database;
public class Profile extends FlowPanel {
private String[] record;
private String value;
private TextBox field_1, field_2, field_3, field_4, field_5;
private TextArea field_A;
private FlowPanel profilo_1=new FlowPanel();
private FlowPanel profilo_2=new FlowPanel();
private FlowPanel profilo_3=new FlowPanel();
private FlowPanel pm_inbox=new FlowPanel();
private FlowPanel pm_outbox=new FlowPanel();
private FlowPanel pm_button=new FlowPanel();
public Profile(String value) {
// memorizzo nome utente
this.value=value;
// imposto div principale
this.setStyleName("content_span");
profilo_1.setStyleName("profilo_1");
profilo_2.setStyleName("profilo_2");
profilo_3.setStyleName("profilo_3");
pm_inbox.setStyleName("pm_cont1");
pm_outbox.setStyleName("pm_cont2");
pm_button.setStyleName("pm_but");
this.add(profilo_1);
this.add(profilo_2);
this.add(profilo_3);
designMainProfile();
}
public final void designMainProfile() {
Database mydb=Configuration.getDatabase();
mydb.connetti();
mydb.disconnetti();
}
}
If i well understand, i can't call Database functions when i load Profile (because its a client class).
So, how can i fix it? I do an asynch call when im loading Profile?
One thing is that you can add them to your code and your IDE doesn't show any problem, another thing is that your application can compile and be executed as a GWT application.
Take a look to RefJREEmulation documentation.
A couple of pointers first.
- Never include anything in *.server in a class in *.client.
- The opposite of the above is also true: never include anything in *.client in a class in *.server.
- The server end points of gwt rpc's are in *.server, and end in Impl. They can do server things.
- The client end points of gwt rpc's are in *.client, and one of them ends in Async. The other isn't appended with anything. These two files are interfaces, and therefore have no logic.
replace callCheckLogin in PageMenuLogin with the following:
public class PageMenuLogin extends FlowPanel {
...
public void designLogin() {
final InlineLabel menu_login_label1 = new InlineLabel("Username");
menu_login_label1.setStyleName("menu_span");
this.add(menu_login_label1);
final TextBox menu_login_input1 = new TextBox();
menu_login_input1.setText("admin");
this.add(menu_login_input1);
final InlineLabel menu_login_label2 = new InlineLabel("Password");
menu_login_label2.setStyleName("menu_span");
this.add(menu_login_label2);
final TextBox menu_login_input2 = new TextBox();
menu_login_input2.setText("pass");
this.add(menu_login_input2);
Button menu_login_button = new Button("Login");
this.add(menu_login_button);
this.center.getContent().clear();
this.center.getContent().add(new TitleContent("Homepage"));
this.center.getContent().add(new Main());
menu_login_button.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
getService().checkLogin(menu_login_input1.getText(),
menu_login_input2.getText(),
new AsyncCallback<Boolean>() {
@Override
public void onFailure(Throwable caught) {
menu_login_label1
.setText("Comunicazione Fallita");
}
@Override
public void onSuccess(Boolean result) {
if (result) {
designLogout(menu_login_input1.getText());
} else {
menu_err.setText("Username e password non validi");
}
}
});
}
});
}
replace designMainProfile in Profile with the following:
public final void designMainProfile() {
PageMenuLogin.getService().foo(new AsyncCallback<Void>() {
@Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}
@Override
public void onSuccess(Void result) {
// TODO Auto-generated method stub
}
});
}
In GWTServiceImpl, add the following:
@Override
public void foo() {
Database mydb = Configuration.getDatabase();
mydb.connetti();
mydb.disconnetti();
}
In GWTService, add the following:
void foo();
In GWTServiceAsync, add the following:
void foo(AsyncCallback<Void> asyncCallback);
If you don't have either of the last two classes, let me know, and we'll back up a couple steps.
精彩评论