2013-05-11 64 views
6

我不太熟悉vaadin 7,我无法弄清楚这一点。我试图使用谷歌,但没有有用的结果。vaadin 7网址参数

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

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

url参数来自Facebook的用户登录(OAuth的)后,我需要处理在我的代码代码的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( “代码”):空 的request.getParameter( “代码”):空

我意识到getParameterMap( )有一个“v-loc”参数。但是,如果使用它的变量我必须写一些代码从这个变量获取URL参数:

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

我想,这不是太好的解决方案。

你能帮助我如何获得vaadin中的原始URL参数?

谢谢。


我的评论是:

我试图用的request.getParameter( “代码”),但它不工作。我不知道为什么。

我原来的网址是: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模式比一切工作得很好。

问题在哪里?


解决方案(感谢您对雷夫Å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", 
          ... 

回答

10

这样做的原因行为是UI.init是从并不总是此次用于加载应用程序相同的URL提出的请求调用。当嵌入UI而不是将其作为独立应用程序提供时,情况尤其如此。

的UI INIT请求(在代码的某些部分名为“浏览器的详细要求”),默认情况下发送到用于加载页面的URL相同。由于没有将Vaadin servlet映射到该URL,因此嵌入时显然不起作用,所以您必须定义一个browserDetailsUrl来确定请求的发送位置。

v-loc您观察到的是内部用于初始化Page.getLocation()返回的值。

解决您的具体问题,我建议的其中一个选项:

  1. Dyncamically产生browserDetailsUrl包括所需要的参数作为查询参数,例如"browserDetailsUrl": "VAADIN?code=erefdvdfgftf"
  2. Page.getLocation().getQuery()中提取值。这比使用您发现的v-loc略微难看,因为它不依赖任何实现细节。
+0

谢谢你的回答! 我解决了它,并在上面分享了我的解决方案。 – zappee 2013-05-15 12:31:11

4

应该一起工作:用request.getParameter( “代码”) 。

+0

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