简体   繁体   中英

grails g:set with null values in gsp page

In my grails project I am using PDF plugin. For this purpose, I use the following link to generate a PDF:

<g:pdfLink class="pdf" pdfController="patient" pdfAction="privacyPolicy" pdfId="${patientInstance?.id}" ><g:message code="patient.generatePrivacy" /></g:pdfLink>

the privacyPolicy() method is as follows:

def privacyPolicy(Long id){

    def patientInstance = Patient.get(id)

    if (!patientInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'patient.label', default: 'Patient'), id])
        redirect(action: "list")
        return
    }

    if(patientInstance.cf.equals("")) {
        flash.message = message(code: 'cf.not.present.message', args: [message(code: 'patient.label', default: 'Patient'), id])
        redirect(action: "list")
        return
    }

    [patientInstance: patientInstance]

}

the gsp page has got two variables defined as follows:

<g:set var="birthdate" value="${PatientController.getDateFromFiscalCode(patientInstance?.cf)}" />
<g:set var="cityName" value="${PatientController.getBirthplaceFromFiscalCode(patientInstance?.cf)}"/>

both variables are dependent by cf , that could be not present for a patientInstance

Analyzing the flow in debug mode(when cf is empty), I've noticed that privacyPolicy() is called twice. First time it enters into second if, but then it enters in first if.

The error in console is the following:

ERROR pdf.PdfService  - org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 

there was a problem with PDF generation java.lang.NullPointerException: Cannot get property 'length' on null object

I think that depends on gsp variables, but I don't know how to manage when they are empty or null (in this case they are equals to "")

Any suggestion?

Instead defining birthdate & cityName in gsps by calling controller method, just pass them as a model values.

Like in your controller:

def privacyPolicy(Long id){

    def patientInstance = Patient.get(id)

    if (!patientInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'patient.label', default: 'Patient'), id])
        redirect(action: "list")
        return
    }

    if(patientInstance.cf.equals("")) {
        flash.message = message(code: 'cf.not.present.message', args: [message(code: 'patient.label', default: 'Patient'), id])
        redirect(action: "list")
        return
    }

    [patientInstance: patientInstance, birthdate: getDateFromFiscalCode(patientInstance.cf),
            cityName: getBirthplaceFromFiscalCode(patientInstance.cf)]

}

Using the controller in the way you used in gsp may work but is not up to the mark.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM