简体   繁体   中英

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

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

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)

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, eg 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.

Check the params map.

You can access fields by its names:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM