我正在使用afBedSheet,并希望处理目录中的所有请求。例如对/abcd的请求调用abcdMethod#doSomething
我把路线设为
@Contribute { serviceId="Routes" }
static Void contributeRoutes(OrderedConfig conf) {
conf.add(Route(`/abcd/?`, abcdMethod#doSomething))
}然而,当我浏览到/abcd时,我得到了404个错误:(
我该怎么做呢?
发布于 2014-02-21 08:31:52
确保您的路由处理程序方法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中的任何URL不匹配。
若要匹配/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"])
}
}https://stackoverflow.com/questions/21924962
复制相似问题