这是我的 Grails (2.3.6) Controller :
class WidgetController {
def index() {
render(
view: "createNew",
model:[
]
)
}
def execute() {
println "Executing form submission!"
redirect(action: "listAll")
}
def listAll() {
// Does some stuff
}
}
index
URL 为 http://localhost:8080/myapp/widget
。这个想法是,当有人访问此 URL 时,他们会看到一个 HTML 表单。当他们填写表单时,它们会(在服务器端)发送到 execute()
方法,该方法会执行一些繁重的工作,然后将它们重定向到 listAll()
code> 方法,它执行一些最后的操作并呈现一个网页供他们查看。
以下是 createNew.gsp
上的 HTML 表单(通过 index()
方法呈现:
<g:form name="create-new-form" url="[action:'execute',controller:'widget']">
<table class="pure-table pure-table-bordered">
<tr>
<td class="row-header">Fizz:</td>
<td><g:textField id="app-fizz" name="fizz" /></td>
</tr>
<tr>
<td class="row-header">Buzz:</td>
<td><g:textField id="app-buzz" name="buzz" /></td>
</tr>
</table>
<g:actionSubmit value="Create" />
</g:form>
当我转到此 URL 并提交表单(单击创建按钮)时,我会被重定向到 http://localhost/myapp/widget/execute
,其中显示我的自定义错误页面之一(基本上是“抱歉此页面不可用”类型的错误。
此外,在日志输出中,我的 println
声明“正在执行表单提交!”没有触发。这告诉我,我没有正确连接某些东西:Grails 正在尝试重定向到 /execute
URL,但不知何故没有将该 URL 与我的 Controller 的 execute()
链接起来> 方法。有想法吗?
最佳答案
尝试:
<g:actionSubmit action="execute" value="Create" />
如果您仅指定 g:actionSubmit
的值,它将创建带有此标签的按钮,并根据此值重定向到操作。如果操作名称与按钮标签不同,您应该指定 action
和 value
属性。看看documentation .
请注意,如果您使用 g:actionSubmit
,则 g:form
的操作属性将被忽略(顺便说一句,您指定了该属性)。您将在上面链接的文档中找到可能有用的更多信息。
关于Grails 表单未提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25787237/