UrlMappings 对于链接来说是一个很棒的功能,但是它们也适用于表单吗?
假设我们有这样的映射:
"/map/$a" {
controller="form"
action="show"
}
但是像这样的表单:
<g:form controller="form" action="show">
<g:textField name="a" />
</g:form>
不会有相同的效果。它将导致请求 /appname/form/show?a=whatever
我知道表单不能像链接一样在 HTML 创建时重写 - 主要是因为参数的值不是目前已知,但我希望该 URL 被重定向到“好”URL。
有没有办法在 grails 中做类似的事情?或者我必须编写自己的重定向?
UrlMappings are a great feature for links, but do they also work for forms?
Suppose we have a mapping like this:
"/map/$a" {
controller="form"
action="show"
}
a <g:link controller="form" action="show" params="[a:'test'] />
will now be rewritten as
<a href="/appname/map/test" />
But a form like this:
<g:form controller="form" action="show">
<g:textField name="a" />
</g:form>
will not have the same effect. It will result in requesting /appname/form/show?a=whatever
I know that a form can't be rewritten at HTML-creation time like a link - mainly because the value of the parameter is not known at this time, but I hoped that this URL would be redirected to the "nice" URL.
Is there a way to do stuff like that in grails? Or do I have to write my own redirect?
发布评论
评论(2)
正如您提到的,由于参数值未知,因此无法在页面渲染时完成。另外,您可以想象一旦您超出了一个表单字段,将会出现什么问题。
您可以编写一些 JavaScript 在 onSubmit 中创建一个漂亮的 URL,或者如果您在各处都这样做,您可能可以创建一个自定义标记。
Like you mentioned, it can't be done at the time of page rendering due to unknown parameter values. Plus you can imagine the problems that would ensue once you go beyond one form field.
You could either write some JavaScript to make a pretty URL in the onSubmit or if it's something you do all over the place you could probably make a custom tag.
我想我已经找到了答案:
URL 重写似乎只在 HTML 渲染时才起作用。
因此
将导致
/appname/map/test
。但这不是我想要的 - 我希望在提交表单时重写 URL。所以我想出了一个重定向操作:
我重写了我的表单,因为
这似乎很有效(以一次重定向为代价)
I guess I've found the answer:
URL-Rewriting seems only to work at the time when the HTML gets rendered.
So
will result in
/appname/map/test
. But that's not what I want - I want the URL to be rewritten when the form gets submitted.So I came up with a redirect action:
and I rewrite my form as
This seems to work great (at the cost of one redirect)