Can't get resource files in my template files (using Restlet and Freemarker)

戏子无情 提交于 2019-12-11 03:08:33

问题


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

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 and JS files are in "/src/main/webapp/public" folder. I forgot something?

Thank you.

Florian.


回答1:


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

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());
(...)

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.

Hope it helps you. Thierry




回答2:


I found the solution :

@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;
    }

But I would like to make a relative path.




回答3:


I prefere to use relative path instead of absolute FileReference. This is used in the method represent(), which return the representation of that resource:

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



回答4:


fixing

733 firstDotIndex = fullEntryName.indexOf('.');

to

733 firstDotIndex = fullEntryName.lastIndexOf('.');

in DirectoryServerResource did the job for me.



来源:https://stackoverflow.com/questions/9651019/cant-get-resource-files-in-my-template-files-using-restlet-and-freemarker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!