gpt4 book ai didi

java - 如何在 Grizzly http 服务器中为 Jersey 设置模板基本路径?

转载 作者:搜寻专家 更新时间:2023-10-31 19:46:05 25 4
gpt4 key购买 nike

使用 Apache JerseyGrizzly http 服务器,如何设置基本模板路径?

因为我没有使用 Servlet 容器,所以我使用绝对文件路径分配模板基本路径。但 Jersey 响应 404。

以下是我的项目设置

项目目录:

src
└─ java
.....
└─ resources
   └─ templates
└─ index.mustache

应用:

public class ExampleApplication extends ResourceConfig {

public CustomTableApplication() {
packages("com.example.app");

setupTemplateEngine();
}

private void setupTemplateEngine() {
property(MvcFeature.TEMPLATE_BASE_PATH, "/templates/");
register(MustacheMvcFeature.class);
}
}

Controller :

@Path("/")
public class Index {

@GET
@Template(name = "index")
public String index() {
return "";
}
}

我如何创建 HttpServer:

HttpServer server = new HttpServer();
NetworkListener listener = new NetworkListener("example", "localhost", 8080);
server.addListener(listener);

ServerConfiguration config = server.getServerConfiguration();
config.addHttpHandler(createJerseyHandler(), "/*");

最佳答案

我误解了 Jersey 在解析模板名称时如何找到模板文件。

使用资源 com.example.app.Index 和模板基本路径 /templates

相对模板引用 @Template(name = "index")

/templates/com/example/app/Index/index.mustache

绝对模板引用 @Template(name = "/index")

/templates/index.mustache

更多细节见文档 : 19.3. Absolute vs. Relative template reference

关于java - 如何在 Grizzly http 服务器中为 Jersey 设置模板基本路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23313849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com