Java Can';t在我的模板文件中获取资源文件(使用Restlet和Freemarker)

Java Can';t在我的模板文件中获取资源文件(使用Restlet和Freemarker),java,jakarta-ee,http-status-code-404,restlet,Java,Jakarta Ee,Http Status Code 404,Restlet,我正在尝试用Restlet开发一个webapp,但在访问我的/public/css/*和/public/js/*时遇到了一个小问题 我在控制台中有这样的消息: INFO: 2012-03-10 23:52:59 127.0.0.1 - - 8182 GET /public/css/bootstrap-responsive.min.css - 404 439 0 0 http://localhost:8182 Mozilla/5.0 (X11;

我正在尝试用Restlet开发一个webapp,但在访问我的/public/css/*和/public/js/*时遇到了一个小问题

我在控制台中有这样的消息:

INFO: 2012-03-10    23:52:59    127.0.0.1   -   -   8182    GET /public/css/bootstrap-responsive.min.css    -   404 439 0   0   http://localhost:8182   Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.11 (KHTML, like Gecko) Ubuntu/11.10 Chromium/17.0.963.65 Chrome/17.0.963.65 Safari/535.11   http://localhost:8182/hello
我目前只有一个使用HTML模板的HelloWorld:

public class RestletServerTest extends ServerResource {

    public static void main(String[] args) throws Exception {
        Component component = new Component();
        component.getServers().add(Protocol.HTTP, 8182);

        component.getDefaultHost().attach("/hello", new HelloWorldApplication());

        component.start();
    }
}

public class HelloWorldApplication extends Application {
    private Configuration configuration;

    @Override
    public synchronized Restlet createInboundRoot() {
        configuration = new Configuration();
        try {
            configuration.setDirectoryForTemplateLoading(new File("src/main/webapp/WEB-INF/template"));
            configuration.setObjectWrapper(new BeansWrapper());
        } catch (IOException e) {
            e.printStackTrace();
        }

        Router router = new Router(getContext());

        router.attach("", HelloWorldResource.class);

        return router;
    }

    public Configuration getConfiguration() {
        return configuration;
    }
}

public class HelloWorldResource extends ServerResource {
    @Get
    public Representation get() {
        TemplateRepresentation templateRepresentation = new TemplateRepresentation("hello.ftl", getApplication()
                .getConfiguration(), MediaType.TEXT_HTML);
        return templateRepresentation;
    }
    @Override
    public HelloWorldApplication getApplication() {
        return (HelloWorldApplication) super.getApplication();
    }
}

<!DOCTYPE html>
<html>
    <head>
        <title>Hello World</title>
        <link rel="stylesheet" href="/public/css/bootstrap-responsive.min.css" />
        <link rel="stylesheet" href="/public/css/bootstrap.min.css" />

        <script type="text/javascript" src="/public/js/bootstrap.min.js"></script>
    </head>
    <body>
        <h1>Hello World</h1>
    </body>
</html>
公共类RestletServerTest扩展了ServerResource{
公共静态void main(字符串[]args)引发异常{
组件=新组件();
component.getServers().add(Protocol.HTTP,8182);
附加(“/hello”,新的HelloWorldApplication());
component.start();
}
}
公共类HelloWorldApplication扩展了应用程序{
私有配置;
@凌驾
公共同步Restlet createInboundRoot(){
配置=新配置();
试一试{
setDirectoryForTemplateLoading(新文件(“src/main/webapp/WEB-INF/template”);
setObjectWrapper(新的BeansWrapper());
}捕获(IOE异常){
e、 printStackTrace();
}
路由器=新路由器(getContext());
附加(“,HelloWorldResource.class”);
返回路由器;
}
公共配置getConfiguration(){
返回配置;
}
}
公共类HelloWorldResource扩展了ServerResource{
@得到
公众代表{
TemplateRepresentation TemplateRepresentation=新的TemplateRepresentation(“hello.ftl”,getApplication())
.getConfiguration(),MediaType.TEXT_HTML);
返回模板表示;
}
@凌驾
公共HelloWorldApplication getApplication(){
return(HelloWorldApplication)super.getApplication();
}
}
你好,世界
你好,世界
CSS和JS文件位于“/src/main/webapp/public”文件夹中。 我忘了什么

多谢各位

Florian.

我找到了解决方案:

@Override
    public synchronized Restlet createInboundRoot() {
        Directory directory = new Directory(getContext(), LocalReference.createFileReference("/home/florian/dev/wkspace/myproject/src/main/webapp/public"));
        directory.setListingAllowed(true);
        Router router = new Router(getContext());

        router.attachDefault(new HomeApplication());
        router.attach("/static", directory);
        router.attach("/hello", new HelloWorldApplication());

        return router;
    }

但是我想创建一个相对路径。

也许您可以尝试使用以下类之一:ClassTemplateLoader或WebappTemplateLoader

例如,可以按如下所述创建ClassTemplateLoader类:

Configuration configuration = new Configuration();
configuration.setTemplateLoader(
            new ClassTemplateLoader(ClassInTheClasspath.class,
                            "/rootpath/under/classpath/");
configuration.setObjectWrapper(new DefaultObjectWrapper());
(...)
这允许在类路径中的path/rootpath/下的/classpath/下查找模板。在此上下文中,第一个/是类路径的根

希望对你有帮助。
Thierry

我更喜欢使用相对路径而不是绝对文件引用。这在represente()方法中使用,该方法返回该资源的表示形式:

Representation indexFtl = new ClientResource(LocalReference.createClapReference(getClass().getPackage()) + "/templates/index.ftl.html").get();
固定


DirectoryServerResource
中为我做了这项工作。

如果从URL中删除
/public/
是否有效?您的css是否位于:
/src/main/webapp/public/css/bootstrap.min.css
?它可读吗?其他请求是否成功(哪一个)?如果我删除
/public/
,并且无法访问
/src/main/webapp/public/css/bootstrap.min.css,则该请求无效。目前我真的不明白。也许我必须为我的公用文件夹制定一条路线,但我不知道该怎么做
733 firstDotIndex = fullEntryName.indexOf('.');
733 firstDotIndex = fullEntryName.lastIndexOf('.');