【问题标题】:GWT - making GET requestsGWT - 发出 GET 请求
【发布时间】:2018-11-03 00:11:21
【问题描述】:

简单的问题。我需要在 GWT 中发出 GET 请求重定向到新页面,但我找不到正确的 API。

有吗?我应该自己简单地形成 URL,然后做Window.Location.replace?

(原因是我希望我的搜索页面是可链接的)

谢谢。

(很抱歉一开始我的问题不够清楚)

【问题讨论】:

  • 我想我的问题可以归结为:如何使用 GWT 拥有多个页面?
  • “多页”是什么意思?

标签: gwt


【解决方案1】:

看看 http://library.igcar.gov.in/readit2007/tutori/tools/gwt-windows-1.4.10/doc/html/com.google.gwt.http.client.html

public class GetExample implements EntryPoint {

    public static final int STATUS_CODE_OK = 200;

    public static void doGet(String url) {
        RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

        try {
            Request response = builder.sendRequest(null, new RequestCallback() {
                public void onError(Request request, Throwable exception) {
                    // Code omitted for clarity
                }

                public void onResponseReceived(Request request, Response response) {
                    // Code omitted for clarity
                }
            });

        } catch (RequestException e) {
            // Code omitted for clarity
        }
    }

    public void onModuleLoad() {
        doGet("/");
    }
}

【讨论】:

    【解决方案2】:

    GWT 不禁止您使用常规 servlet。

    您可以在“web.xml”文件中声明一个 servlet:

    <servlet>
        <servlet-name>myServlet</servlet-name>
        <servlet-class>org.myapp.server.MyServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>myServlet</servlet-name>
        <url-pattern>/myurl/*</url-pattern>
    </servlet-mapping>
    

    然后你就可以实现你的Servlet了:

    public class MyServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws 
            IOException {
    
          // your code here
    
    }
    
    }
    

    【讨论】:

      【解决方案3】:

      使用 Window.Location.replace 重定向到新页面。

      应该使用历史机制来处理多个页面。

      【讨论】:

        【解决方案4】:

        如果你打开一个单独的窗口,这很容易:

        Window.open(url, windowName, "resizable=yes,scrollbars=yes,menubar=yes,location=yes,status=yes");
        

        否则,请按照 Silfverstrom 的建议使用 RequestBuilder

        【讨论】:

          【解决方案5】:

          answer from ivo 类似。我可以在我的 GWT todomvc 框架中使用 filter mapping 而不是 web.xml 文件中的 servlet-mapping 来执行此操作。

          <?xml version="1.0" encoding="UTF-8"?>
          <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                   version="2.5"
                   xmlns="http://java.sun.com/xml/ns/javaee">
          
            <filter>
              <filter-name>guiceFilter</filter-name>
              <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
            </filter>
          
            <filter-mapping>
              <filter-name>guiceFilter</filter-name>
              <url-pattern>/myurl/*</url-pattern>
            </filter-mapping>
          
            <listener>
              <listener-class>com.todomvc.server.ToDoServerInjector</listener-class>
            </listener>
          
            <!-- Default page to serve -->
            <welcome-file-list>
              <welcome-file>GwtGaeChannelToDo.html</welcome-file>
            </welcome-file-list>
          
          </web-app>
          

          【讨论】:

            猜你喜欢
            • 2014-08-15
            • 1970-01-01
            • 1970-01-01
            • 2021-12-09
            • 1970-01-01
            • 1970-01-01
            • 2016-11-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多