0
私の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を置くことができる、ちょうど維持するのが面倒です。