Vaadin 7 URL 参数

7

我对 Vaadin 7 不太熟悉,无法理解这个问题。我尝试使用谷歌搜索,但没有找到有用的结果。

我的问题非常简单。如何在我的 UI 类中获取请求 URL 参数?

我的 URL 有一个参数:host/myvaadinapp/?code=erefdvdfgftf...

URL 参数来自用户登录后的 Facebook(OAuth),我需要在我的代码中处理 code URL 参数的值。

我尝试了以下操作:

    @PreserveOnRefresh
    public class AdminUi extends UI
    {
        @Override
        protected void init(VaadinRequest request)
        {
            ...
            System.out.println( request.getPathInfo());
            System.out.println( request.getRemoteHost());
            System.out.println( request.getParameterMap().size());
            System.out.println( request.getAttribute("code") );
            System.out.println( request.getParameter("code") );
            ...
        }
    }

结果: request.getAttribute("code"): null request.getParameter("code"): null

我意识到getParameterMap()有一个"v-loc"参数。但是如果我要使用这个变量,我需要编写一些代码来从这个变量中获取url参数:

v-loc: host/myvaadinapp/?code=erefdvdfgftf... theme: reindeer v-rtzo: -600 v-dston: false ...

我认为这不是一个很好的解决方案。

你能帮我在vaadin中如何获取原始url参数吗?

谢谢。


我的评论如下:

我尝试使用request.getParameter("code"),但它没有起作用。我不知道为什么。

我的原始url是:host/demoVaadinApp/?name=zzz

我使用了这段代码:

    public class Xxxx extends UI
    {
        /**
     * Main UI
     */
    @Override
    protected void init(VaadinRequest request)
    {
        String name = request.getParameter("name");
        if (name == null)
            {
            name = "Unknown";
        }

        setContent(new Label("Hello " + name));
        }
    }

我以嵌入式UI模式启动我的Vaadin应用程序。

我的web.xml文件内容:

<!-- ******************************************************************* -->
<!-- context parameters                                                  -->
<!-- ******************************************************************* -->
<context-param>
    <description>Vaadin production mode</description>
    <param-name>productionMode</param-name>
    <param-value>false</param-value>
</context-param>

<!-- ******************************************************************* -->
<!-- servlet definition                                                  -->
<!-- ******************************************************************* -->
<!-- +++++ Vaadin  servlet +++++++++++++++++++++++++++++++++++++++++++++ -->
<servlet>
    <servlet-name>VaadinServlet</servlet-name>
    <servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
    <init-param>
        <description>Vaadin UI class</description>
        <param-name>UI</param-name>
        <param-value>com.coupoholics.ui.admin.AdminUi</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>VaadinServlet</servlet-name>
    <url-pattern>/VAADIN/*</url-pattern>
</servlet-mapping>

<!-- ******************************************************************* -->
<!-- welcome file list definition                                        -->
<!-- ******************************************************************* -->
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

index.html:

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>

<body>
    <script type="text/javascript" src="./VAADIN/vaadinBootstrap.js"></script>
    <iframe tabindex="-1" id="__gwt_historyFrame" style="position: absolute; width: 0; height: 0; border: 0; overflow: hidden" src="javascript:false"></iframe>

                 <div id="content-body">
                    <!-- Optional placeholder for the loading indicator -->
                    <div class=" v-app-loading"></div>
                    <!-- Alternative fallback text -->
                    <noscript>You have to enable javascript in your browser to use this application.</noscript>
                </div>

    <!--  Initializing the UI -->
    <script type="text/javascript">
        //<![CDATA[
            if (!window.vaadin)
                alert("Failed to load the bootstrap JavaScript:" + "VAADIN/vaadinBootstrap.js");

            /* The UI Configuration */
            vaadin.initApplication("content-body", {
              "browserDetailsUrl": "VAADIN",
              "serviceUrl": "VAADIN",
              "widgetset": "com.vaadin.DefaultWidgetSet",
              "theme": "reindeer",
              "versionInfo": {"vaadinVersion": "7.0.5"},
              "vaadinDir": "VAADIN/",
              "heartbeatInterval": 300,
              "debug": true,
              "standalone": false,
              "authErrMsg": {
                  "message": "Take note of any unsaved data, "+
                             "and <u>click here<\/u> to continue.",
                  "caption": "Authentication problem"
              },
              "comErrMsg": {
                  "message": "Take note of any unsaved data, "+
                             "and <u>click here<\/u> to continue.",
                  "caption": "Communication problem"
              },
              "sessExpMsg": {
                  "message": "Take note of any unsaved data, "+
                             "and <u>click here<\/u> to continue.",
                  "caption": "Session Expired"
              }
         });//]]>
        </script>

</body>

我的结果是:你好,未知。

如果我不使用嵌入UI模式,一切都运行得非常顺利。

问题在哪里?


解决方案(感谢Leif Åstrand):

var urlParts = window.location.href.split("?");

var queryString = (urlParts.length == 1) ? "" : "?" + urlParts[1];

queryString = (urlParts.length == 0) ? "" : queryString.split("#")[0];

"browserDetailsUrl": "VAADIN" + queryString,

完整源代码:

    <!--  Initializing the UI -->
    <script type="text/javascript">
        var urlParts = window.location.href.split("?");
        var queryString = (urlParts.length == 1) ? "" : "?" + urlParts[1];
        queryString = (urlParts.length == 0) ? "" : queryString.split("#")[0];

        //<![CDATA[
            if (!window.vaadin)
                alert("Failed to load the bootstrap JavaScript:" + "VAADIN/vaadinBootstrap.js");

            /* The UI Configuration */
            vaadin.initApplication("content-body", {
              "browserDetailsUrl": "VAADIN" + queryString,
              "serviceUrl": "VAADIN",
                          ...
2个回答

11
这种行为的原因在于,UI.init被调用的请求并不总是与用于加载应用程序的相同URL。特别是当嵌入UI而不是作为独立应用程序提供时,情况就尤其如此。
默认情况下,UI init请求(在某些代码的某些部分中称为“浏览器详细信息请求”)会发送到与加载页面使用的相同URL。当嵌入时,这显然无法工作,因为没有Vaadin servlet映射到该URL,所以您必须定义一个browserDetailsUrl来告诉请求应该发送到哪里。
你观察到的v-loc在内部用于初始化由Page.getLocation()返回的值。
要解决你的特定问题,我建议以下其中一种选项:
1. 动态生成browserDetailsUrl以包括所需参数作为查询参数,例如"browserDetailsUrl": "VAADIN?code=erefdvdfgftf"。 2. 从Page.getLocation().getQuery()中提取值。这比使用你发现的v-loc略微好看,因为它不依赖于任何实现细节。

谢谢你的回答!我解决了,并且我在上面分享了我的解决方案。 - zappee

4

它应该与以下一起使用:request.getParameter("code")。


谢谢你的回答。是的,这段代码应该可以工作。这个维基页面描述得非常好: https://vaadin.com/wiki/-/wiki/Main/Using%20URI%20or%20parameters%20or%20screen%20size%20when%20initializing%20an%20application我花了很多时间来解决我的问题,我意识到我的问题是什么了。但我还无法解决它。我正在使用嵌入式 UI 模式,我想这就是问题所在。如果我不使用嵌入式 UI 模式,那么 request.getParameter("code") 就可以工作了。请阅读我上面的评论。谢谢。 - zappee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接