问题描述:

In my grails project I've built up a new view in which user can perform a search of entities.

I've created the gsp adding the method search() in controller and automatically creating the gsp as described here

In this gsp there is only one input field and a g:actionSubmit button. If I fill form with correct data everything works well, but if data does not have any correspondance I would see an error message in the view like the validation error messages with popups...but I don't know how to show it, because I'm not using any bean with this gsp.

In addition, after an error, I would render the same view, but with render(view: "search", model: [patientInstance: patientInstance]) the view is the same, but path is /index and not /search...

How can I show an error message? How can I have the right path?

here is the search()

def search()

{

def patientInstance = new Patient()

if(params.patient_textField == "" || params.patient_textField == " " || params.patient_id =="")

{

//here I would like to show message

//the redirect works correctly

redirect(controller: "patient", action: "search")

}

else {

def patientToShow = Patient.findById(params.patient_id)

redirect(controller: "patient", action: "show", params: [id: patientToShow?.id])

}

}

here is the snippet of gsp

 <g:form>

<div id="patientDiv">

<label for="patient">

<g:message code="event.patient.label" default="Patient" />

</label>

<input style=" margin: 0px 10px 10px 0px;" type="text" name="patient_textField" id="patient_textField" value="" placeholder="${g.message(code: 'patient.choose', default: 'Insert Patient...')}" />

<input type="hidden" id="patient_id" name="patient_id" value="" />

<g:actionSubmit class="search" value="${g.message(code: 'default.search.label', default: 'Search Patient')}" action="search" ></g:actionSubmit>

</div>

</g:form>

EDIT:

solved problem of path changing render with redirect(controller: "patient", action: "search")

网友答案:

In the error portion of your code you can do flash.error = "Your error message here" And in the gsp do something like:

<g:if test="${flash.error}">
    <div class="alert alert-info">
      ${flash.message}
    </div>
  </g:if>

There is already a flash bean in scope. http://grails.org/doc/latest/ref/Controllers/flash.html

相关阅读:
Top