作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
使用 Apache Jersey 和 Grizzly 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
关于java - 如何在 Grizzly http 服务器中为 Jersey 设置模板基本路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23313849/
我是一名优秀的程序员,十分优秀!