我在搜索查询的 URL 编码方面遇到问题,我在 Velocity 中有这个问题模板。
<form method="GET" action="$req.contextPath/plugins/peopledirectory/search.action">
<div class="greyboxfill" style="width: 420px">
<input type="hidden" name="pageId" value="$pageId"/>
<input type="text" name="search" id="search" size="30" value="$search"/>
<input type="submit" name="searchbtn" value="Pesquisar">
</div>
</form>
问题是,当我单击提交按钮时,搜索字符串未经过 URL 编码,如果我搜索 ME&A 之类的内容,它只会搜索 ME。 Velocity 中是否需要任何定义才能使其正常工作?
我在网络上看到表单 HTML 标记具有内部 URL 编码,为什么在这种情况下它不起作用?
最佳答案
最好的办法是使用 EscapeTool 显式转义字符串来自 VelocityTools .这是一个额外的库,您需要下载该库,然后将其包含在您的速度环境中。
URL 编码就像这样简单:
$escape.url($search)
关于HTML 表单未对 Velocity 中的搜索 URL 进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5390869/