Skip to content

Instantly share code, notes, and snippets.

@ntung
Forked from itsmeritesh/SitemapController.groovy
Created October 8, 2022 16:25
Show Gist options
  • Save ntung/dc99afd7cd39279c7da14ea8acbcc786 to your computer and use it in GitHub Desktop.
Save ntung/dc99afd7cd39279c7da14ea8acbcc786 to your computer and use it in GitHub Desktop.

Revisions

  1. @itsmeritesh itsmeritesh revised this gist Sep 18, 2013. 1 changed file with 3 additions and 7 deletions.
    10 changes: 3 additions & 7 deletions SitemapController.groovy
    Original file line number Diff line number Diff line change
    @@ -34,8 +34,7 @@ class SitemapController {
    mkp.urlset(xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
    'xmlns:xsi': "http://www.w3.org/2001/XMLSchema-instance",
    'xsi:schemaLocation': "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd") {

    def controllerActionNames = [:]

    grailsApplication.controllerClasses.each { controller ->
    Class controllerClass = controller.clazz

    @@ -47,15 +46,12 @@ class SitemapController {
    controllerClass.methods.each { Method method ->

    if (method.getAnnotation(Action) && !actionsToExclude.contains(method.name)) {
    def actions = controllerActionNames[logicalControllerName] ?: []
    actions << method.name


    mkp.url {
    loc(g.createLink(absolute: true, controller: logicalControllerName, action: method.name))
    changefreq('hourly')
    priority(0.8)
    }
    controllerActionNames[logicalControllerName] = actions
    }
    }
    }
    }
  2. @itsmeritesh itsmeritesh created this gist Sep 18, 2013.
    66 changes: 66 additions & 0 deletions SitemapController.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    /**
    * A Sitemap controller that automatically generates sitemap.xml for a grails website.
    *
    * Be sure to notice the controllerNamesToExclude and actionsToExclude lists
    *
    * References:
    * http://stackoverflow.com/questions/3748125/xml-sitemap-in-grails
    * http://stackoverflow.com/questions/2956294/reading-out-all-actions-in-a-grails-controller
    *
    */
    package com.muhive
    import groovy.xml.MarkupBuilder
    import java.lang.reflect.Method
    import grails.web.Action

    class SitemapController {

    def grailsApplication

    /**
    * If you want to exclude any controllers in the sitemap, especially Error controllers and services etc, include them in this array
    */
    def controllerNamesToExclude = [ 'sitemap', 'error']

    /**
    * If you want to certain actions excluded, include them in this array. All actions with this name will be ignored
    */
    def actionsToExclude = ['submitForm']

    def index = {
    StringWriter writer = new StringWriter()
    MarkupBuilder mkp = new MarkupBuilder(writer)
    mkp.mkp.xmlDeclaration(version: "1.0", encoding: "UTF-8")
    mkp.urlset(xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9",
    'xmlns:xsi': "http://www.w3.org/2001/XMLSchema-instance",
    'xsi:schemaLocation': "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd") {

    def controllerActionNames = [:]
    grailsApplication.controllerClasses.each { controller ->
    Class controllerClass = controller.clazz

    // skip controllers in plugins
    if (controllerClass.name.startsWith('com.muhive') && !controllerNamesToExclude.contains(controller.logicalPropertyName)) {
    String logicalControllerName = controller.logicalPropertyName

    // get the actions defined as methods (Grails 2)
    controllerClass.methods.each { Method method ->

    if (method.getAnnotation(Action) && !actionsToExclude.contains(method.name)) {
    def actions = controllerActionNames[logicalControllerName] ?: []
    actions << method.name

    mkp.url {
    loc(g.createLink(absolute: true, controller: logicalControllerName, action: method.name))
    changefreq('hourly')
    priority(0.8)
    }
    controllerActionNames[logicalControllerName] = actions
    }
    }
    }
    }
    }
    render(text: writer.toString(),contentType: "text/xml", encoding: "UTF-8")
    }
    }