Url rewriting 向url播放框架添加表单参数
我正在用Play Framework 2.1版开发一个应用程序 我的一个scala视图文件中有一个表单,如下所示:Url rewriting 向url播放框架添加表单参数,url-rewriting,playframework,playframework-2.1,Url Rewriting,Playframework,Playframework 2.1,我正在用Play Framework 2.1版开发一个应用程序 我的一个scala视图文件中有一个表单,如下所示: @helper.form(action = routes.Users.formHandle("title")) { <fieldset> @select( firstForm("country"), options = options(Country.listAll), '_default -> "--- Choose a countr
@helper.form(action = routes.Users.formHandle("title")) {
<fieldset>
@select(
firstForm("country"),
options = options(Country.listAll),
'_default -> "--- Choose a country ---",
'_showConstraints -> false,
'_label -> "country",
)
@select(
firstForm("state"),
options = options(State.listAll),
'_default -> "--- choose your state ---",
'_showConstraints -> false,
'_label -> "state",
)
}
显然,提交表单后,url将如下所示:
host/submit?country=nameOfTheCountry&state=nameOfTheState
如何将上述url更改为:
host/submit/nameOfTheCountry/nameOfTheState
任何人都可以帮忙吗?当web浏览器使用
GET
方法提交表单时,它会将所有参数放入查询字符串中-这是HTML规范。您不能让它执行任何其他操作。如果你想让它做其他事情,那么你必须使用JavaScript截取表单提交,自己用JavaScript手动构建URL,然后将窗口位置设置为该URL。我不想也不能更改HTTP规范,我只是在寻找一种绕过此限制的方法,因此,在表单上附加一个onsubmit
回调,该回调将从表单中提取参数并构建URL,例如使用jquery,它的外观如下:$(“#myform”).submit(函数(e){window.location=“/submit/”+$(“#country”).val()+$(“#state”).val();e.preventDefault()});我的方法目前已经在这个链接的答案中解释过了。你认为哪种方法推荐重定向或使用javascript?哪种方法最好取决于你想要实现什么。在您链接到的解决方案中,客户端发出两个请求,一个被重定向,然后是实际请求。我不认为这有什么意义,为什么不在第一个请求中处理它,两个请求意味着用户要花两倍的时间才能看到结果?但是答案可能是你想要一个特定的URL格式的原因。。。我不知道你为什么关心URL格式,所以我无法回答。
host/submit/nameOfTheCountry/nameOfTheState