在我的grails项目中,我建立了一个新 View ,用户可以在其中执行实体搜索。
我创建了gsp,在 Controller 中添加了search()方法,并按照here的描述自动创建了gsp
在此gsp中,只有一个输入字段和一个g:actionSubmit按钮。如果我用正确的数据填写表格,则一切正常,但如果数据没有任何对应关系,我会在 View 中看到一条错误消息,如带有弹出窗口的验证错误消息...但是我不知道如何显示它,因为我没有将此gsp使用任何bean。
此外,发生错误后,我将渲染相同的 View ,但是使用render(view: "search", model: [patientInstance: patientInstance])
时,该 View 是相同的,但是路径是/ index而不是/ search ...
如何显示错误消息?我怎么走正确的道路?
这是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])
}
}
这是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>
编辑:
用
redirect(controller: "patient", action: "search")
解决了路径更改渲染的问题
最佳答案
在代码的错误部分,您可以执行flash.error =“您的错误消息在这里”
然后在gsp中执行以下操作:
<g:if test="${flash.error}">
<div class="alert alert-info">
${flash.message}
</div>
</g:if>
作用域中已经存在一个Flash bean。 http://grails.org/doc/latest/ref/Controllers/flash.html
关于grails - 在独立的gsp View 中显示grails错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26344729/