0
votes

I am trying this simple ExtJS - Grails/Groovy test. My Groovy Server Page ( gsp ) file contains the below ExtJS code:

  • It has two fields State Code and State Name.
  • User enters the details and click on Submit.
  • The Submit triggers the handler which further POSTs the form to Controller class.

function createTender() {
      var submitHandler = function() {
      alert("Submit Pressed !");
      var formPanel = Ext.getCmp('stateForm');
      formPanel.getForm().submit({
        url     : 'state/saveState',
        method  : 'POST',
        success : function() {
        alert('State saved successfully!');
      },
      failure : function() {
        alert('State save failed!');
      }
    });
 }

 Ext.create('Ext.form.Panel',{
    id: 'stateForm',
    height: 300,
    width: 400,
    bodyPadding: 10,
    title: 'Create State',
    items: [{
      xtype:'textfield',
      fieldLabel: 'State Code',
      name: 'stateCode',
      allowBlank:false
    }, {
      xtype:'textfield',
      fieldLabel: 'State Name',
      name: 'stateName'
    }],     

    buttons: [{
      text: 'Save',
      handler: submitHandler
    },{
      text: 'Cancel'
    }],
    renderTo: Ext.getBody()
    });
  }

Below is the StateController class with the action/method saveState. It just prints the state code and does nothing.

class StateController {
  static scaffold = true
  def saveState = {
    println "Into saveTender() method !!!"
    println params.stateCode
    // Steps to save the state code and name into Database.
  }
}

Problem:

  • The saveState method is printing both the println statements. However, like you can see, I am not returning anything, because I don't know what should I return to the gsp. My intention is just to save the state details and throw an alert saying 'State saved Successfully!' message.
  • But, the Ext code in gsp is throwing the "State save Failed!" message.

I want to return success from Controller to gsp. How do I do it? Please don't mind if this question is too naive as I am a beginner.

3

3 Answers

2
votes
        render(contentType: "text/json") {
               array = {
                   result "success": 'true',
                          "message": 'State Saved'
               }
        }
0
votes

It fails because your closure saveState will try to render a GSP by default, since you not render or return anything. If you're using Grails 2.x the preferred way of declaring actions is methods, so I'm using a method instead of a Closure.

def saveState() {
  State state = new State(stateCode: params.stateCode, stateName: params.stateName)
  if(state.save()) {
    render text: "State saved."
  } else {
    render text: "State fails: $state.errors"
  } 
}
0
votes

You are not returning anything from "saveState". Try to return a JSON like,

{
     success: 'true',
     message: 'State Saved'
}

You can lookup grails inbuilt JSON converters to return something more complex.