- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是 GWT 的新手...我想在我的 Web 应用程序中实现 session 基本上我希望 session 在单击一个按钮(处理一个事件)时开始并在单击另一个按钮(其他处理一个事件)时结束。可能吗?
如何一步一步来?
这段代码可以吗?:
主要(客户端):
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 应用程序并且 session 仍然有效......我该怎么做?
我希望我已经很好地解释了我的问题......
*****新问题***:**
我试过这样做......
---客户端....主要:
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;
}
最佳答案
GWT 中的session 类似于servlet 中的session。区别在于您调用的 servlet
HTTPSession session = request.getSession();
在 gwt 中你调用
HttpServletRequest request = this.getThreadLocalRequest();
获得请求然后再次request.getSession();
在您的情况下,您应该在单击按钮时调用 RPC 并在服务器上管理 session 之前的代码,并在单击另一个按钮并使 session 无效时调用另一个 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
}
此链接可能对您有帮助Using Servlet Sessions in GWT
编辑:
如果要测试session是否为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());
}
});
关于java - 使用 GWT 的 HTTPSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4698658/
我想知道 GWT、GWT-RPC、EXT-GWT 和 Smart GWT 之间的区别。目前,我设法借了有关 GWT 的书籍,据我所知,它只是一个旨在促进快速高效的 Ajax(异步 JavaScript
不,这不是问如何让 Guava 在 GWT 中工作,因为我已经让它工作了。 我的问题是,当我执行继承时 我希望在命名空间 com.google.common.collect 中找到一个文件 Coll
Ext GWT 和 GWT-Ext 之间有区别吗?因为我在浏览 Ext GWT 时看到了这个页面 http://gwt-ext.com/demo/ .有什么帮助吗? 最佳答案 ExtGWT 由开发 E
调查gwt-dispatch之后和 Google Wave I/O presentation (Best practices) ( video here ),我想知道为什么官方 GWT 版本 (2.0
我在我的应用程序中使用带有 ext 的 GWT 2.0.3。该项目不再处于积极开发状态并且已被 Smart GWT 取代。我正在为此应用程序使用 HMVC 模式。现在使用现有的 GWT 2.0.3 和
当我尝试在 Windows Vista 上为 ie 8 使用 GWT 开发模式插件时,我不断看到安装插件的提示。 运行插件后我仍然总是看到这个页面。有谁知道如何解决这样的问题? 最佳答案 看这个:Ca
我正在尝试对 GWT RPC 序列化策略进行一些背景阅读,发现 GWT 在编译后将 *.gwt.rpc 文件中的可序列化类型列入白名单。 以下是我的应用程序中生成的一个此类 .gwt.rpc 文件的摘
如果 Enum 实现了 java.io.Serializable,我无法将它序列化为 GWT。它会成功编译 GWT,但在运行时,我会感到害怕: Type 'com....security..Admin
是否有可以与 GWT 一起使用的进度条小部件,还是必须自己制作?我尝试在 google-web-toolkit-incubator、gwtupload 和 upload4gwt 中使用进度条,但没有任
由于 Javadoc 没有说明使用 com.google.gwt.core.shared.GWT 的原因,它似乎包含 com.google.gwt.core.client.GWT 的功能子集,前者存在
我必须在 gwt 中创建一个图像按钮,它使用三个图像(左侧图像、中心拉伸(stretch)图像和右侧图像)。左侧图像和右侧图像具有圆角。中心图像想要拉伸(stretch)取决于按钮标题大小.创建的 I
我正在尝试在 GWT 的垂直面板中设置 align 属性,如下所示: vpanel = new VerticalPanel(); vPanel.setHorizontalAlignment(HasHo
我想在 GWT 中添加可编辑的组合框,请告诉我解决方案? 最佳答案 试试 Advanced GWT Components , 具体来说 org.gwt.advanced.client.ui.widge
我在使用 GWT Designer 配置 GXT 时遇到问题。我拥有 Eclipse、GWT 插件和 GXT 的所有新版本,但无法将 GXT 配置为与 GWT Designer 一起使用。我设置了我的
我们有一个当前使用 Capistrano 部署的应用程序。该应用程序使用 php 作为后端,使用 GWT 作为前端。 我已经设法通过 Ant 文件编译 GWT,但想用自定义 Capistrano 任务
这应该很简单,但不知何故我找不到在 GWT 中创建简单超链接的方法。基本上,我想在用户点击某些东西时加载另一个页面。 Hyperlink似乎仅指向内部 GWT 应用程序状态。我想我可以把链接放在 HT
在 GWT 界面中哪个更好,使用带有 javacode 的普通 MVP,还是 UiBinder?从性能、编辑、简单性方面。 最佳答案 这就是Google says : Besides being a
GWT 2.5.0 开发模式 我在下面对文件上传做了一个简单的测试, startupUrl: http://127.0.0.1:8888/UploadTest.html?gwt.codesvr=127
我需要创建一个SuggestBox,在按下时将显示所有选项 Enter键。 我已经写了以下实现,看来是 工作正常。 我希望有人审查我的实现情况,并让我知道 在任何特定情况下都会引起问题。 另外,要传递
在哪里可以找到有关 GWT 和 GWT-Ext 延迟加载的更多信息? 最佳答案 快速谷歌搜索显示 nice blog entry由 GWT 团队提供。 关于 GWT-Ext,我无话可说,但无论采用何种
我是一名优秀的程序员,十分优秀!