Search code examples
javajakarta-eehttp-status-code-404restlet

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


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.


Solution

  • 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