【问题标题】:XML sitemap in GrailsGrails 中的 XML 站点地图
【发布时间】:2011-04-14 11:17:25
【问题描述】:

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

你们会怎么做呢?您会考虑什么以及如何实施?

谢谢!

【问题讨论】:

    标签: grails grails-plugin


    【解决方案1】:

    我正在使用 UrlMappings.groovy 在 Grails 上制作站点地图,并且不需要控制器来进行此练习。我把下一个代码放在 UrlMappings 中:

    "/robots.txt" (view: "/robots")
    "/sitemap.xml" (view: "/sitemap")
    

    我将我的站点地图创建为带有 xml 编码的 gsp,sitemap.gsp 的示例:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    
       <url>
    
          <loc>http://www.putYourSite.com.py/</loc>
    
          <lastmod>putAdate</lastmod>
    
          <changefreq>daily</changefreq>
    
          <priority>1.0</priority>
    
       </url>
    
    </urlset>
    

    【讨论】:

      【解决方案2】:

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

      以下是我们为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)
                          }
                      }
                 }
          }
      

      网址映射

      class UrlMappings {
          static mappings = {
      
              "/sitemap"{
                  controller = 'sitemap'
                  action = 'sitemap'
              }
          }
      }
      

      【讨论】:

      • 啊,所以你正在即时构建它?
      • 是的,由于动态部分以及它不会受到太多影响的事实,所以我们没有费心实现任何缓存(但这并不难)。跨度>
      • @Don 我已将它们添加到答案中
      • 这个BaseQueueController是从哪里来的?
      • @CassioLandim 它是我们自己的基类,具有我们应用程序通用的逻辑。它不包含任何特定于站点地图生成的内容,我将更新示例
      猜你喜欢
      • 1970-01-01
      • 2021-05-23
      • 2011-05-13
      • 2014-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多