In my Play app when I use reverse routing to generate links, forward slashes in paramete开发者_StackOverflow社区rs 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?
Since we (apparently) can't override the way RouteArgs works, we can decode the given url in the method, like this :
public class Application extends Controller {
public static void page(String path) {
render("/static/" + URLDecoder.decode(path, "UTF-8"); + ".html");
}
}
But be aware of security risks! (well, .html not so much, but just in case ...)
精彩评论