4

私はGWTを初めて使用します...Webアプリにセッションを実装したい基本的に、セッションはボタンをクリックして開始(イベントを処理)し、別のボタンをクリックして終了(他のイベントを処理)する必要があります)。それが可能だ?

ステップバイステップでそれを行う方法は?

このコードは大丈夫ですか?:

メイン(クライアント側):

Button b1 = new Button("b1");
b1.addClickHandler(new ClickHandler) {
      public voin onClick(){
              ...
             rpc.setSession(callback); //rpc call the service...

   }
}

Button b2 = new Button("b2");
b1.addClickHandler(new ClickHandler) {
      public voin onClick(){
              ...
             rpc.exitSession(callback);

   }
}

// ------------------------------------------------ ------------------------------------

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

public interface MySession extends RemoteService {

    public void setSession();

    public void exitSession();
}

// ------------------------------------------------ ------------------------------------

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

public interface MySessionAsync {

    void setSession(AsyncCallback<Void> callback);

    void exitSession(AsyncCallback<Void> callback);

}

// ------------------------------------------------ ------------------------------------

import de.vogella.gwt.helloworld.client.MySession;

public class MySessionImpl extends RemoteServiceServlet implements MySession {

    HttpSession httpSession;
    @Override

    public void setSession() {
        httpSession = getThreadLocalRequest().getSession();

        httpSession = this.getThreadLocalRequest().getSession();
        httpSession.setAttribute("b", "1");

    }

    @Override
    public void exitSession() {
          httpSession = this.getThreadLocalRequest().getSession();
          httpSession.invalidate(); // kill session     
    }

}

私がしていることは、Webアプリケーションを別のWebページに接続することです。ブラウザの戻るボタンをクリックすると、セッションがまだ残っている状態でWebアプリに戻ります...どうすればよいですか?

私の問題が何であるかをよく説明できたと思います...

*****新しい問題***:**

私はそうしようとしました...

---クライアント側....メイン:

        MyServiceAsync service = (MyServiceAsync) GWT.create(MyService.class);
        ServiceDefTarget serviceDef = (ServiceDefTarget) service;
        serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL()+ "rpc");

        boolean b=false;;

        b=service.checkSession(new AsyncCallback<Boolean>() {

            @Override
            public void onSuccess(Boolean result) {
                // here is the result
                if(result){
                        // yes the attribute was setted
                   }
            }

            @Override
            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());

            }
        });

        if (b==false){ // se non esiste una sessione
        RootPanel.get().add(verticalPanel); 
        RootPanel.get().add(etichetta); 
        RootPanel.get().add(nameField);
        RootPanel.get().add(sendButton);
        RootPanel.get().add(horizontalPanel); 

        }

        else{ //esiste già una sessione attiva (pagina da loggato)
            welcome.setText("Ciao "+userCorrect+"!!");
            RootPanel.get().add(verticalPanelLog);
            RootPanel.get().add(etichetta);
            RootPanel.get().add(nameField);
            RootPanel.get().add(cercaLog);
            RootPanel.get().add(horizontalPanel);
        }

////////////////////////////////////////////////// //////////////////////

public interface MyServiceAsync {
...

    void exitSession(AsyncCallback<Void> callback);

    void setSession(AsyncCallback<Void> callback);

    void checkSession(AsyncCallback<Boolean> callback); //error!!

////////////////////////////////////////////////// //////////////////////

public interface MyService extends RemoteService {
    /.....

    public void setSession();

    public void exitSession();

    public boolean checkSession();

////////////////////////////////////////////////// //////////////////////

サーバ側:

public boolean checkSession() {

      httpSession = this.getThreadLocalRequest().getSession();

      //se la sessione esiste già
      if (httpSession.getAttribute("b")!= null){
          return true;
      }
      else{ .
          return false;
      }
4

1 に答える 1

11

GWT のセッションは、サーブレットのセッションに似ています。違いは、呼び出すサーブレットにあります
HTTPSession session = request.getSession();

gwt では、

HttpServletRequest request = this.getThreadLocalRequest();リクエストを取得するために呼び出してから、もう一度呼び出しますrequest.getSession();

あなたの状況では、ボタンをクリックしたときにRPCを呼び出し、前のコードでサーバー上のセッションを管理し、別のボタンをクリックしてセッションを無効にするときに別のRPCを呼び出す必要があります。これが例です。

Button b1 = new Button("b1");
b1.addClickHandler(new ClickHandler) {
    // call RPC and 
   // session = this.getThreadLocalRequest().getSession();
  // session.setAtribute("b", "1");
}


Button b2 = new Button("b2");
b1.addClickHandler(new ClickHandler) {
    // call RPC and 
   // session = this.getThreadLocalRequest().getSession();
  // session.invalidate(); // kill session
}

このリンクは、GWT でのサーブレット セッションの使用に役立つ場合があります。

編集 :

セッションかどうかをテストしたい場合は、isExist()これを試してください

インターフェイスboolean test(String attr);
に追加 .async にvoid test(String attr, AsyncCallback<Boolean> callback);
追加.impl に追加

@Override
public boolean test(String attr) {
    return session.getAttribute(attr) != null;
}

そしてただ電話する

Rpc.test(attribute, new AsyncCallback<Boolean>() {

        @Override
        public void onSuccess(Boolean result) {
            // here is the result
            if(result){
                    // yes the attribute was setted
               }
        }

        @Override
        public void onFailure(Throwable caught) {
            Window.alert(caught.getMessage());

        }
    });
于 2011-01-15T09:16:12.503 に答える