无法在我的模板文件中获取资源文件(使用Restlet和Freemarker) [英] Can't get resource files in my template files (using Restlet and Freemarker)

查看:217
本文介绍了无法在我的模板文件中获取资源文件(使用Restlet和Freemarker)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Restlet开发一个webapp,我在访问my / public / css / *和/ public / js /*时遇到了一些问题。

I'm trying to develop a webapp with Restlet and I have a little problem for access to my /public/css/* and /public/js/*.

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

I have messages like this in the console :

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:

I currently only have a HelloWorld using a HTML template :

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>

CSS和JS文件位于/ src / main / webapp / public文件夹中。
我忘记了什么?

CSS and JS files are in "/src/main/webapp/public" folder. I forgot something?

谢谢。

Florian。

推荐答案

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

Perhaps can you try to use one of following classes: ClassTemplateLoader or WebappTemplateLoader.

例如,您可以使用如下所述的ClassTemplateLoader类:

For example, you can the ClassTemplateLoader class as described below:

Configuration configuration = new Configuration();
configuration.setTemplateLoader(
            new ClassTemplateLoader(ClassInTheClasspath.class,
                            "/rootpath/under/classpath/");
configuration.setObjectWrapper(new DefaultObjectWrapper());
(...)

这允许在路径/ rootpath /下的类路径中查找模板在/ classpath /。在这个上下文中,第一个/是类路径的根。

This allows finding your templates in the classpath under the path /rootpath/under/classpath/. In this context, the first / is the root of your classpath.

希望它可以帮到你。
Thierry

Hope it helps you. Thierry

这篇关于无法在我的模板文件中获取资源文件(使用Restlet和Freemarker)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆