Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails Controller Actions may not be overloaded

Tags:

grails

groovy

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?

like image 258
AnthonyMDev Avatar asked Feb 15 '23 03:02

AnthonyMDev


1 Answers

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.

like image 106
Ian Roberts Avatar answered Mar 20 '23 04:03

Ian Roberts