Grails - The Link Between Controllers and Views

I am wondering how I can fill a text box in my view from a list in my controller, I searched for examples for a while, but did not find anything. I am not sure how to access the text field from the view exactly where, as in Java, you can do something as simple as jTextField.getText(). I am also wondering how to capture text in a text box.

Below I will post an example code of what I am doing.

Controller:

def loadFile = {
    def propFile = "c:/props.txt"
    def propMap = [:]
    def propList = []

    new File(propFile).eachLine { line ->
    def (key,value) = line.split(':').collect { it.trim() }
    propMap."$key" = "$value"

    if(propMap) {
    propList << propMap
    propMap = [:]
    }
}

}

def saveFile = {
    //get contents of text box
    //over-write props.txt with new information
}

View:

<g:textField name="properties"/>
<span class="menuButton"/><g:link action="loadFile" controller="myController">Load File</g:link>
<span class="menuButton"/><g:link action="saveFile" controller="myController">Save File</g:link> 

So my question seems relatively straightforward, how do I access a text field when I want to populate it and save data from it?

.

.

.

EDIT

After checking some of the examples you presented, I have a final question.

Why is the following code valid when a button is clicked Load File?

<g:form controller="drive">
<g:textArea name="properties" value="${params.param1}" rows="50" cols="6"/>
<br/>
<g:submitButton name="loadFile" value="Load File"/>
<g:submitButton name="saveFile" value="Save File"/>
</g:form>
<span class="menuButton"/><g:link action="loadFile" controller="drive">Load File</g:link>

g:submitButton loadFile list gsp. , menuButton, textArea . , , , , .

+3
2

. , , .

<g:form controller="myController" action="saveFile">
  <g:textField name="properties"/>
  <g:submitButton name="saveFile" value="Save File" />
</g:form>

properties :

def saveFile = {
    def properties = params.properties
    // do whatever you need
}

EDIT:

, , .

, Drive, view ( ) - drive/properties.gsp. , , - :

def loadFile = {
   // your code here

   render(view: 'properties.gsp', model=[properties:propList])
}

:

<g:form controller="drive">
  <g:textArea name="properties" value="${properties?.join("\n")}" rows="50" cols="6"/>
  <br/>
  <g:actionSubmit name="loadFile" action="loadFile" value="Load File"/>
  <g:actionSubmit name="saveFile" action="saveFile" value="Save File"/>
</g:form>

, . .

+4

jjczopek answer , , .

, , ...

    params.param1 ='value to pass'
    render(view:"testView")

, , ...

<g:textField name="text1" value="${params.param1}"/>

.

, ... Grails Controller - Render

+4

All Articles