[英]How to get the input from a text box on a webpage form
This is on Grails.这是在 Grails 上。 This is a very basic thing which apperatnly I am failing to understand.
这是一个非常基本的事情,我显然无法理解。
I have this in my index.gsp我的 index.gsp 中有这个
<g:form name="testForm" url="[controller:'test',action:'index']">
<g:textField name="Input A" value="${Input1}"> </g:textField>
<g:textField name="Input B" value="${Input2}"> </g:textField>
</g:form>
I also have this in my TestController class:我的 TestController class 中也有这个:
class TestController {
def index = {
def Input1
def Input2
}
}
I want to get the two inputs that the user enters on the webpage and save them to the appropriate fields (Input1, Input2) on the controller.我想获取用户在网页上输入的两个输入,并将它们保存到 controller 上的相应字段(Input1、Input2)中。
How do I go about it?我该怎么做 go 呢?
thanks谢谢
You can write your form like this:你可以这样写你的表格:
<g:form name="testForm" controller="test" action="index">
<g:textField name="Input1" value="${Input1}"> </g:textField>
<g:textField name="Input2" value="${Input2}"> </g:textField>
<g:actionSubmit value="Send to controller" action="index"/>
</g:form>
Note that in this case,请注意,在这种情况下,
Then in the controller然后在 controller
class TestController {
def index = {
def Input1 = params.Input1
def Input2 = params.Input2
["Input1": Input1, "Input2": Input2]
}
}
With this, the values will be rendered properly (inside the returned model)有了这个,值将被正确呈现(在返回的模型内)
You receive the form parameters from the implicit variable " params
".您从隐式变量“
params
”接收表单参数。 Do a log.error(params)
in your controller and you will know how they are passed.在您的 controller 中执行
log.error(params)
,您将知道它们是如何通过的。 You can access your parameter like params."Input 1"
.您可以访问您的参数,如
params."Input 1"
。
Note that there are neat ways to handle multiple inputs from one class, eg given a domain class:请注意,有一些巧妙的方法可以处理来自一个 class 的多个输入,例如给定域 class:
class Test {
String a;
String b;
}
You can have a form:你可以有一个表格:
<g:form name="testForm" controller="test" action="index">
<g:textField name="test.a" value="${Input1}"> </g:textField>
<g:textField name="test.b" value="${Input2}"> </g:textField>
</g:form>
And in the controller you do:在 controller 中,您可以:
class TestController {
def index = {
def testInstance = new Test(params.test)
}
}
However this trick you should only use in admin areas or something, since there are some security considerations to be done.但是,您应该只在管理区域或其他地方使用此技巧,因为需要考虑一些安全问题。
So having a submit button works.所以有一个提交按钮是有效的。
<g:form name="testForm" controller="test" action="index">
<g:textField name="input1" value="${input1}"> </g:textField>
<g:textField name="input2" value="${input2}"> </g:textField>
<g:submitButton name="Submit" value="Submit"></g:submitButton>
</g:form>
... ...
class TestController {
def index = {
def Input1 = params.input1
def Input2 = params.input2
render(Input1+"<br />")
render(Input2+"<br />")
}
}
class TestController {
def index = {
def Input1 = params.input1
def Input2 = params.input2
render(Input1+"")
render(Input2+"")
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.