I'm trying to use an overloaded method to append XML in a controller in Grails 2.3.4.
I have the following overloaded methods in my ReportController.
String makePhotoXml(StringBuilder sb, Report r, String url, String desc) {
sb.append("<photo>")
sb.append(Utilities.makeElementCdata("url", url))
sb.append(Utilities.makeElementCdata("caseId", r.caseId))
sb.append(Utilities.makeElementCdata("type", r.type))
sb.append(Utilities.makeElementCdata("date", r.dateCreated.format('MM/dd/yy')))
sb.append(Utilities.makeElementCdata("address", r.address))
sb.append("<extra>extra</extra>")
sb.append(Utilities.makeElementCdata("description", desc))
sb.append("</photo>")
}
String makePhotoXml(List<Report> reports) {
StringBuilder sb = new StringBuilder()
sb.append("<photos>")
sb.append("<title>Photos</title>")
for (Report r : reports) {
for (Photo photo : r.photos) {
makePhotoXml(sb, r, photo.url(), photo.description)
}
for (Document doc : r.photoDocuments) {
makePhotoXml(sb, r, doc.url(-1), doc.getDescription())
}
}
sb.append("</photos>")
}
When running the application I get this compiler error:
| Error Compilation error: startup failed:
/Users/Anthony/GrailsApps/AppOrderWeb/grails-app/controllers/com/apporder/ReportController.groovy: 1360: Controller actions may not be overloaded. The [makePhotoXml] action has been overloaded in [com.apporder.ReportController]. @ line 1360, column 5.
String makePhotoXml(StringBuilder sb, Report r, String url, String desc) {
I thought that Groovy and Grails supported method overloading. Any ideas on how to work around this and make this overloaded method work?
Groovy in general does allow method overloading, but Grails forbids it for the specific case of controller actions. If you want utility methods in your controller, you need to make them private
or protected
so Grails does not try and treat them as web-visible actions.
Alternatively, it would be more Grails-y to move the helper methods into a service instead of having them in the controller.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With