简体   繁体   English

Ratpack:如何从类路径渲染文件?

[英]Ratpack: how to render file from classpath?

I want to render or send a "static" file from the classpath. 我想从类路径中呈现或发送“静态”文件。 Logistically the file comes from a referenced project and is available under classpath root. 从逻辑上讲,该文件来自引用的项目,并且在类路径根目录下可用。

My code so far: 到目前为止,我的代码:

  handlers {

    get{
      log.info "trying to get index ${file( '/index.html' )}"
      render file( '/index.html' )
    }

  }

upon calling the url, I'm getting a 404 error page back and in the logs I see: 调用该网址后,我得到了404错误页面,并且在日志中看到:

INFO ratpack - trying to get index C:\\my-project\\build\\resources\\main\\index.html INFO ratpack-尝试获取索引C:\\ my-project \\ build \\ resources \\ main \\ index.html

I tried to add a spring-like classpath: prefix with no positive effect. 我试图添加一个类似spring的classpath:前缀,但没有任何积极效果。

What am I missing? 我想念什么? A BaseDir set up? 建立BaseDir

seems there is no support of resources rendering in ratpack: 似乎在ratpack中不支持资源渲染:

FileSystemBinding.of() always creates DefaultFileSystemBinding FileSystemBinding.of()始终创建DefaultFileSystemBinding

And DefaultFileSystemBinding.file() currently supports only plain file system. 并且DefaultFileSystemBinding.file()当前仅支持纯文件系统。

however easy to add it: 多么容易添加:

./www/index.htm ./www/index.htm

<h1>hello world</h1>

./Main.groovy ./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)
        }
    }
}

to run it: 运行它:

groovy -cp ./www Main.groovy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM