zap*_*pee 7 url request vaadin
我对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") );
...
}
}
Run Code Online (Sandbox Code Playgroud)
结果:request.getAttribute("code"):null request.getParameter("code"):null
我意识到getParameterMap()有一个"v-loc"参数.但是如果我使用它变量,我必须编写一些代码来从这个变量中获取url参数:
v-loc:host/myvaadinapp/?code=erefdvdfgftf...
主题:驯鹿v-rtzo:-600 v-dston:false ...
我认为,这不是太好的解决方案.
你能帮我解决一下如何在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));
}
}
Run Code Online (Sandbox Code Playgroud)
我在嵌入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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
我的结果是:Hello Unknown.
如果我不使用嵌入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,
完整源代码:
<!-- 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",
...
Run Code Online (Sandbox Code Playgroud)
Lei*_*and 11
此行为的原因UI.init是从一个请求调用,该请求并不总是与用于加载应用程序的URL相同.在嵌入UI而不是将其作为独立应用程序提供时尤其如此.
UI初始化请求(在代码的某些部分中名为"浏览器详细信息请求")默认发送到用于加载页面的相同URL.这显然在嵌入时不起作用,因为没有Vaadin servlet映射到该URL,因此您必须定义一个browserDetailsUrlto来告诉应该发送请求的位置.
v-loc您观察到的内部用于初始化返回的值Page.getLocation().
要解决您的特定问题,我建议使用以下选项之一:
browserDetailsUrl以包括期望参数作为查询参数,例如"browserDetailsUrl": "VAADIN?code=erefdvdfgftf".Page.getLocation().getQuery().这比使用v-loc您发现的稍微不那么难看,因为它不依赖于任何实现细节.