2014-02-21 39 views
2

我正在玩afBedSheet并希望处理所有到目录的请求。 例如到/ ABCD的请求调用abcdMethod#DoSomething的如何路由到目录?

我必须设置为

@Contribute { serviceId="Routes" } 
static Void contributeRoutes(OrderedConfig conf) { 
    conf.add(Route(`/abcd/?`, abcdMethod#doSomething)) 
} 

航线然而,当我浏览到/ ABCD,我得到404错误:(

如何使?这项工作

回答

1

确保您的路由处理方法doSomething()确实带任何参数例如,以下内容作为Example.fan

using afIoc 
using afBedSheet 

class MyRoutes { 
    Text abcdMethod() { 
    return Text.fromPlain("Hello from `abcd/`!") 
    } 
} 

class AppModule { 
    @Contribute { serviceId="Routes" } 
    static Void contributeRoutes(OrderedConfig conf) { 
    conf.add(Route(`/abcd/?`, MyRoutes#abcdMethod)) 
    } 
} 

class Example { 
    Int main() { 
    afBedSheet::Main().main([AppModule#.qname, "8080"]) 
    } 
} 

而且随着运行:

> fan Example.fan -env dev 

(追加-env dev将列出404页上的所有可用路由)

因为/abcd/?一直尾随?,它将匹配两个文件http://localhost:8080/abcd的URL和http://localhost:8080/abcd/的目录URL。但请注意,不会与匹配/abcd中的任何网址。

要匹配文件里面/abcd,一个URI参数添加到您的路线方法(捕捉路径),并改变你的路线:

/abcd/** only matches direct descendants --> /abcd/wotever 

/abcd/*** will match subdirectories too --> /abcd/wot/ever 

例如:

using afIoc 
using afBedSheet 

class MyRoutes { 
    Text abcdMethod(Uri? subpath) { 
    return Text.fromPlain("Hello from `abcd/` - the sub-path is $subpath") 
    } 
} 

class AppModule { 
    @Contribute { serviceId="Routes" } 
    static Void contributeRoutes(OrderedConfig conf) { 
    conf.add(Route(`/abcd/***`, MyRoutes#abcdMethod)) 
    } 
} 

class Example { 
    Int main() { 
    afBedSheet::Main().main([AppModule#.qname, "8080"]) 
    } 
}