没有模板引擎的 Micronaut 视图渲染

mar*_*x07 2 viewrendering micronaut

micronaut 可以渲染静态文件吗?

我添加compile 'io.micronaut:micronaut-views'build.gradle

控制器:

@Controller("/main")
public class MainController {

    @View("index.html")
    @Get("/")
    public HttpResponse index() {
        return HttpResponse.ok();
    }
}
Run Code Online (Sandbox Code Playgroud)

index.html文件在/src/main/resources/views/index.html

请求localhost:8080/main不呈现视图。

Jam*_*eeh 6

这是按设计运行的。当无法将模型应用于视图时,应用视图模型逻辑毫无意义。

只需配置静态资源即可达到预期效果。例如:

micronaut:
  router:
    static-resources:
      main:
        paths: classpath:views
        mapping: /main/**           
Run Code Online (Sandbox Code Playgroud)

通过上述配置,index.html在访问 URLsrc/main/resources/views时将提供一个文件/main