gpt4 book ai didi

forms - Grails表单和URL映射

转载 作者:行者123 更新时间:2023-12-02 14:17:37 25 4
gpt4 key购买 nike

我正在编写Grails应用程序,我想在其中设置一个允许用户键入图像ID号的表单,并将此值传递给 Controller ​​/操作,该 Controller /操作将从S3中检索给定图像ID的图像。

所需的网址格式为example.com/results/1234。我已经设置了以下URL映射:

class UrlMappings {

static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}

"/results/$id?" {
controller = "s3Image"
action = "getS3Image"
}

"/"(view:"/index")
"500"(view:'/error')
}
}

以下是我如何设置表格的方法:
<g:form  controller="results" method="get">
<input type="text" name="id" class="input-xxlarge" placeholder="http://www.example.com">
<button class="btn btn-inverse">Submit</button>
</g:form>

但是,这似乎是将表单提交给example.com/results?id=12345。

如何更改表单或映射,以便在提交表单后生成所需的网址?

谢谢!

最佳答案

<g:form  controller="results" method="get">

将生成一个HTML表单,其 Action URL为 /results(名为“results”的 Controller 的反向URL映射,没有 Action 或ID)。提交此表单后,浏览器会将 ?id=1234添加到此URL的末尾,因为表单方法是GET。您无法在服务器端影响URL映射。

相反,您应该将表单POST到另一个 Controller 操作,该操作将重定向到 getS3Image操作。重定向将有权访问服务器端的ID,因此可以为重定向生成外观友好的URL。

网址映射:
"/results/$id?" {
controller = "s3Image"
action = "getS3Image"
}

"/findImage" {
controller = "s3Image"
action = "find"
}

S3ImageController:
def find() {
redirect(action:"getS3Image", id:params.id)
}

def getS3Image() {
// as before
}

GSP:
<g:form  controller="s3Image" action="find" method="post">
<input type="text" name="id" class="input-xxlarge" placeholder="http://www.example.com">
<button class="btn btn-inverse">Submit</button>
</g:form>

关于forms - Grails表单和URL映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11152172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com