How to get the input from a text box on a webpage

2019-07-10 04:37发布

问题:

This is on Grails! This is a very basic thing which apperatnly I am failing to understand.

I have this in my 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:

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.

How do I go about it?

thanks

回答1:

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,

  1. the controller parameter for the g:form tag is not needed, it is used by convention
  2. Action may also probably be removed depending on your route (and grails version), but most of the time, this is what you specify to the form so grails knows where to submit
  3. Parameters were slightly out of sync "Input A" -> "Input1"

Then in the 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)



回答2:

You receive the form parameters from the implicit variable "params". Do a log.error(params) in your controller and you will know how they are passed. You can access your parameter like params."Input 1".

Note that there are neat ways to handle multiple inputs from one class, e.g. given a domain 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:

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.



回答3:

Check the params map.

You can access fields by its names:

def input1 = params.input1;
def input2 = params.input2


回答4:

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 />")
       }
}


回答5:

class TestController {

       def index = {
        def Input1 = params.input1
        def Input2 = params.input2


        render(Input1+"")
        render(Input2+"")
       }
}