【问题标题】:gwt open a new window and post json datagwt 打开一个新窗口并发布 json 数据
【发布时间】:2014-05-30 14:17:49
【问题描述】:

我有一个有按钮的页面。我想单击按钮并打开一个新窗口。同时,我将向该请求发送 post Json 数据。我知道 gwt 有 get 方法来打开这样的新窗口:Window.open(url, "", "");但这不是post方法。 所以我尝试使用RequestBuilder。但是,这只是一个 Ajax 调用。我想要的是在我发送 post json 请求后,它会得到响应(响应包括 html div)并打开一个新窗口。有人知道吗?

【问题讨论】:

  • 我是根据我的记忆说的,但我想我已经看到使用 GQuery 完成了这一点。有机会我会尝试,但同时如果你没有任何路径,请自己尝试。 ide 是创建一个窗口,使用 JSNI 保存引用,然后尝试在该变量上注入数据。嗯,也许使用 JSNI 也应该很容易......快乐编码。

标签: gwt post httprequest


【解决方案1】:

尝试使用HttpSession 保存用户特定数据的状态,同一域内的任何页面都可以访问这些数据。

要遵循的步骤:

  • 只需创建一个 GWT RPC call 即可在 post 请求中将 JSON 字符串传递给服务器。
  • 在 RPC 调用的服务器端,将 JSON 字符串保存在 HTTP Session 中以供以后访问
  • 现在打开一个新页面(具有相同的域),该页面将从 HTTP 会话访问 JSON 字符串

包含 GWT PRC 类的完整示例代码

@RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
    public void setJSON(String json)throws IllegalArgumentException;
}

public interface GreetingServiceAsync {  
    void setJSON(String json, AsyncCallback<Void> callback);
}

public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService {
    @Override
    public void setJSON(String json) throws IllegalArgumentException {
        // set the JSON string in HTTP Session
        getThreadLocalRequest().getSession().setAttribute("json", json);
    }
}

web.xml:

<servlet>
    <servlet-name>greetServlet</servlet-name>
    <servlet-class>com.x.y.z.server.GreetingServiceImpl</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>greetServlet</servlet-name>
    <url-pattern>/modulename/greet</url-pattern>
</servlet-mapping>

入口点类:

GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
public class GWTProject implements EntryPoint {
    public void openNewWindow(final String json) {
        greetingService.setJSON(json, new AsyncCallback<Void>() {

            @Override
            public void onFailure(Throwable caught) {

            }

            @Override
            public void onSuccess(Void result) {
                // open any page within the same domain
                Window.open("/page.jsp", "", "");
            }
        });
 }

【讨论】:

    猜你喜欢
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多