I'm working on a Grails application and as you can see from the screenshot below, at the bottom of my create page I have two submit buttons, the one on the left creates and saves the instance and takes me to the show page of the instance I have just created.
The submit button on the right does exactly the same thing however I now want that button to save/create the instance but stay on this same (create) page and not redirect me to show.gsp with the fields still filled with the data previously entered. Is this possible?
Here is the code for the two buttons at the bottom of create.gsp
<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
instead of <g:submitButton> you should use:
<g:actionSubmit value="${message(default:'Create')}" action="create" />
<g:actionSubmit value="${message(default:'Create and Stay')}" action="createAndStay" />
in the corresponding controler actions you decide wether you redirect to list or to create actions
Related
I'm completely new to grails, and I appreciate any help.
This code is in my gsp file:
<g:form action="backfillMachineTags">
<fieldset class="buttons">
<g:submitButton name="create" class="save"
value="${message(code: 'default.button.create.label', default: 'Create')}"/>
</fieldset>
</g:form>
And this code is in my RegressionPoolMachineController.groovy file:
def backfillMachineTags()
{
flash.message = "testing message"
redirect(action: "list")
}
But when I click the submit button on the form, the method is never entered and I get a 404 not found.
message:/pool-manager/regressionPoolMachine/backfillMachineTags
status: The requested resource is not available.
I also tried doing:
<g:form url="[action:'backfillMachineTags',controller:'RegressionPoolMachineController']">
but get a similar 404 not found.
Am I missing a step to connect my form and controller?
Try to use the following:
<g:form controller = "controllerName" action ="actionName" >
<fieldset class="buttons">
<g:submitButton name="create" class="save" value ="${message(code: 'default.button.create.label', default: 'Create')}"/>
</fieldset>
</g:form>
Try adding a "controller" attribute to your form in addition to the "action" attribute. You could use url instead of controller and action, but you would need something like
url="${createLink(controller: "myController", action: "myAction")}" instead of what you've shown here.
For reference: http://docs.grails.org/2.5.6/ref/Tags/form.html
You can also look at the generated HTML in your browser (just view source) and see what is actually being generated on the resulting web page. That's usually pretty helpful for finding out why things aren't going where you want them to!
Recently I began to study Grails, and I noticed that the file create.groovy is generated by a dynamic scaffold. The form tag doesn't have an attribute called controller, only action.
So my question is, how does the Grails framework discover the controller that should be called?
<g:form action="save">
<fieldset class="form">
<f:field bean="categoria" property="nome" class="form-control" />
</fieldset>
<fieldset class="buttons">
<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
</fieldset>
</g:form>
Thanks, and sorry for my bad english!
Grails uses "convention over configuration" so whenever you use a form without controller, grails will use the default convention.
Since your view is put in a folder named after a controller, grails will assume that the default controller for any view inside that folder is the one that matches with the folder name. Example
BookController is the default controller for any gsp inside the grails-app/views/book folder.
*This convention also applies to things like links.
I'm using Struts 2 and MySQL.
In my JSP I have 3 select controls:
<s:select list="country" name="country" onchange="getstate();" />
<s:select list="state" name="state" onchange="getcity();" />
<s:select list="city" name="city" />
In my action class:
public string displayDetails() {
getCountry();
getState(getCountry());
getCity(getState());
}
If I want to get state I have to access getCountry() method again. How can I avoid that call every time?
If I'm not adding all the action in one method I'm getting "list field is not filled" error.
The bean class looks like this:
String houseNo
String address
Person person
The view looks like this
<g:form action="save">
<fieldset class="form">
<g:render template="form" />
</fieldset>
<fieldset class="buttons">
<g:submitButton name="create" class="save"
value="${message(code: 'default.button.create.label', default: 'Create')}" />
</fieldset>
</g:form>
According to my knowledge in Grails, i think is <g:render template="form" /> will pull all form attributes and display it.
But what i want to do is Instead of displaying a drop-down for Person, i want to display all Person related fields like personName, personAge. How can i display these fields in a label underneath ?
Help
You're correct about the way g:render works, the template part refers to a GSP which will look through the bean values and print them according to the html + groovy markup in "_form.gsp" (located in your views folder under the controller name).
To change the way the Person domain object is displayed, simply edit this "_form.gsp" and take out the 'select' html code - replacing it with groovy markup using the property values of the bean, eg.
${beanName.person.personName} //(use the existing code to help workout the bean name etc)
Hopefully that helps you on your way.
Hi All,
I have an edit button
<span class="button"><g:actionSubmit class="edit" action="edit" value="${message(code: 'default.button.edit.label', default: 'Edit')}" /></span>
But when click on it the adderess on the browser was not http:\...\edit,
it was http:\...\index. Why?
I tried to delete or rename the name of Edit in controller, it still not influence. Why?
Thanks!!!
Try specifying controller's name with g:form like
<g:form controller="CONTROLLER_NAME">