java 在 FreeMarker 上获取 URL 的 queryString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14372165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get queryString of a URL on FreeMarker
提问by franticfrantic
learning FTL here.
在这里学习 FTL。
I'm trying to get add a query string on my FTL page, like http://localhost/search
, i'd like to add a query string on the URL, say http://localhost/search?bing
so that user could switch with the default setting when there is no query string.
我正在尝试在我的 FTL 页面上添加一个查询字符串,例如http://localhost/search
,我想在 URL 上添加一个查询字符串,http://localhost/search?bing
这样用户可以在没有查询字符串时使用默认设置进行切换。
However, I have no luck on grabbing the queryString from the URL. I'm also trying to avoid using a JavaScript solution on this.
但是,我没有运气从 URL 中获取 queryString。我也试图避免在这方面使用 JavaScript 解决方案。
here's my code:
这是我的代码:
<#if RequestParameters.bing?exists >
<#assign useServer = "http://www.bing.com">
<#else>
<#assign useServer = "http://www.google.com">
</#if>
<h1>${useServer}</h1>
typing in the queryString into the url still returns http://www.google.com
on the h1
.
打字查询字符串到URL仍返回http://www.google.com
上h1
。
回答by TheWhiteRabbit
For query string ?param1=abc¶m2=123
, you can retrive params like below:
对于查询字符串?param1=abc¶m2=123
,您可以检索如下参数:
${RequestParameters.param1
} & ${RequestParameters.param2}
${RequestParameters.param1
} & ${RequestParameters.param2}
And also try <#if RequestParameters.bing??>
也试试 <#if RequestParameters.bing??>
parameters are something that followed by < protocol >://< host >:< port >?< param1 >&< param2 >&..
参数后面跟着<协议>://<主机>:<端口> ?<参数1>&<参数2>&..
for example in https://www.google.co.in/search?q=StackOverflowURL param name is qand value is 'StackOverflow'
例如在https://www.google.co.in/search?q=StackOverflowURL 参数名称是q值是“StackOverflow”
回答by franticfrantic
I figured out to use request.getParameter("param")
我想出使用 request.getParameter("param")
<#if (request.getParameter("param")?has_content && request.getParameter("param")?lower_case?matches("true"))>
<#assign useServer = "http://bing.com">
<#else>
<#assign useServer = "http://google.com">
Worked like a charm.
像魅力一样工作。
回答by OulinaArt
I tried to check request parameters with first post's code:
我试图用第一篇文章的代码检查请求参数:
<#if RequestParameters.bing??>
BUT nothing happened.
Then I checked <#if RequestParameters.param1??>
then I got result.
但什么也没发生。然后我检查<#if RequestParameters.param1??>
然后我得到了结果。
I hope it helps someone.
我希望它可以帮助某人。