2017-02-27 5 views
0

My Grails 프로젝트에는 각각 적어도 4 개의 메소드가있는 제어기가 20 개 이상 있으며 계속 증가하고 있습니다. 이 같은 모든 방법을 주석하기로 결정요청시 URL 매핑 추가

import enums.URLMapped 

class HomeController { 
    @URLMapped(url="/", alias="home") 
    def landingPage() { 
     render(view: "/index") 
    } 
} 
나는 동적으로 URLMapping.groovy 안쪽이 URL 값을 추가 할 수있는 방법

? 내가 좋아하는 뭔가를 할 수 :

import enums.URLMapped 
import java.lang.reflect.Method 
import org.apache.commons.lang.StringUtils 
import org.codehaus.groovy.grails.commons.DefaultGrailsControllerClass 
import org.codehaus.groovy.grails.commons.GrailsApplication 

class UrlMappings { 
    static mappings = { 
     "/$controller/$action?/$id?(.$format)?"{ 
      constraints { 
       // apply constraints here 
      } 
     } 

     // My annotation crawler/parser 
     for(DefaultGrailsControllerClass controller in GrailsApplication.getControllerClasses()) { 
      for(Method method in controller.getClazz().getDeclaredMethods()) { 
       if(!method.isSynthetic()) { 
        if(method.isAnnotationPresent(URLMapped.class)) { 
         URLMapped url = method.getAnnotation(URLMapped.class) 
         name "${url.alias()}": "${url.url()}" { 
          action="${method.getName()}" 
          controller="${StringUtils.capitalize(controller.getClazz().getSimpleName().split(/Controller/).getAt(0)}" 
         } 
         /* What I want to achieve above is this static URL: 
          name home: "/" { 
           action="landingPage" 
           controller="Home" 
          } 
         */ 
        } 
       } 
      } 
     } 
    } 
} 

을 그러나 문제는 static mapping가 폐쇄하고 당신이 그것을 내부 루프를 생각하지해야한다는 것입니다 (?). 그러면 내 URL을 어떻게 추가 할 수 있습니까? 나는 항상 여기에있는 모든 링크에 대해 모든 고정 URL을 넣을 수 있습니다. 유지 보수가 지루해졌습니다.

답변