簡體   English   中英

grails g:在gsp頁面中設置為空值

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

在我的grails項目中,我正在使用PDF插件。 為此,我使用以下鏈接生成PDF:

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

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]

}

gsp頁面有兩個定義如下的變量:

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

這兩個變量都取決於cf ,對於patientInstance可能不存在

在調試模式下分析流(當cf為空時),我注意到privacyPolicy()被調用了兩次。 第一次進入第二個if,但隨后進入第一個if。

控制台中的錯誤如下:

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

我認為這取決於gsp變量,但是我不知道如何管理它們為空或為空(在這種情況下,它們等於“”)

有什么建議嗎?

而不是通過調用控制器方法在gsps中定義birthdatecityName ,只需將它們作為模型值傳遞即可。

就像在您的控制器中一樣:

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)]

}

以您在gsp中使用的方式使用控制器可能有效,但不符合要求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM