2016-03-19 9 views
1

Bir sitemap.xml dosyasını dinamik olarak oluşturmak istiyorum. O zaman kontrol biriminde tüm URL adreslerini almam gerekiyorsa, Bu tür bir şeyi nasıl çözebilirim? Tek istediğim, yay ile sitemap.xml oluşturmaktır.İlkbaharda GET yöntemiyle requestMapping URL'nin tümünü alabilir miyim?

Sitemap.xml, sitemde bir arama motorunun tarama yapacağı tüm URL'ye sahip ve bu yüzden bu çözüme ihtiyacım var.

cevap

2

Aşağıdaki kod, @Controller sınıflarındaki tür ve yöntem düzeyindeki @RequestMapping ek açıklamalardan tüm RequestMappingInfo örneklerini ayıklar.

// context = ApplicationContext 
Map<String, RequestMappingHandlerMapping> matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, 
    RequestMappingHandlerMapping.class, true, false); 

if (!matchingBeans.isEmpty()) { 
    ArrayList<HandlerMapping> handlerMappings = new ArrayList<HandlerMapping>(matchingBeans.values()); 
    AnnotationAwareOrderComparator.sort(handlerMappings); 

    RequestMappingHandlerMapping mappings = matchingBeans.get("requestMappingHandlerMapping"); 
    Map<RequestMappingInfo, HandlerMethod> handlerMethods = mappings.getHandlerMethods(); 

    for (RequestMappingInfo requestMappingInfo : handlerMethods.keySet()) { 
     RequestMethodsRequestCondition methods = requestMappingInfo.getMethodsCondition(); 

     // Get all requestMappingInfos with 
     // 1) default request-method (which is none) 
     // 2) or method=GET 
     if (methods.getMethods().isEmpty() || methods.getMethods().contains(RequestMethod.GET)) { 
      System.out.println(requestMappingInfo.getPatternsCondition().getPatterns() + " -> produces " + 
        requestMappingInfo.getProducesCondition()); 
     } 
    } 
} 

Muhtemelen hata sayfaları için eşlemeleri filtrelemeniz gerekir. RequestMappingInfo nesne gibi @RequestMapping üzerinde ek açıklamalar tanımlar olduğunu ilgili tüm haritalama bilgileri içerir: ->@RequestMapping(method=RequestMethod.GET)

  • RequestMappingInfo.getPatternsCondition().getPatterns() -

    • RequestMappingInfo.getMethods() Daha fazla ayrıntı>@RequestMapping(value = "/foo")
    • vb RequestMappingInfo

    Daha fazla c için ör. ViewController yapılandırmaları, hem de sen SimpleUrlHandlerMapping türü için filtre gerekir:

    Map<String, SimpleUrlHandlerMapping> matchingUrlHandlerMappingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(context, 
          SimpleUrlHandlerMapping.class, true, false); 
    SimpleUrlHandlerMapping mappings = matchingUrlHandlerMappingBeans.get("viewControllerHandlerMapping"); 
    System.out.println(mappings.getUrlMap()); 
    
  • +0

    Çok teşekkür ederim :) – Richard

    +0

    yardım edebilirim sevindim! – fateddy

    İlgili konular