防止反向路径中的斜杠转义
在我的 Play 应用程序中,当我使用反向路由生成链接时,参数中的正斜杠会被转义,但我宁愿它们没有被转义。
例如:
<a href="@{Application.page('about/contact')}">Contact Us</a>
生成一个工作链接,但将您带到 /about%2Fcontact.html
而不是 /about/contact.html
。 (在地址栏中输入 /about/contact.html
也会到达正确的位置。)
我这样做是因为我只有几个页面可以做一些有趣的事情,其余的基本上都是static——它们仅使用基本模板功能(extends
、include
、get
、set
)。由于为每个控制器和操作创建单独的控制器和操作太过分了,因此我设置了一个操作来处理所有控制器和操作。
public class Application extends Controller {
public static void page(String path) {
render("/static/" + path + ".html");
}
}
用一条路线处理:
GET /{<.+>path}.html Application.page
知道如何改变事情以使斜杠不被编码吗?
In my Play app when I use reverse routing to generate links, forward slashes in parameters are being escaped, and I'd rather they weren't.
For example:
<a href="@{Application.page('about/contact')}">Contact Us</a>
generates a working link, but takes you to /about%2Fcontact.html
rather than /about/contact.html
. (Entering /about/contact.html
in the address bar also goes to the right place.)
I'm doing this because I've only got a few pages that do anything interesting and the rest are basically static -- they only use the basic template features (extends
,include
,get
,set
). Since creating separate controllers and actions for each of them would be overkill I've set up a single action to handle all of them.
public class Application extends Controller {
public static void page(String path) {
render("/static/" + path + ".html");
}
}
handled with one route:
GET /{<.+>path}.html Application.page
Any idea how I can change things around so that the slashes aren't encoded?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于我们(显然)无法覆盖 RouteArgs 的工作方式,因此我们可以在方法中解码给定的 url,如下所示:
但要注意安全风险! (好吧,.html 不是那么多,但以防万一......)
Since we (apparently) can't override the way RouteArgs works, we can decode the given url in the method, like this :
But be aware of security risks! (well, .html not so much, but just in case ...)