按需添加URL映射 [英] Add URL Mapping on demand
问题描述
import enums.URLMapped
class HomeController {
@URLMapped(url =/,alias =home)
def landingPage(){
render(view:/ index)
}
}
如何在 URLMapping.groovy $ c中添加此URL值$ c>动态?我可以这样做:
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
//我的注释抓取器/解析器
(GrailsApplication.getControllerClasses()中的DefaultGrailsControllerClass控制器){
for(Method method in controller.getClazz()。getDeclaredMethods()){
if(!method.isSynthetic()){
if(method.isAnnotationPresent(URLMapped.class)){
URLMapped url = method。 getAnnotation(URLMapped.class)
名称$ {url.alias()}:$ {url.url()}{
action =$ {method.getName()}
controller =$ {StringUtils。我想在上面实现的是这个静态的URL:$ b $($。
/ *) b姓名:/{
action =landingPage
controller =Home
}
* /
}
}
}
}
}
}
但问题是静态映射
是一个闭包,你不应该假设在它内部循环(?)。那么如何添加我的网址?我始终可以将所有链接的所有静态URL都放在这里,这只是令人费解的维护。
你可以通过注释去掉这个想法,但是将你的项目分成几个插件(每个插件都是一个单独的grails应用程序),它们与gradle绑定在一起。
一些有用的信息:
- https://docs.gradle.org/current/userguide/custom_plugins.html
- https://docs.gradle.org/current/userguide/multi_project_builds.html
My grails project has at least 20 controllers each with at least 4 methods each, and it is continuing to grow. I decided to annotate every method like this:
import enums.URLMapped
class HomeController {
@URLMapped(url="/", alias="home")
def landingPage() {
render(view: "/index")
}
}
How can I append this URL value inside the URLMapping.groovy
dynamically? I could do something like:
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"
}
*/
}
}
}
}
}
}
But the problem is that static mapping
is a closure and you shouldn't suppose to loop (?) inside it. So how can I add my URL? I could always put all static URL for all of our links here, it's just getting tedious to maintain.
I would strongly recommend you get rid of this idea with annotations but to divide your project into several plugins (each - a separate grails-app) tied together with gradle.
Some useful info:
- https://docs.gradle.org/current/userguide/custom_plugins.html
- https://docs.gradle.org/current/userguide/multi_project_builds.html
这篇关于按需添加URL映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!