简体   繁体   English

grails g:在gsp页面中设置为空值

[英]grails g:set with null values in gsp page

In my grails project I am using PDF plugin. 在我的grails项目中,我正在使用PDF插件。 For this purpose, I use the following link to generate a PDF: 为此,我使用以下链接生成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: privacyPolicy()方法如下:

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: gsp页面有两个定义如下的变量:

<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 这两个变量都取决于cf ,对于patientInstance可能不存在

Analyzing the flow in debug mode(when cf is empty), I've noticed that privacyPolicy() is called twice. 在调试模式下分析流(当cf为空时),我注意到privacyPolicy()被调用了两次。 First time it enters into second if, but then it enters in first if. 第一次进入第二个if,但随后进入第一个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 "") 我认为这取决于gsp变量,但是我不知道如何管理它们为空或为空(在这种情况下,它们等于“”)

Any suggestion? 有什么建议吗?

Instead defining birthdate & cityName in gsps by calling controller method, just pass them as a model values. 而不是通过调用控制器方法在gsps中定义birthdatecityName ,只需将它们作为模型值传递即可。

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. 以您在gsp中使用的方式使用控制器可能有效,但不符合要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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