我对vaadin 7不太熟悉,我无法弄清楚这一点。我尝试使用谷歌但没有有用的结果。
我的问题很简单。如何在我的 UI 类中获取请求 url 参数?
我的网址有一个参数:host/myvaadinapp/?code=erefdvdfgftf...
url 参数来自用户登录(OAuth)后的 facebook,我需要在我的代码中处理 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...
主题:驯鹿 v-rtzo:-600 v-dston:假...
我认为,这不是很好的解决方案。
您能帮我如何在 vaadin 中获取原始 url 参数吗?
谢谢你。
我的评论是:
我尝试使用 request.getParameter("code") 但它不起作用。我不知道为什么。
我原来的网址是: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>
索引.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) ?“”:“?” + 网址部分[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",
...