Grails中的XML站点地图

Rya*_*nch 7 grails grails-plugin

我正在尝试找出为Grails应用程序生成XML站点地图的最佳方法(如此处所述:http://www.sitemaps.org/).我不知道有任何现有的插件,所以我可能会建立一个.但是,我想首先得到社区的意见.除了支持标准控制器/操作之外,我认为支持内容驱动的应用程序也很好,例如,基于title属性可以生成URL.

你们怎么会这样做?你会考虑什么,你将如何实现它?

谢谢!

lee*_*tts 16

站点地图非常特定于每个应用程序,因此我不确定是否有足够的公共代码可以提取到插件.

以下是我们为http://www.shareyourlove.com生成站点地图的方法.正如你所看到的那样,由于Groovy/Grails很好的XML语法,它非常简单和DRY

class SitemapController{

        def sitemap = {
            render(contentType: 'text/xml', encoding: 'UTF-8') {
                mkp.yieldUnescaped '<?xml version="1.0" encoding="UTF-8"?>'
                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") {
                    url {
                        loc(g.createLink(absolute: true, controller: 'home', action: 'view'))
                        changefreq('hourly')
                        priority(1.0)
                    }
                    //more static pages here
                    ...
                    //add some dynamic entries
                    SomeDomain.list().each {domain->
                    url {
                        loc(g.createLink(absolute: true, controller: 'some', action: 'view', id: domain.id))
                        changefreq('hourly')
                        priority(0.8)
                    }
                }
           }
    }
Run Code Online (Sandbox Code Playgroud)

URL映射

class UrlMappings {
    static mappings = {

        "/sitemap"{
            controller = 'sitemap'
            action = 'sitemap'
        }
    }
}
Run Code Online (Sandbox Code Playgroud)