[英]Correct Implementation of Export Plugin for Grails to export a GSP as PDF
[英]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中定义birthdate
和cityName
,只需将它们作为模型值传递即可。
就像在您的控制器中一样:
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.