似乎在ratpack中不支持资源渲染:
FileSystemBinding.of()总是创建DefaultFileSystemBinding
而DefaultFileSystemBinding.file()目前只支持普通文件系统。
但是很容易添加它:
./www/index.htm
<h1>hello world</h1>
./Main.groovy
@Grapes([
@Grab(group='io.ratpack', module='ratpack-groovy', version='1.7.3', transitive=false),
@Grab(group='io.ratpack', module='ratpack-core', version='1.7.3'),
@Grab(group='io.ratpack', module='ratpack-guice', version='1.7.3'),
@Grab(group='org.slf4j', module='slf4j-simple', version='1.7.26')
])
import static ratpack.groovy.Groovy.ratpack
import java.nio.file.Paths
import java.nio.file.Path
@groovy.transform.CompileStatic
Path resource(String rname){
URL url = this.getClass().getClassLoader().getResource(rname)
assert url : "resource not found: $rname"
return Paths.get( url.toURI() )
}
ratpack {
handlers {
get { //render default file
render resource('index.htm')
}
get(":filename") { //render other files
render resource(pathTokens.filename)
}
}
}
运行它:
groovy -cp ./www Main.groovy