vaadin 7 URL 参数 [英] vaadin 7 URL parameter

查看:27
本文介绍了vaadin 7 URL 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 vaadin 7 不太熟悉,我无法弄清楚这一点.我尝试使用谷歌但没有有用的结果.

我的问题很简单.如何在我的 UI 类中获取请求 url 参数?

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

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

我尝试这样做:

 @PreserveOnRefresh公共类 AdminUi 扩展了 UI{@覆盖protected void init(VaadinRequest 请求){...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"): nullrequest.getParameter("code"): null

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

v-loc:host/myvaadinapp/?code=erefdvdfgftf...主题:驯鹿v-rtzo:-600v-dston: 假...

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

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

谢谢.

<小时>

我的评论是:

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

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

我使用了这个代码:

 公共类 Xxxx 扩展 UI{/*** 主界面*/@覆盖protected void init(VaadinRequest 请求){String name = request.getParameter("name");如果(名称 == 空){name = "未知";}setContent(new Label("Hello " + name));}}

我在嵌入 UI 模式下启动我的 vaadin 应用程序.

我的 web.xml 文件的内容:

<!-- 上下文参数--><!-- ********************************************************************** --><上下文参数><description>Vaadin生产模式</description><param-name>productionMode</param-name><param-value>false</param-value></context-param><!-- ********************************************************************** --><!-- servlet定义--><!-- ********************************************************************** --><!-- +++++ Vaadin servlet +++++++++++++++++++++++++++++++++++++++++++ --><小服务程序><servlet-name>VaadinServlet</servlet-name><servlet-class>com.vaadin.server.VaadinServlet</servlet-class><init-param><description>Vaadin UI 类</description><param-name>UI</param-name><param-value>com.coupoholics.ui.admin.AdminUi</param-value></init-param><servlet-mapping><servlet-name>VaadinServlet</servlet-name><url-pattern>/VAADIN/*</url-pattern></servlet-mapping><!-- ********************************************************************** --><!-- 欢迎文件列表定义--><!-- ********************************************************************** --><欢迎文件列表><welcome-file>index.html</welcome-file></welcome-file-list>

index.html:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><身体><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"><!-- 加载指示器的可选占位符--><div class=" v-app-loading"></div><!-- 替代后备文本--><noscript>您必须在浏览器中启用 javascript 才能使用此应用程序.</noscript>

<!-- 初始化用户界面--><script type="text/javascript">//<![CDATA[如果(!window.vaadin)alert("加载引导 JavaScript 失败:" + "VAADIN/vaadinBootstrap.js");/* 界面配置 */vaadin.initApplication("content-body", {"browserDetailsUrl": "VAADIN","serviceUrl": "VAADIN","widgetset": "com.vaadin.DefaultWidgetSet","theme": "驯鹿","versionInfo": {"vaadinVersion": "7.0.5"},"vaadinDir": "VAADIN/",心跳间隔":300,调试":真的,独立":假,authErrMsg":{"message": "记下任何未保存的数据,"+"和<u>点击这里<\/u>继续.","caption": "身份验证问题"},comErrMsg":{"message": "记下任何未保存的数据,"+"和<u>点击这里<\/u>继续.","caption": "通讯问题"},sessExpMsg":{"message": "记下任何未保存的数据,"+"和<u>点击这里<\/u>继续.","caption": "会话已过期"}});//]]>

我的结果是:你好未知.

如果我不使用嵌入 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,

完整源代码:

 <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[如果(!window.vaadin)alert("加载引导 JavaScript 失败:" + "VAADIN/vaadinBootstrap.js");/* 界面配置 */vaadin.initApplication("content-body", {"browserDetailsUrl": "VAADIN" + queryString,"serviceUrl": "VAADIN",...

解决方案

这种行为的原因是 UI.init 是从一个请求中调用的用于加载应用程序.嵌入 UI 而不是将其作为独立应用程序提供时尤其如此.

UI 初始化请求(在代码的某些部分称为浏览器详细信息请求")默认发送到用于加载页面的相同 URL.这在嵌入时显然不起作用,因为没有 Vaadin servlet 映射到该 URL,因此您必须定义一个 browserDetailsUrl 来告诉应该将请求发送到哪里.

您观察到的

v-loc 在内部用于初始化 Page.getLocation() 返回的值.

为了解决您的特定问题,我会建议以下选项之一:

  1. 动态生成 browserDetailsUrl 以包含所需的参数作为查询参数,例如"browserDetailsUrl": "VAADIN?code=erefdvdfgftf".
  2. Page.getLocation().getQuery() 中提取值.这比使用您发现的 v-loc 稍微不那么丑陋,因为它不依赖于任何实现细节.

I am not too familiar with vaadin 7 and I can not figure out this. I tried to use google but no useful result.

My question is very simple. How can I get the request url parameters in my UI class?

My url has a parameter: host/myvaadinapp/?code=erefdvdfgftf...

The url parameter comes from facebook after user logged in (OAuth) and I need to handle the value of code url parameter in my code.

I tried to do these:

    @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") );
            ...
        }
    }

Result: request.getAttribute("code"): null request.getParameter("code"): null

I realized that getParameterMap() has a "v-loc" parameter. But if I use it variable I have to write some code to get url parameter from this variable:

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

I think, it is not too nice solution.

Could you help me how can I get the original url parameters in vaadin?

Thank you.


My comment are:

I tried to use request.getParameter("code") but it was not working. I do not know why.

My original url was: host/demoVaadinApp/?name=zzz

I used this code:

    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));
        }
    }

I start my vaadin application in Embedding UI mode.

Content of my web.xml file:

<!-- ******************************************************************* -->
<!-- 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>

My result is: Hello Unknown.

If I do not use Embedding UI mode than everything is working very well.

Where is the problem?


The solution (thank you for Leif Åstrand):

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

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

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

and

"browserDetailsUrl": "VAADIN" + queryString,

Full source code:

    <!--  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",
                          ...

解决方案

The reason for this behavior is that UI.init is invoked from a request that is not always made to the same URL that was used to load the application. This is especially the case when embedding the UI instead of serving it as a standalone application.

The UI init request (named "browser details request" in some parts of the code) is by default sent to the same URL that was used to load the page. This does obviously not work when embedding as there is not Vaadin servlet mapped to that URL, so instead you have to define a browserDetailsUrl to to tell where the request should be sent.

v-loc that you observe is internally used to initialize the value returned by Page.getLocation().

To solve your particular problem, I would suggest one of these options:

  1. Dyncamically generate browserDetailsUrl to include the desired parameter as a query parameter, e.g. "browserDetailsUrl": "VAADIN?code=erefdvdfgftf".
  2. Extract the value from Page.getLocation().getQuery(). This is slightly less ugly than using the v-loc that you discovered as it doesn't rely on any implementation details.

这篇关于vaadin 7 URL 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆