2013-05-11 95 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