我正在尝试使用 Restlet 开发一个 web 应用程序,但在访问 /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>
CSS 和 JS 文件位于“/src/main/webapp/public”文件夹中。
我忘记了什么?
谢谢。
Florian.
也许您可以尝试使用以下类之一:ClassTemplateLoader 或 WebappTemplateLoader。
例如,您可以按如下所述使用 ClassTemplateLoader 类:
Configuration configuration = new Configuration();
configuration.setTemplateLoader(
new ClassTemplateLoader(ClassInTheClasspath.class,
"/rootpath/under/classpath/");
configuration.setObjectWrapper(new DefaultObjectWrapper());
(...)
这允许在路径 /rootpath/under/classpath/ 下的类路径中查找模板。在这种情况下,第一个 / 是类路径的根。
希望对您有帮助。
蒂埃里
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)