Vaadin url 7

I am not very familiar with vaadin 7, and I cannot figure it out. I tried using google, but not a useful result.

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

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

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

I tried to do this:

  @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 parameter of "v-loc". But if I use a variable, I need to write some code to get the url parameter from this variable:

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

I think this is not a very pleasant solution.

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

Thanks.


My comments:

I tried using request.getParameter ("code"), but it did not work. 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 run my vaadin app in UI attachment mode.

The contents 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: Hello Unknown.

If I do not use Embedding UI mode, everything works very well.

Where is the problem?


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", ... 
+6
source share
2 answers

The reason for this behavior is that UI.init is called from a request that is not always done with the same URL that was used to download the application. This is especially important when embedding the user interface instead of serving it as a standalone application.

A user interface initialization request (with the name "requesting browser information" in some parts of the code) is by default sent to the same URL that was used to load the page. Obviously, this doesn’t work when embedding, since the Vaadin servlet is not displayed on this URL, so you should define browserDetailsUrl to indicate where the request should be sent.

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

To solve your specific problem, I would suggest one of the following options:

  • Dynamically generate browserDetailsUrl to include the required parameter as a request parameter, for example. "browserDetailsUrl": "VAADIN?code=erefdvdfgftf" .
  • Extract value from Page.getLocation().getQuery() . This is a little less ugly than using v-loc , which you discovered, as it does not depend on any implementation details.
+10
source

It should work with: request.getParameter ("code").

+4
source

Source: https://habr.com/ru/post/944753/


All Articles